本文整理汇总了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);
}
示例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());
}
示例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();
}
示例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);
}
}
示例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();
}
示例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();
}
}
示例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");
}
示例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");
}
示例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);
}
示例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();
}
示例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();
}
示例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;
}
示例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);
}
示例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();
}
}
示例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" }));
}