List of usage examples for java.beans XMLEncoder writeObject
public void writeObject(Object o)
From source file:Main.java
/** * Serializes an object into an xml file * // ww w . jav a 2 s .c om * @param object object to serialize * @param fileName path to file */ public static void encodeToFile(Object object, String fileName) throws FileNotFoundException, IOException { XMLEncoder encoder = new XMLEncoder(new FileOutputStream(fileName)); try { encoder.writeObject(object); encoder.flush(); } finally { encoder.close(); } }
From source file:net.sourceforge.fenixedu.util.tests.ResponseExternalization.java
public static String externalize(Response source) { ByteArrayOutputStream out = new ByteArrayOutputStream(); XMLEncoder encoder = new XMLEncoder(out); encoder.writeObject(source); encoder.close();// w w w.j a v a 2s. c om try { // I think that this is wrong and that we should get the // bytes of the ByteArrayOutputStream interpreted as // UTF-8, which is what the XMLEncoder produces in the // first place. // WARNING: If this is changed, the internalize method // should be changed accordingly. return out.toString(Charset.defaultCharset().name()); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block logger.error(e.getMessage(), e); return out.toString(); } }
From source file:fi.vm.sade.log.client.LoggerJms.java
/** * Encode messages to string (xml).//from w w w . j a va 2 s .com * * @param event * @return */ public static String encode(LogEvent event) { if (event == null) { return null; } ByteArrayOutputStream baos = new ByteArrayOutputStream(); XMLEncoder xmlEncoder = new XMLEncoder(baos); xmlEncoder.writeObject(event); xmlEncoder.close(); return baos.toString(); }
From source file:Main.java
/** * Writes an object to an XML file.//from w w w.j a v a 2 s. com * * @param bean the bean * @param filename the filename * * @throws IOException Signals that an I/O exception has occurred. */ public static void writeBeanToFile(Object bean, String filename) throws IOException { XMLEncoder encoder = null; try { encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(filename))); encoder.writeObject(bean); } finally { if (encoder != null) { encoder.close(); } } }
From source file:com.npower.dl.FirmwareMimeTypeHelper.java
public static void save(File file) throws IOException { FileOutputStream out = new FileOutputStream(file); XMLEncoder xmlEncoder = new XMLEncoder(out); xmlEncoder.writeObject(mimeTypes); xmlEncoder.flush();/* www. j a va 2 s.c o m*/ xmlEncoder.close(); out.close(); }
From source file:Main.java
/** * Creates from the given Object an xml string. * /*from w w w . jav a 2s . co m*/ * @param <T> * the generic type of the return type * @param obj * the obj to transform to an xml string. * @return the xml string */ public static <T> String toXmlWithXMLEncoder(final T obj) { XMLEncoder enc = null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { enc = new XMLEncoder(baos); enc.writeObject(obj); enc.close(); enc = null; } finally { if (enc != null) { enc.close(); } } return baos.toString(); }
From source file:Main.java
public static void writeBeanToXml(String path, Object bean) { XMLEncoder encoder = null; try {/*from www.ja va 2 s . co m*/ // Serialize object into XML encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(path))); encoder.writeObject(bean); encoder.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (encoder != null) { encoder.close(); } } }
From source file:Main.java
/** * Creates from the given Object an xml string. * /* w w w . java 2 s . c o m*/ * @param <T> * the generic type of the return type * @param obj * the obj to transform to an xml string. * @return the xml string */ public static <T> String toXmlWithXMLEncoder(final T obj) { XMLEncoder enc = null; final ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { enc = new XMLEncoder(baos); enc.writeObject(obj); enc.close(); enc = null; } finally { if (enc != null) { enc.close(); } } return baos.toString(); }
From source file:Main.java
public static byte[] encodeObject(Object object, final boolean noisy) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); XMLEncoder encoder = new XMLEncoder(outputStream); encoder.setExceptionListener(new ExceptionListener() { public void exceptionThrown(Exception ex) { if (noisy) ex.printStackTrace();// w ww .ja v a2 s .com } }); encoder.writeObject(object); encoder.close(); if (noisy) System.out.println(outputStream.toString()); return outputStream.toByteArray(); }
From source file:Main.java
public static String parseObj2Xml(Object object) throws Exception { XMLEncoder encoder = null; String str = null;// w ww . j av a 2 s . c om BufferedOutputStream bufOut = null; ByteArrayOutputStream out = null; try { out = new ByteArrayOutputStream(); bufOut = new BufferedOutputStream(out); encoder = new XMLEncoder(bufOut); encoder.writeObject(object); encoder.close(); str = new String(out.toByteArray()); } catch (Exception ex) { throw ex; } finally { if (out != null) out.close(); if (bufOut != null) bufOut.close(); } return str; }