Java examples for XML:JAXB
marshal Object To File using JAXB
//package com.java2s; import java.io.ByteArrayOutputStream; import java.io.File; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamWriter; public class Main { public static void main(String[] argv) throws Exception { Object objJAXB = "java2s.com"; String fullFileName = "java2s.com"; marshalToFile(objJAXB, fullFileName); }/*from w w w . j a v a 2 s . c o m*/ private static void marshalToFile(Object objJAXB, String fullFileName) throws Exception { newMarshaller(objJAXB.getClass()).marshal(objJAXB, new File(fullFileName)); } private static String marshal(Object objJAXB) throws Exception { ByteArrayOutputStream result = new ByteArrayOutputStream(); newMarshaller(objJAXB.getClass()).marshal(objJAXB, newWriter(result)); return result.toString(); } private static Marshaller newMarshaller(Class<?> classJAXB) throws Exception { return newContext(classJAXB).createMarshaller(); } private static XMLStreamWriter newWriter(ByteArrayOutputStream result) throws Exception { XMLOutputFactory output = XMLOutputFactory.newInstance(); XMLStreamWriter writer = output.createXMLStreamWriter(result); return writer; } private static JAXBContext newContext(Class<?> classJAXB) throws Exception { return JAXBContext.newInstance(classJAXB); } }