當前位置: 首頁>>代碼示例>>Java>>正文


Java XMLEncoder類代碼示例

本文整理匯總了Java中java.beans.XMLEncoder的典型用法代碼示例。如果您正苦於以下問題:Java XMLEncoder類的具體用法?Java XMLEncoder怎麽用?Java XMLEncoder使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


XMLEncoder類屬於java.beans包,在下文中一共展示了XMLEncoder類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: write

import java.beans.XMLEncoder; //導入依賴的package包/類
public @Override void write(java.io.Writer w, final Object inst) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    XMLEncoder e = new XMLEncoder(out);
    e.setExceptionListener(new ExceptionListener() {
        public @Override void exceptionThrown(Exception x) {
            Logger.getLogger(XMLBeanConvertor.class.getName()).log(Level.INFO, "Problem writing " + inst, x);
        }
    });
    ClassLoader ccl = Thread.currentThread().getContextClassLoader();
    try {
        // XXX would inst.getClass().getClassLoader() be more appropriate?
        ClassLoader ccl2 = Lookup.getDefault().lookup(ClassLoader.class);
        if (ccl2 != null) {
            Thread.currentThread().setContextClassLoader(ccl2);
        }
        e.writeObject(inst);
    } finally {
        Thread.currentThread().setContextClassLoader(ccl);
    }
    e.close();
    String data = new String(out.toByteArray(), "UTF-8");
    data = data.replaceFirst("<java", "<!DOCTYPE xmlbeans PUBLIC \"-//NetBeans//DTD XML beans 1.0//EN\" \"http://www.netbeans.org/dtds/xml-beans-1_0.dtd\">\n<java");
    w.write(data);
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:25,代碼來源:XMLBeanConvertor.java

示例2: setPersistenceDelegates

import java.beans.XMLEncoder; //導入依賴的package包/類
private static void setPersistenceDelegates(XMLEncoder encoder,
        Class<?>[] types) {
    if (types != null && types.length > 0) {
        PersistenceDelegate persistenceDelegate = new EnumPersistenceDelegate();
        for (int i = 0; i < types.length; i++) {
            encoder.setPersistenceDelegate(types[i], persistenceDelegate);
        }
    }

    // Handle "BiGDecimal" manually (has no default constructor)
    encoder.setPersistenceDelegate(BigDecimal.class,
            new BigDecimalPersistenceDelegate());

    encoder.setPersistenceDelegate(byte[].class,
            new ByteArrayPersistenceDelegate());
    encoder.setPersistenceDelegate(UUID.class, new UUIDDelegate());
}
 
開發者ID:servicecatalog,項目名稱:oscm,代碼行數:18,代碼來源:XMLSerializer.java

示例3: getPriceModel

import java.beans.XMLEncoder; //導入依賴的package包/類
/**
 * Get the price model data for the given context and locales
 * 
 * @return the price model data as an XML encoded list of strings
 */
@GET
@Produces(MediaType.TEXT_PLAIN)
public Response getPriceModel() {

    FileBilling fb = new FileBilling();
    splitParametersToList(contextKeys);
    splitParametersToList(contextValues);

    List<String> priceModelContent = fb.getPriceModel(contextKeys,
            contextValues, locales);


    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try (XMLEncoder xmlEncoder = new XMLEncoder(bos)) {
        xmlEncoder.writeObject(priceModelContent);
    }

    String serializedList = "";
    try {
        serializedList = bos.toString(StandardCharsets.UTF_8.name());
    } catch (UnsupportedEncodingException e) {
    }
    return Response.ok(serializedList).build();
}
 
開發者ID:servicecatalog,項目名稱:oscm,代碼行數:30,代碼來源:PriceModelResource.java

示例4: prepareTestData

import java.beans.XMLEncoder; //導入依賴的package包/類
private static void prepareTestData(final int amount) {
    final int actualYear = new GregorianCalendar().get(Calendar.YEAR);
    final Collection<Customer> customers = new LinkedList<>();
    for (int i = 1; i <= amount; i++) {
        final Calendar birthday = new GregorianCalendar();
        birthday.set(Calendar.YEAR, random(actualYear - 100, actualYear));
        birthday.set(Calendar.DAY_OF_YEAR, random(1, birthday.getActualMaximum(Calendar.DAY_OF_YEAR)));
        final Customer customer = new Customer();
        customer.setId(i);
        customer.setName(UUID.randomUUID().toString().replaceAll("[^a-z]", ""));
        customer.setBirthday(birthday);
        customer.setTransactions(random(0, 100));
        customers.add(customer);
    }
    try (final XMLEncoder encoder = new XMLEncoder(new FileOutputStream(CustomerReportJobConfig.XML_FILE))) {
        encoder.writeObject(customers);
    } catch (final FileNotFoundException e) {
        log.error(e.getMessage(), e);
        System.exit(-1);
    }
}
 
開發者ID:asaen,項目名稱:spring-batch-article,代碼行數:22,代碼來源:BatchApplication.java

示例5: objectXmlEncoder

import java.beans.XMLEncoder; //導入依賴的package包/類
/** 
 * 把java的可序列化的對象(實現Serializable接口)序列化保存到XML文件裏麵,如果想一次保存多個可序列化對象請用集合進行封裝 
 * 保存時將會用現在的對象原來的XML文件內容 
 * @param obj 要序列化的可序列化的對象 
 * @param fileName 帶完全的保存路徑的文件名  
 * @throws FileNotFoundException 指定位置的文件不存在 
 * @throws IOException 輸出時發生異常 
 * @throws Exception 其他運行時異常 
 */ 
public static void objectXmlEncoder(Object obj,String fileName) 
 throws FileNotFoundException,IOException,Exception 
{   
 //創建輸出文件 
 File fo = new File(fileName); 
 //文件不存在,就創建該文件 
 if(!fo.exists()) 
 { 
  //先創建文件的目錄 
     String path = fileName.substring(0,fileName.lastIndexOf('.')); 
     File pFile = new File(path); 
     pFile.mkdirs();          
 } 
 //創建文件輸出流 
 FileOutputStream fos = new FileOutputStream(fo); 
 //創建XML文件對象輸出類實例 
 XMLEncoder encoder = new XMLEncoder(fos);   
 //對象序列化輸出到XML文件 
 encoder.writeObject(obj); 
 encoder.flush();  
 //關閉序列化工具 
 encoder.close(); 
 //關閉輸出流 
 fos.close();     
}
 
開發者ID:LuJiangLin,項目名稱:TestDemo1-github,代碼行數:35,代碼來源:ObjectToXMLUtil.java

示例6: saveAnObjectXML

import java.beans.XMLEncoder; //導入依賴的package包/類
public static void saveAnObjectXML(Object obj, String path) {
    try {
        // Serialize object into XML
        XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
                new FileOutputStream(path)));
        encoder.writeObject(obj);
        encoder.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}
 
開發者ID:phweda,項目名稱:MFM,代碼行數:12,代碼來源:PersistUtils.java

示例7: main

import java.beans.XMLEncoder; //導入依賴的package包/類
public static void main(String[] args) {
    XMLEncoder encoder = new XMLEncoder(System.out);
    encoder.setPersistenceDelegate(Test4646747.class, new MyPersistenceDelegate());
    // WARNING: This can eat up a lot of memory
    Object[] obs = new Object[10000];
    while (obs != null) {
        try {
            obs = new Object[obs.length + obs.length / 3];
        }
        catch (OutOfMemoryError error) {
            obs = null;
        }
    }
    PersistenceDelegate pd = encoder.getPersistenceDelegate(Test4646747.class);
    if (!(pd instanceof MyPersistenceDelegate))
        throw new Error("persistence delegate has been lost");
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:18,代碼來源:Test4646747.java

示例8: run

import java.beans.XMLEncoder; //導入依賴的package包/類
public void run() {
    // run thread a few time
    // object stays the same but NullPointerException appears randomly
    // on dual proccessor a lock is generated
    for (int i = 0; i < THREAD_LENGTH; i++) {
        // create XMLEncoder to ByteArrayOutputStream
        // this is to exclude file locking problems
        XMLEncoder encoder = new XMLEncoder(new ByteArrayOutputStream());
        encoder.setExceptionListener(this);
        // write the object
        // will see randomly null pointer exceptions
        // a bug as object is same through different encode phases
        encoder.writeObject(this.object);
        //close encoder
        encoder.close();
    }
    System.out.println(Thread.currentThread().getName() + " is finished");
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:19,代碼來源:Test4880633.java

示例9: test

import java.beans.XMLEncoder; //導入依賴的package包/類
static void test(AbstractTest object) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();

    XMLEncoder encoder = new XMLEncoder(output);
    encoder.setPersistenceDelegate(
            object.getClass(),
            new DefaultPersistenceDelegate(new String[] {"value"}));

    encoder.writeObject(object);
    encoder.close();

    System.out.print(output);

    ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
    XMLDecoder decoder = new XMLDecoder(input);
    AbstractTest result = (AbstractTest) decoder.readObject();
    decoder.close();

    if (object.getValue() != result.getValue())
        throw new Error("Should be " + object);
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:22,代碼來源:AbstractTest.java

示例10: doSave

import java.beans.XMLEncoder; //導入依賴的package包/類
/**
	 * This code abstracted because it relies on java.beans.* which is not always available
	 * e.g. on Android
	 */

@Override
public void
doSave(
	OutputStream 	out,
	Map				usersMap )
{
	UserManagerConfig config = new UserManagerConfig();
	List users = new ArrayList( usersMap.values() );
	config.setUsers(users);

	XMLEncoder encoder = new XMLEncoder( new BufferedOutputStream( out ) );
	encoder.writeObject(config);
	encoder.close();
}
 
開發者ID:BiglySoftware,項目名稱:BiglyBT,代碼行數:20,代碼來源:UserManagerXMLPersist.java

示例11: objectToXml

import java.beans.XMLEncoder; //導入依賴的package包/類
public static byte[] objectToXml(Object obj,ClassLoader cl){
    if(cl == null){ return objectToXml(obj); }
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    XMLEncoder xml = new XMLEncoder(bos);
    //HACK
    ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
    if (cl != oldLoader && cl != null){
        try{
            Thread.currentThread().setContextClassLoader(cl);
            xml.writeObject(obj);
        }catch(Exception e){e.printStackTrace();}
        finally {
            Thread.currentThread().setContextClassLoader(oldLoader);
        }
    }else{
        xml.writeObject(obj);
    }

    xml.close();
    return bos.toByteArray();
}
 
開發者ID:asiermarzo,項目名稱:Ultraino,代碼行數:22,代碼來源:FileUtils.java

示例12: toXml

import java.beans.XMLEncoder; //導入依賴的package包/類
/**
 * To xml.
 *
 * @param obj
 *            the obj
 * @return the string
 */
// ////////////////////////////////////////////////////////////////////
public static String toXml(final Object obj) {
	final ByteArrayOutputStream out = new ByteArrayOutputStream();

	// XStream x = createXStream();
	// String xml = x.toXML(obj);
	// return xml;
	final XMLEncoder e = new XMLEncoder(out);
	e.setExceptionListener(new XmlEncoderExceptionListener());
	// e.setPersistenceDelegate(Object.class, new MyPersistenceDelegate());
	e.writeObject(obj);
	e.close();
	return out.toString();
	// return null;
}
 
開發者ID:kiswanij,項目名稱:jk-util,代碼行數:23,代碼來源:JKObjectUtil.java

示例13: saveWorkspaceToFile

import java.beans.XMLEncoder; //導入依賴的package包/類
private void saveWorkspaceToFile(File file) {

		try {
			FileOutputStream fos = new FileOutputStream(file);
			XMLEncoder encoder = new XMLEncoder(fos);
			encoder.writeObject(config);
			encoder.close();
			recentMenu.addRecentWorkspace(file);
		} catch (FileNotFoundException e) {
			GUIHelper.showMessage(this, "An error occured while saving to "
					+ file.getPath(), JOptionPane.ERROR_MESSAGE);
		}
		GUIHelper.showMessage(this, "Workspace saved to " + file.getPath(),
				JOptionPane.INFORMATION_MESSAGE);
		actionSaveWorkspaceAs.setEnabled(true);
	}
 
開發者ID:linchaolong,項目名稱:ApkToolPlus,代碼行數:17,代碼來源:BrowserMDIFrame.java

示例14: save

import java.beans.XMLEncoder; //導入依賴的package包/類
public static void save(File fout, SkeletonBean obj) throws IOException
   {
FileOutputStream fos = new FileOutputStream(fout);
try {
    XMLEncoder enc = new XMLEncoder(fos);
    enc.writeObject(obj);
    enc.close();
} catch (Exception ex)
    {
	throw new IOException(ex.toString());
    }
finally
    {
	fos.close();
    }
   }
 
開發者ID:BisonSchweizAG,項目名稱:JSmooth,代碼行數:17,代碼來源:SkeletonPersistency.java

示例15: setupXML

import java.beans.XMLEncoder; //導入依賴的package包/類
/**
 * Configure the serialization of the File.
 */
@Override
protected void setupXML(XMLEncoder enc) {
	super.setupXML(enc);
	
	PropertyDescriptor[] pdArr;
	try {
		pdArr = Introspector.getBeanInfo(VirtualFileSystemFile.class)
				.getPropertyDescriptors();
	} catch (IntrospectionException e) {
		logger.error("I don't know what to do here", e);
		throw new RuntimeException(e);
	}
	for (PropertyDescriptor pd : pdArr) {
		// logger.debug("PropertyDescriptor - VirtualFileSystemFile - "
		// + pd.getDisplayName());
		if (transientListFile.contains(pd.getName())) {
			pd.setValue("transient", Boolean.TRUE);
		}
	}
	// needed to create a VFSFile object during unserialization
	enc.setPersistenceDelegate(VirtualFileSystemFile.class,
			new DefaultPersistenceDelegate(new String[] { "username",
					"group", "size", "slaves" }));
}
 
開發者ID:drftpd-ng,項目名稱:drftpd3,代碼行數:28,代碼來源:VirtualFileSystemFile.java


注:本文中的java.beans.XMLEncoder類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。