Example usage for javax.xml.bind JAXBContext createMarshaller

List of usage examples for javax.xml.bind JAXBContext createMarshaller

Introduction

In this page you can find the example usage for javax.xml.bind JAXBContext createMarshaller.

Prototype

public abstract Marshaller createMarshaller() throws JAXBException;

Source Link

Document

Create a Marshaller object that can be used to convert a java content tree into XML data.

Usage

From source file:Main.java

public static <T> String toXmlString(T obj, Class<T> type) {
    try {/*from   w w  w . j av  a2  s  .  c  o  m*/
        JAXBContext jaxbContext = JAXBContext.newInstance(type);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        jaxbContext.createMarshaller().marshal(obj, byteArrayOutputStream);
        return byteArrayOutputStream.toString();
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String toXml(Object object) {
    final StringWriter out = new StringWriter();
    JAXBContext context = null;
    try {/*  w  w w  .  ja va  2s  .co m*/
        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

/**
 * @param obj/*w  w w .j  a  v a  2  s  .  c  o m*/
 * @param clazz
 * @param node
 * @throws JAXBException
 * @throws IOException
 */
// @SuppressWarnings("unused")
public static void exportXmlJAXB(Object obj, Class<?>[] clazz, Node node) throws JAXBException, IOException {
    JAXBContext jc = getJAXBContext(clazz);
    Marshaller m = jc.createMarshaller();
    setMarshallerProperty(m).marshal(obj, node);
}

From source file:Main.java

public static void exportXmlJAXB(Object obj, Class<?>[] clazz, OutputStream out)
        throws JAXBException, IOException {
    JAXBContext jc = getJAXBContext(clazz);
    Marshaller m = jc.createMarshaller();
    // XMLWriter writer = new XMLWriter(new OutputWriter(out));
    setMarshallerProperty(m).marshal(obj, out);
}

From source file:Main.java

public static <T> void xmlWriter(T entity, String xmlDestinationPath) {
    try {/*from   w w  w .  j  a  va2  s  .  c  o m*/
        JAXBContext jc = JAXBContext.newInstance(entity.getClass());
        File destinationFile = new File(xmlDestinationPath);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(entity, destinationFile);
    } catch (JAXBException ex) {
    }
}

From source file:Main.java

public static Marshaller createMarshaller(String pack, Schema schema) {
    JAXBContext jaxbContext = null;
    try {//from   w  ww  .j  a v a2s  .  c  om
        jaxbContext = JAXBContext.newInstance(pack);
        Marshaller marsh = jaxbContext.createMarshaller();

        if (schema != null) {
            marsh.setSchema(schema);
            //                marsh.setEventHandler( new DefaultValidationEventHandler() {
            //                    @Override
            //                    public boolean handleEvent( ValidationEvent event ) {
            //                        return super.handleEvent( event );
            //                    }
            //                });
        }
        marsh.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        marsh.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        return marsh;
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * Object to XML//  ww w.jav  a  2 s  . c o  m
 * 
 * @param object
 * @return
 */
public static String convertToXML(Object object) {
    try {
        System.out.println(mMap.containsKey(object.getClass()) + "---mmap-----");
        if (!mMap.containsKey(object.getClass())) {
            JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            // marshaller.setProperty(CharacterEscapeHandler.class.getName(),
            // new CharacterEscapeHandler() {
            // public void escape(char[] ac, int i, int j, boolean
            // flag,Writer writer) throws IOException {
            // writer.write( ac, i, j ); }
            // });
            mMap.put(object.getClass(), marshaller);
        }
        System.out.println("----mmap--" + mMap.toString());
        StringWriter stringWriter = new StringWriter();
        mMap.get(object.getClass()).marshal(object, stringWriter);
        return stringWriter.getBuffer().toString();
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * Marshal a JAXB element to a XML DOM document
 *
 * @param jaxbElement// w w  w  .j a  v  a 2 s .com
 *            The root of content tree to be marshalled
 * @return XML DOM document
 * @throws Exception
 *             in error case
 */
public static Document doMarshallingJAXBObject(Object jaxbElement) throws Exception {
    if (jaxbElement == null) {
        throw new RuntimeException("No JAXB element to marshal (null)!");
    }
    Document doc = null;
    try {
        JAXBContext jaxbCtx = JAXBContext.newInstance(jaxbElement.getClass().getPackage().getName());
        Marshaller marshaller = jaxbCtx.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); // NOI18N
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        doc = db.newDocument();
        marshaller.marshal(jaxbElement, doc);
        doc.getDocumentElement().normalize();
    } catch (Exception e) {
        //            Logger.XMLEval.logState("Marshalling failed: " + e.getMessage(), LogLevel.Error);
        throw e;
    }

    return doc;
}

From source file:Main.java

/**
 * Marshal a JAXB element to a XML file/*from   ww w.  j ava  2 s  .  c om*/
 *
 * @param jaxbElement
 *            The root of content tree to be marshalled
 * @param strXMLFilePath
 *            XML output file path
 * @throws Exception
 *             in error case
 */
public static void doMarshalling(Object jaxbElement, String strXMLFilePath) throws Exception {
    if (jaxbElement == null) {
        throw new RuntimeException("No JAXB element to marshal (null)!");
    }
    if (strXMLFilePath == null) {
        throw new RuntimeException("No XML file path (null)!");
    }
    FileOutputStream fos = null;
    try {
        JAXBContext jaxbCtx = JAXBContext.newInstance(jaxbElement.getClass().getPackage().getName());
        Marshaller marshaller = jaxbCtx.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); // NOI18N
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        fos = new FileOutputStream(strXMLFilePath);
        marshaller.marshal(jaxbElement, fos);// System.out);
    } catch (Exception e) {
        //            Logger.XMLEval.logState("Marshalling failed: " + e.getMessage(), LogLevel.Error);
        throw e;
    } finally {
        if (fos != null) {
            fos.close();
            fos = null;
        }
    }
}

From source file:de.hybris.platform.b2b.punchout.PunchOutUtils.java

public static String marshallFromBeanTree(final CXML cxml) {
    final StringWriter writer = new StringWriter();

    try {/*  ww w .java  2 s  .  c o m*/
        final JAXBContext context = JAXBContext.newInstance(CXML.class);
        final Marshaller m = context.createMarshaller();
        removeStandalone(m);
        setHeader(m);
        m.marshal(cxml, writer);
    } catch (final JAXBException e) {
        throw new PunchOutException(e.getErrorCode(), e.getMessage());
    }

    final String xml = writer.toString();

    return xml;
}