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> 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 <T extends Object> void marshal(Class clz, T marshalObj) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(clz);
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.marshal(marshalObj, System.out);

}

From source file:Main.java

public static byte[] serializerBytes(Object xmlObj) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(xmlObj.getClass());
    Marshaller marshaller = context.createMarshaller();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    marshaller.marshal(xmlObj, baos);//www  .  j  ava  2 s  .  co  m
    return baos.toByteArray();
}

From source file:Main.java

public static void pojoToXml(Object o, String arquivo) throws JAXBException {
    File file = new File(arquivo);
    JAXBContext jaxbContext = JAXBContext.newInstance(o.getClass());
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    jaxbMarshaller.marshal(o, file);/*from  www. jav  a  2  s . co  m*/
}

From source file:Main.java

public static String object2Xml(Object obj) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(obj.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

    StringWriter writer = new StringWriter();
    marshaller.marshal(new JAXBElement(new QName("xml"), obj.getClass(), obj), writer);

    return writer.toString();
}

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   www  .  j av a2 s . c o  m*/
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> String marshal(T object) throws IOException, JAXBException {
    Class<T> clzz = (Class<T>) object.getClass();
    JAXBContext context;
    context = JAXBContext.newInstance(clzz);
    Marshaller m = context.createMarshaller();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(object, os);/* www. ja  v a  2s  .  co  m*/
    return os.toString();
}

From source file:Main.java

public static String convertToXml(Object source, Class... type) {
    String result;//from  w  w  w  . j  a  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

/**
 * Parse XML using JAXB and a model class.
 * //from ww  w.  j  av a  2s .c o  m
 * @param in an input stream
 * @return the requested object
 * @param cls the root class
 * @throws JAXBException thrown on an error
 */
@SuppressWarnings("unchecked")
public static <T> T parseJaxb(Class<T> cls, InputStream in) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(cls);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    Unmarshaller um = context.createUnmarshaller();
    return (T) um.unmarshal(in);
}

From source file:Main.java

public static <T> String toXml(T object) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    StringWriter writer = new StringWriter();
    marshaller.marshal(object, writer);//from   w  ww.j  a v  a2s  .  c o m
    return writer.toString();
}