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 toXml(Class<T> z, Object o) {
    try {/*from   ww w. j  ava2 s .c om*/
        JAXBContext jc = JAXBContext.newInstance(z);
        Marshaller ms = jc.createMarshaller();
        ms.marshal(o, System.out);
    } catch (JAXBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static <T> void marshal(JAXBElement<T> element, Class<T> c, OutputStream os) {
    try {//from   w w w  .  ja  v  a 2s.  com
        JAXBContext jc = JAXBContext.newInstance(c);
        Marshaller ma = jc.createMarshaller();
        ma.marshal(element, os);
    } catch (JAXBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:Main.java

public static <T> void marshall(String file, JAXBElement<T> object, Class context) throws Exception {
    JAXBContext ctx = JAXBContext.newInstance(context);
    Marshaller marshaller = ctx.createMarshaller();
    marshaller.marshal(object, new File(file));
}

From source file:Main.java

public static void parseBeanToXmlFileByJAXB(String path, Object bean, Class clase) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(clase);
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    JAXBElement<Object> rootElement = new JAXBElement<Object>(new QName(clase.getSimpleName()), clase, bean);
    marshaller.marshal(rootElement, new FileOutputStream(path));
}

From source file:Main.java

public static String objectToXML(Class clazz, Object object) throws JAXBException {
    String xml = null;/*from   ww  w . jav a 2  s .  c  o  m*/
    JAXBContext context = JAXBContext.newInstance(clazz);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    Writer w = new StringWriter();
    m.marshal(object, w);
    xml = w.toString();
    return xml;
}

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);//from   w ww.j ava  2s  . c o  m
    return sw.toString();
}

From source file:Main.java

private static <T> Marshaller createMarshaller(Class<T> clazz, Marshaller.Listener listener)
        throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(clazz);
    Marshaller marshaller = jc.createMarshaller();
    if (listener != null) {
        marshaller.setListener(listener);
    }//from w  w  w. ja va2s  . co  m
    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    return marshaller;
}

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);//from  ww  w .  j a v  a2s .c o  m
    return f;
}

From source file:Main.java

/**
 * @param <T>//from   w w w  .  j  a v  a  2s  . c o m
 *           the type to serialize
 * @param c
 *           the class of the type to serialize
 * @param o
 *           the instance containing the data to serialize
 * @return a string representation of the data.
 * @throws Exception
 */
@SuppressWarnings("unchecked")
public static <T> String marshal(Class<T> c, Object o) throws Exception {
    JAXBContext ctx = JAXBContext.newInstance(c);
    Marshaller marshaller = ctx.createMarshaller();
    StringWriter entityXml = new StringWriter();
    marshaller.marshal(o, entityXml);
    String entityString = entityXml.toString();
    return entityString;
}

From source file:Main.java

public static Marshaller getMarshaller(Class jaxbClass) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(jaxbClass);
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
    return marshaller;
}