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 <T> void serialize(T object, OutputStream resultStream) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    jaxbMarshaller.marshal(object, resultStream);
}

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;//  w  ww.ja  va  2 s .  c o m
    context = JAXBContext.newInstance(clzz);
    Marshaller m = context.createMarshaller();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(object, os);
    return os.toString();
}

From source file:Main.java

public static <T> void marshal(Object jaxbElement, Class<T> jaxbFactory, OutputStream out)
        throws JAXBException {
    if (!(jaxbElement instanceof JAXBElement<?>)) {
        throw new JAXBException("Must be a instance of JAXBElement<?>");
    }//from ww  w.  j a va  2 s.  co  m
    try {
        JAXBContext jc = JAXBContext.newInstance(jaxbFactory);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(jaxbElement, out);

    } catch (PropertyException e) {
        e.printStackTrace();
    } catch (JAXBException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Saves the object to the file.//from   w ww  .j  av  a2  s.  co m
 *
 * @param <T> the object type.
 * @param path the XML file.
 * @param object the object to be saved.
 */
public static <T> void saveObject(Path path, T object) {

    if (path == null || object == null) {
        throw new RuntimeException("The path to file or object is null!");
    }

    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.marshal(object, path.toFile());
    } catch (Exception ex) {
        throw new RuntimeException("Error saving the object to path " + path.toString(), ex);
    }
}

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);
    return writer.toString();
}

From source file:Main.java

public static <T> String toString(T xml) {
    JAXBContext jc;//from w ww .ja v  a 2  s  .c om
    try {
        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 <T> String print(T xml, Class<?>... clazz) throws JAXBException {
    //Create JAXB Context
    JAXBContext jc = JAXBContext.newInstance(clazz);

    //Create marshaller
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    StringWriter writer = new StringWriter();
    marshaller.marshal(xml, writer);

    return writer.toString();
}

From source file:Main.java

public static String parseObjToXmlString(Object obj) {
    if (obj == null) {
        return noResult;
    }/*from w  ww . j av a2s. c  o  m*/
    StringWriter sw = new StringWriter();
    JAXBContext jAXBContext;
    Marshaller marshaller;
    try {
        jAXBContext = JAXBContext.newInstance(obj.getClass());
        marshaller = jAXBContext.createMarshaller();
        marshaller.marshal(obj, sw);
        return sw.toString();
    } catch (JAXBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return noResult;
}

From source file:Main.java

/**
 * Convert a bean to XML format using JAXB.
 * /*from   ww  w . j  a  v  a2s  .c o m*/
 * @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

public static String parseObject2XmlString(Object object) {
    if (object == null) {
        return noResult;
    }//from w ww  .  j  ava2s. c o  m
    StringWriter sw = new StringWriter();
    JAXBContext jAXBContent;
    Marshaller marshaller;
    try {
        jAXBContent = JAXBContext.newInstance(object.getClass());
        marshaller = jAXBContent.createMarshaller();
        marshaller.marshal(object, sw);
        return sw.toString();
    } catch (Exception e) {
        e.printStackTrace();
        return noResult;
    }
}