List of usage examples for javax.xml.bind JAXBContext createMarshaller
public abstract Marshaller createMarshaller() throws JAXBException;
From source file:Main.java
public static String toXML(Object obj) { try {//from w ww . ja v a2 s .com JAXBContext context = JAXBContext.newInstance(obj.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); StringWriter writer = new StringWriter(); marshaller.marshal(obj, writer); return writer.toString(); } catch (JAXBException e) { e.printStackTrace(); return ""; } }
From source file:Main.java
public static String parseBeanToXmlStringByJAXB(Object bean, Class clase) throws Exception { JAXBContext jc = JAXBContext.newInstance(clase); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false); JAXBElement<Object> rootElement = new JAXBElement<Object>(new QName(clase.getSimpleName()), clase, bean); OutputStream output = new OutputStream() { private StringBuilder string = new StringBuilder(); public void write(int b) throws IOException { this.string.append((char) b); }/* w ww . j a v a2s . c o m*/ //Netbeans IDE automatically overrides this toString() public String toString() { return this.string.toString(); } }; marshaller.marshal(rootElement, output); return output.toString(); }
From source file:Main.java
public static void marshal(Object model, OutputStream output) throws JAXBException { JAXBContext jaxbContext = JAXBContext.newInstance(model.getClass()); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); jaxbMarshaller.marshal(model, output); }
From source file:Main.java
public static String obj2Xml(Object obj, String encoding) { String result = null;/*from ww w. j av a 2s .c o m*/ try { JAXBContext context = JAXBContext.newInstance(obj.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding); StringWriter writer = new StringWriter(); marshaller.marshal(obj, writer); result = writer.toString(); } catch (Exception e) { e.printStackTrace(); } return result; }
From source file:Main.java
/** * /*from w w w . j a v a 2 s. c om*/ * @param contextPath * @return Marshaller * @throws JAXBException */ public static Marshaller getMarshaller(final String contextPath) throws JAXBException { final JAXBContext jaxbContext = JAXBContext.newInstance(contextPath); final Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); return marshaller; }
From source file:Main.java
/** * Helper method to serialize an object to XML. Requires object to be Serializable * @param entity Object to serialize// w w w .j ava 2s. c om * @param <T> class that implements Serializable * @return String XML representation of object * @throws IOException if errors during serialization * @throws JAXBException if errors during serialization */ public static <T extends Serializable> String getXml(T entity) throws IOException, JAXBException { StringWriter sw = new StringWriter(); if (null != entity) { JAXBContext ctx = JAXBContext.newInstance(entity.getClass()); Marshaller m = ctx.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.marshal(entity, sw); } sw.flush(); sw.close(); return sw.toString(); }
From source file:Main.java
public static final String obj2xml(final Object obj, final boolean formatted) throws JAXBException { StringWriter writer = new StringWriter(); JAXBContext contextOut = JAXBContext.newInstance(obj.getClass()); Marshaller marshallerOut = contextOut.createMarshaller(); marshallerOut.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, formatted); marshallerOut.marshal(obj, writer);/*ww w. j av a 2 s. c om*/ return new String(writer.getBuffer()); }
From source file:Main.java
public static String convertToXml(Object obj, String encoding) { String result = null;/* w w w . j a v a2 s . c om*/ try { JAXBContext context = JAXBContext.newInstance(obj.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding); StringWriter writer = new StringWriter(); marshaller.marshal(obj, writer); result = writer.toString(); } catch (Exception e) { e.printStackTrace(); } return result; }
From source file:Main.java
public static <T> String print(T xml, 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); StringWriter writer = new StringWriter(); marshaller.marshal(xml, writer);//from w w w . j av a2 s. c om return writer.toString(); }
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(); }