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

private static String parseObject2XmlString(Object object) {
    if (object == null) {
        return noResult;
    }/*from w  w  w .j  av  a 2 s  .  c om*/
    StringWriter sw = new StringWriter();
    JAXBContext jaxbContext;
    Marshaller marshaller;
    try {
        jaxbContext = JAXBContext.newInstance(object.getClass());
        marshaller = jaxbContext.createMarshaller();
        marshaller.marshal(object, sw);
        return sw.toString();
    } catch (JAXBException e) {
        e.printStackTrace();
        return noResult;
    }
}

From source file:Main.java

public static String bean2Xml(Object bean) throws Exception {
    StringWriter writer = null;//from w  ww .j  a  v  a  2 s  . c  o m
    try {
        JAXBContext jc = JAXBContext.newInstance(new Class[] { bean.getClass() });
        Marshaller m = jc.createMarshaller();

        m.setProperty("jaxb.fragment", Boolean.valueOf(true));
        writer = new StringWriter();
        m.marshal(bean, writer);
        return prefix + writer.toString();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        close(writer);
    }
    return null;
}

From source file:Main.java

/**
 * Marshal a jaxb object to obtain the xml format as a string.
 * //from  w w w  .java 2  s  .  c  om
 * @param jabxbObject the jaxb object
 * @param jaxbContextName the contect name where to look to find jaxb classes
 * @param cl : {@link ClassLoader} of the ObjectFactory
 * @return a string representing the jaxb object
 * @throws JAXBException
 */
public static String marshal(Object jabxbObject, String jaxbContextName, ClassLoader cl) throws JAXBException {
    Marshaller marshaller = createMarshaller(jaxbContextName, cl);

    final ByteArrayOutputStream xmlStream = new ByteArrayOutputStream();
    marshaller.marshal(jabxbObject, xmlStream);

    return xmlStream.toString();
}

From source file:Main.java

/**
 * Marshal a jaxb object to obtain the xml format as a string in specific encoding.
 * /* www  . j  av  a  2 s .c o  m*/
 * @param jabxbObject the jaxb object
 * @param jaxbContextName the contect name where to look to find jaxb classes
 * @param cl : {@link ClassLoader} of the ObjectFactory
 * @return a string representing the jaxb object
 * @throws JAXBException
 * @throws UnsupportedEncodingException
 */
public static String marshal(Object jabxbObject, String jaxbContextName, ClassLoader cl, String encoding)
        throws JAXBException, UnsupportedEncodingException {
    Marshaller marshaller = createMarshaller(jaxbContextName, cl);

    final ByteArrayOutputStream xmlStream = new ByteArrayOutputStream();
    marshaller.marshal(jabxbObject, xmlStream);

    return xmlStream.toString(encoding);
}

From source file:Main.java

public static void serialize(Object o, OutputStream os, Boolean format) throws JAXBException {
    Marshaller m = CTX.createMarshaller();
    m.setEventHandler(new DefaultValidationEventHandler());
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, format);
    m.marshal(o, os);
}

From source file:Main.java

public static <T> String generateObject2XMLString(T object) throws JAXBException {

    JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    StringWriter sw = new StringWriter();
    jaxbMarshaller.marshal(object, sw);

    return sw.toString();
}

From source file:net.firejack.aws.license.LicenseHelper.java

public static File create(License license) throws IOException, NoSuchAlgorithmException, JAXBException {
    signature(license);/*from w  ww  . java2 s .  c  o m*/

    File tmp = new File("/tmp/", license.getName() + ".xml");

    tmp.getParentFile().mkdirs();
    tmp.createNewFile();
    FileOutputStream stream = new FileOutputStream(tmp);

    JAXBContext jaxbContext = JAXBContext.newInstance(License.class);
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.marshal(license, stream);
    stream.close();

    return tmp;
}

From source file:de.hybris.platform.b2b.punchout.PunchOutUtils.java

public static String marshallFromBeanTree(final CXML cxml) {
    final StringWriter writer = new StringWriter();

    try {//from w  w  w .j  ava  2  s  .co  m
        final JAXBContext context = JAXBContext.newInstance(CXML.class);
        final Marshaller m = context.createMarshaller();
        removeStandalone(m);
        setHeader(m);
        m.marshal(cxml, writer);
    } catch (final JAXBException e) {
        throw new PunchOutException(e.getErrorCode(), e.getMessage());
    }

    final String xml = writer.toString();

    return xml;
}

From source file:Main.java

public static String marshaller(Object o, Class<?> T) {
    JAXBContext jc;//from  w  w w  .ja  va2  s  .  c  o m
    Marshaller marshaller;
    StringWriter writer = new StringWriter();
    try {
        jc = JAXBContext.newInstance(T);
        marshaller = jc.createMarshaller();
        marshaller.marshal(o, writer);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return writer.toString();

}

From source file:ch.entwine.weblounge.common.impl.security.AccessControlParser.java

/**
 * Serializes an AccessControlList to its XML form.
 * /*from   w  w  w  . ja v  a2  s.c  om*/
 * @param acl
 *          the access control list
 * @return the xml as a string
 * @throws IOException
 *           if there is a problem marshaling the xml
 */
public static String toXml(AccessControlList acl) throws IOException {
    try {
        Marshaller marshaller = jaxbContext.createMarshaller();
        Writer writer = new StringWriter();
        marshaller.marshal(acl, writer);
        return writer.toString();
    } catch (JAXBException e) {
        throw new IOException(e);
    }
}