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

/**
 * @since 2.4/*  w  w  w .  j  av a2  s .  c  om*/
 */
public static Marshaller createMarshaller(final Object object) throws JAXBException {
    JAXBContext context = createContext(object.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    return marshaller;
}

From source file:Main.java

public static <T> String toString(T xml) {
    JAXBContext jc;
    try {//  www .  j  av a  2 s  . c  o  m
        jc = JAXBContext.newInstance(xml.getClass());
        Marshaller m = jc.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        StringWriter writer = new StringWriter();
        m.marshal(xml, writer);
        return writer.toString();
    } catch (JAXBException e) {
        return "";
    }
}

From source file:Main.java

public static void marshal(Object o, OutputStream os, Map<String, Object> properties) {
    try {/*ww  w .  j  ava 2 s . c o  m*/
        JAXBContext ctx = JAXBContext.newInstance(o.getClass());
        Marshaller marshaller = ctx.createMarshaller();

        for (Entry<String, Object> p : properties.entrySet()) {
            marshaller.setProperty(p.getKey(), p.getValue());
        }

        marshaller.marshal(o, os);
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static void marshal(JAXBContext jaxbContext, Object item, XMLStreamWriter xmlWriter)
        throws JAXBException {
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.marshal(item, xmlWriter);
}

From source file:Main.java

/**
 * Prints the document for debug.//  w  ww  .  jav a2 s . c om
 *
 * @param model the model to be printed
 * @throws Exception for any errors encountered
 */
public static void printModel(Object model) throws Exception {
    JAXBContext jaxbContext = JAXBContext.newInstance("gov.medicaid.domain.model");
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.marshal(model, System.out);
}

From source file:Main.java

/**
 * Convert a bean to XML format using JAXB.
 * /*from  w  ww  .j  a  va2s .c  om*/
 * @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, new Boolean(true));
        marshaller.marshal(bean, baos);
    } catch (JAXBException e) {
        throw new IllegalStateException("Error marshalling", e);
    }
    return new String(baos.toByteArray());
}

From source file:Main.java

/**
 * Convert a bean to XML format using JAXB.
 * /*from ww  w. j a v a 2 s .c om*/
 * @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());
}

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

/**
 * Serializes any Object to XML/*from  www .j  av  a2  s.  c om*/
 *
 * @param object Object to serialize
 * @return XML serialization of the supplied object
 */
public static String toXmlJaxb(Object object) {
    String result = "";
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
        Marshaller marshaller = jaxbContext.createMarshaller();
        StringWriter writer = new StringWriter();
        marshaller.marshal(object, writer);
        result = writer.toString();
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
    return result;
}

From source file:Main.java

public static String toXml(Class className, Object object) {
    String strXml = "";
    StringWriter writer = null;/*from   ww  w . j  a  v  a  2s .  c  om*/
    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("&lt;", "<");
        strXml = strXml.replace("&gt;", ">");
    } catch (Exception e) {
    } finally {
        if (writer != null) {
            try {
                writer.close();
                writer = null;
            } catch (Exception e) {
            }
        }
    }
    return strXml;
}