Example usage for javax.xml.bind Marshaller marshal

List of usage examples for javax.xml.bind Marshaller marshal

Introduction

In this page you can find the example usage for javax.xml.bind Marshaller marshal.

Prototype

public void marshal(Object jaxbElement, javax.xml.stream.XMLEventWriter writer) throws JAXBException;

Source Link

Document

Marshal the content tree rooted at jaxbElement into a javax.xml.stream.XMLEventWriter .

Usage

From source file:Main.java

public static String beanToXml(ByteArrayOutputStream out, Object to) {
    try {/*from  w  ww . ja  v a  2  s  .c o m*/
        JAXBContext context = JAXBContext.newInstance(to.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.marshal(to, out);
        return new String(out.toByteArray(), "UTF-8");
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String convertToXml(Object source, Class... type) {
    String result;//from  ww  w  . ja  v a 2 s  .c  o m
    StringWriter sw = new StringWriter();
    try {
        JAXBContext context = JAXBContext.newInstance(type);
        Marshaller marshaller = context.createMarshaller();
        marshaller.marshal(source, sw);
        result = sw.toString();
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }

    return result;
}

From source file:Main.java

public static String parseObjectToXml(Object obj) {
    if (obj == null) {
        return NO_RESULT;
    }//from   w w  w. j  a v  a  2s .co  m
    StringWriter sw = new StringWriter();
    try {
        JAXBContext context = JAXBContext.newInstance(obj.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.marshal(obj, sw);
        return sw.toString();
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return NO_RESULT;
}

From source file:Main.java

public static String bean2XML(Object obj) throws Exception {
    JAXBContext context = JAXBContext.newInstance(obj.getClass());
    Marshaller m = context.createMarshaller();
    StringWriter sw = new StringWriter();
    m.marshal(obj, sw);
    return sw.toString();
}

From source file:Main.java

public static String toString(Object entity, boolean formatOutput) {

    StringWriter stringWriter = new StringWriter();

    try {/*from   ww w .  j av a 2s .  co m*/

        Map<String, Object> properties = new HashMap<String, Object>();

        properties.put(Marshaller.JAXB_FORMATTED_OUTPUT, formatOutput);

        JAXBContext jaxbContext = JAXBContext.newInstance(entity.getClass());

        Marshaller marshaller = jaxbContext.createMarshaller();

        marshaller.marshal(entity, stringWriter);

    } catch (JAXBException e) {
        stringWriter.write(e.getMessage());
    }
    return stringWriter.toString();
}

From source file:Main.java

public static void marshall(Object object, Node root, Marshaller.Listener listener) throws JAXBException {
    Marshaller marshaller = createMarshaller(object.getClass(), listener);
    marshaller.marshal(object, root);
}

From source file:Main.java

public static void marshall(Object object, OutputStream os, Marshaller.Listener listener) throws JAXBException {
    Marshaller marshaller = createMarshaller(object.getClass(), listener);
    marshaller.marshal(object, os);
}

From source file:Main.java

public static void marshall(Object object, Result result, Marshaller.Listener listener) throws JAXBException {
    Marshaller marshaller = createMarshaller(object.getClass(), listener);
    marshaller.marshal(object, result);
}

From source file:Main.java

public static void marshall(Object object, Writer writer, Marshaller.Listener listener) throws JAXBException {
    Marshaller marshaller = createMarshaller(object.getClass(), listener);
    marshaller.marshal(object, writer);
}

From source file:Main.java

public static Document toXml(Object o) throws Exception {
    Marshaller jaxbMarshaller = createMarshaller(o);
    Document d = createDocument();
    jaxbMarshaller.marshal(o, d);
    return d;//from ww w . j a  v  a 2 s  .  c o  m
}