List of usage examples for javax.xml.bind Marshaller marshal
public void marshal(Object jaxbElement, javax.xml.stream.XMLEventWriter writer) throws JAXBException;
From source file:Main.java
public static String objectToXml(JAXBContext jaxbContext, Object object) throws JAXBException { StringWriter writerTo = new StringWriter(); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(object, writerTo); return writerTo.toString(); }
From source file:Main.java
/** * * @param obj//from ww w. j av a 2 s . c o m * @return * @throws JAXBException * @throws ParserConfigurationException */ public static Document jaxbToDocument(Object obj) throws JAXBException, ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder db = factory.newDocumentBuilder(); Document doc = db.newDocument(); // Marshal the Object to a Document final Marshaller m = JAXBContext.newInstance(obj.getClass()).createMarshaller(); m.marshal(obj, doc); return doc; }
From source file:Main.java
public static void writeXML(Class<?> class1, Object obj, File outputFile) throws JAXBException, FileNotFoundException { JAXBContext context = JAXBContext.newInstance(class1); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); m.marshal(obj, outputFile); }
From source file:Main.java
/** * @param <T>//from w ww . ja v a 2s . c om * the type to serialize * @param c * the class of the type to serialize * @param o * the instance containing the data to serialize * @return a string representation of the data. * @throws Exception */ @SuppressWarnings("unchecked") public static <T> String marshal(Class<T> c, Object o) throws Exception { JAXBContext ctx = JAXBContext.newInstance(c); Marshaller marshaller = ctx.createMarshaller(); StringWriter entityXml = new StringWriter(); marshaller.marshal(o, entityXml); String entityString = entityXml.toString(); return entityString; }
From source file:Main.java
public static byte[] serializerBytes(Object xmlObj) throws JAXBException { JAXBContext context = JAXBContext.newInstance(xmlObj.getClass()); Marshaller marshaller = context.createMarshaller(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); marshaller.marshal(xmlObj, baos); return baos.toByteArray(); }
From source file:Main.java
public static void marshall(String cntxtPkg, Object obj, OutputStream out) throws JAXBException, SAXException, IOException { Marshaller marshaller = createMarshall(cntxtPkg); if (marshaller == null) return;/*ww w .j a va 2 s .c o m*/ marshaller.marshal(obj, out); }
From source file:Main.java
public static <T> Element marshal(JAXBElement<T> jaxbElement, Class<T> cls) { try {/* w w w.j a v a 2 s . c o m*/ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); JAXBContext jaxbContext = JAXBContext.newInstance(cls); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.marshal(jaxbElement, doc); return doc.getDocumentElement(); } catch (JAXBException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
From source file:Main.java
public static <T> void save(File file, T obj, Class<?>... clazz) throws JAXBException { //Create JAXB Context JAXBContext jc = JAXBContext.newInstance(clazz); //Create marshaller Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(obj, file); }
From source file:Main.java
public static <T> String asXml(T object) { try {//from w w w . j a v a2s. c o m JAXBContext jaxbContext = null; jaxbContext = JAXBContext.newInstance(object.getClass()); Marshaller marshaller = jaxbContext.createMarshaller(); StringWriter writer = new StringWriter(); marshaller.marshal(object, writer); return writer.toString(); } catch (JAXBException e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static Document deserialize(Object object) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);/*from w ww .j ava 2 s . c o m*/ DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); JAXBContext context = JAXBContext.newInstance(object.getClass()); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.marshal(object, doc); return doc; }