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 saveObjectToXmlString(Object obj) throws JAXBException {
    final Marshaller m = JAXBContext.newInstance(obj.getClass()).createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    final StringWriter writer = new StringWriter();
    m.marshal(obj, writer);

    return writer.toString();
}

From source file:Main.java

public static OutputStream serializerOutputStream(Object xmlObj) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(xmlObj.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    marshaller.marshal(xmlObj, baos);
    return baos;/*from w  w  w  . jav  a2  s  .  c  o  m*/
}

From source file:Main.java

public static <T> void writeXml(Class<T> _class, T jaxbElement, OutputStream outputStream, String encoding)
        throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(_class);
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);

    marshaller.marshal(jaxbElement, outputStream);
}

From source file:Main.java

public static String objectToXml(Object object) {
    if (null == object) {
        return NO_RESULT;
    }//from   ww  w .j  a va 2s  .c om

    StringWriter sw = new StringWriter();
    JAXBContext jAXBContent = null;
    Marshaller marshaller = null;

    try {
        jAXBContent = JAXBContext.newInstance(object.getClass());
        marshaller = jAXBContent.createMarshaller();
        marshaller.marshal(object, sw);

        return sw.toString();
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return NO_RESULT;
}

From source file:Main.java

public static <T> void JAXBMarshalling(T object, File output) throws JAXBException {
    JAXBContext jAXBContext = JAXBContext.newInstance(object.getClass());
    Marshaller marshaller = jAXBContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    marshaller.marshal(object, output);
}

From source file:org.vertx.java.http.eventbusbridge.util.SerializationHelper.java

private static String toXml(final Object xmlObject) throws JAXBException {
    StringWriter writer = new StringWriter();
    Marshaller jaxbMarshaller = JAXB_CONTEXT.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    jaxbMarshaller.marshal(xmlObject, writer);
    return writer.toString();
}

From source file:Main.java

public static String marshallToStringNoValidation(final Object xmlObject) throws JAXBException {
    StringWriter writer = new StringWriter();
    JAXBContext context = JAXBContext.newInstance(xmlObject.getClass());
    Marshaller jaxbMarshaller = context.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    jaxbMarshaller.marshal(xmlObject, writer);
    return writer.toString();
}

From source file:Main.java

public static File Marshall(Object object, String filePath) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(object.getClass());
    Marshaller u = jc.createMarshaller();
    u.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    u.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    File f = new File(filePath);
    u.marshal(object, f);
    return f;/*from  w w  w .  j  ava  2s  .c  om*/
}

From source file:Main.java

@SuppressWarnings("rawtypes")
public static String ObjToXml(Object object, boolean isXmlFormat, Class... classesToBeBound)
        throws JAXBException, IOException {
    JAXBContext context = JAXBContext.newInstance(classesToBeBound);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, isXmlFormat);
    m.setSchema(null);//from   w w w .  j  a  v  a  2s.  co  m
    StringWriter sw = new StringWriter();
    m.marshal(object, sw);
    String result = sw.toString();
    sw.close();
    return result;
}

From source file:Main.java

public static <T extends Object> String marshalAsString(Class clz, T marshalObj) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(clz);
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    StringWriter writer = new StringWriter();
    marshaller.marshal(marshalObj, new BufferedWriter(writer));
    return writer.toString();

}