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:gov.hhs.fha.nhinc.lift.proxy.util.ProxyUtil.java
public static String marshalToString(Object obj) throws JAXBException { StringWriter writer = new StringWriter(); JAXBContext jc = JAXBContext.newInstance(obj.getClass()); Marshaller marshaller = jc.createMarshaller(); marshaller.marshal(obj, writer); log.info("Marshal: " + writer.toString()); return writer.toString(); }
From source file:Main.java
/** * Marshals a bean to XML.//from w w w .j a v a2 s . c o m * * @param bean * @param namespaceURI * @param localPart * @return XML {@link String} * @throws JAXBException */ @SuppressWarnings("unchecked") public static <T> String marshal(T bean, String namespaceURI, String localPart) throws JAXBException { QName qName = new QName(namespaceURI, localPart); Class<T> clazz = (Class<T>) bean.getClass(); Marshaller marshaller = createMarshaller(clazz); Writer stw = new StringWriter(); marshaller.marshal(new JAXBElement<T>(qName, clazz, bean), stw); return stw.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); return new String(writer.getBuffer()); }
From source file:Main.java
public static String objectToXML(Class clazz, Object object) throws JAXBException { String xml = null;/*w ww .j a v a 2s. co m*/ JAXBContext context = JAXBContext.newInstance(clazz); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); Writer w = new StringWriter(); m.marshal(object, w); xml = w.toString(); return xml; }
From source file:Main.java
public static <T> void serialize(T object, OutputStream out) throws JAXBException { JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass()); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jaxbMarshaller.marshal(object, out); }
From source file:Main.java
public static void saveInstance(File outputFile, Object instance) throws JAXBException, IOException { Marshaller marshaller = JAXBContext.newInstance(instance.getClass()).createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.marshal(instance, outputFile); }
From source file:Main.java
public static void marshal(Object obj, OutputStream out, Class... boundClasses) { try {//from www. j a v a 2s . c o m Marshaller m = JAXBContext.newInstance(boundClasses).createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); m.marshal(obj, out); } catch (Exception ex) { } }
From source file:Main.java
public static String toXml(Class className, Object object) { String strXml = ""; StringWriter writer = null;//from w ww.j a v a2 s . c o m try { writer = new StringWriter(); JAXBContext context = JAXBContext.newInstance(className); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(object, writer); strXml = writer.toString(); writer.flush(); strXml = strXml.replace("<", "<"); strXml = strXml.replace(">", ">"); } catch (Exception e) { } finally { if (writer != null) { try { writer.close(); writer = null; } catch (Exception e) { } } } return strXml; }
From source file:Main.java
public static String toXml(Object object) { final StringWriter out = new StringWriter(); JAXBContext context = null;/* w ww.jav a 2 s. c o m*/ try { context = JAXBContext.newInstance(object.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.marshal(object, new StreamResult(out)); } catch (PropertyException e) { e.printStackTrace(); } catch (JAXBException e) { e.printStackTrace(); } return out.toString(); }
From source file:Main.java
/** * Convert a bean to XML format using JAXB. * /* w w w .jav a 2 s . co m*/ * @param bean * The bean to marshal as XML. * @param bc * Additional classes to register on the JAXB context for * marshalling. * @return An XML representation of the bean. */ public static <T> String marshal(T bean, Class<?>... bc) { assert bean != null; Class<?>[] bind; if (bc.length > 0) { bind = new Class<?>[bc.length + 1]; bind[0] = bean.getClass(); for (int i = 0; i < bc.length; i++) bind[i + 1] = bc[i]; } else bind = new Class<?>[] { bean.getClass(), }; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { JAXBContext jaxbContext = JAXBContext.newInstance(bind); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(bean, baos); } catch (JAXBException e) { throw new IllegalStateException("Error marshalling", e); } return new String(baos.toByteArray()); }