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 jaxbToString(Class<?> xmlClass, JAXBElement<?> jaxbElement) {

    // Make sure we are given the correct input.
    if (xmlClass == null || jaxbElement == null) {
        return null;
    }/*ww  w  .jav a  2s . c o  m*/

    // We will write the XML encoding into a string.
    StringWriter writer = new StringWriter();
    String result;
    try {
        // We will use JAXB to marshal the java objects.
        final JAXBContext jaxbContext = JAXBContext.newInstance(xmlClass);

        // Marshal the object.
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.marshal(jaxbElement, writer);
        result = writer.toString();
    } catch (Exception e) {
        // Something went wrong so get out of here.
        return null;
    } finally {
        try {
            writer.close();
        } catch (IOException ex) {
        }
    }

    // Return the XML string.
    return result;
}

From source file:Main.java

/**
 *
 * @param obj//from   ww w .  j ava2  s  .c o m
 * @return
 * @throws JAXBException
 */
public static String serializeToString(Object obj) throws JAXBException {
    java.io.StringWriter sw = new StringWriter();
    final Marshaller m = JAXBContext.newInstance(obj.getClass()).createMarshaller();
    m.setProperty(JAXB_FRAGMENT, TRUE);
    m.setProperty(JAXB_FORMATTED_OUTPUT, TRUE);
    m.marshal(obj, sw);
    return sw.toString();
}

From source file:eu.planets_project.tb.impl.serialization.ExperimentRecords.java

/**
 * @param exp//from w  w  w.j a v  a2s  . c om
 * @param out
 */
private static void writeToOutputStream(ExperimentRecords exp, OutputStream out) {
    try {
        JAXBContext jc = JAXBContext.newInstance(ExperimentRecords.class);
        Marshaller m = jc.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.marshal(exp, out);
    } catch (JAXBException e) {
        log.fatal("Writing Experiments to XML failed: " + e);
    }
}

From source file:Main.java

public static String marshal(Object object) throws Exception {
    StringWriter string = new StringWriter();
    JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); //$NON-NLS-1$
    marshaller.marshal(object, string);
    return string.toString();
}

From source file:Main.java

public static <T> String marshal(T object) throws JAXBException {
    final Marshaller marshaller = JAXBContext.newInstance(object.getClass()).createMarshaller();
    final StringWriter stringWriter = new StringWriter();

    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(object, stringWriter);

    return stringWriter.toString();
}

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 void marshal(Object o, OutputStream os, Map<String, Object> properties) {
    try {//w ww.  java  2 s  . com
        JAXBContext ctx = JAXBContext.newInstance(o.getClass());
        Marshaller marshaller = ctx.createMarshaller();

        for (Entry<String, Object> p : properties.entrySet()) {
            marshaller.setProperty(p.getKey(), p.getValue());
        }

        marshaller.marshal(o, os);
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
}

From source file:eu.seaclouds.platform.dashboard.util.ObjectMapperHelpers.java

/**
 * Transforms an annotated Object to a XML string using javax.xml.bind.Marshaller
 *
 * @param object to transform to a XML String
 * @return a XML string representing the object
 * @throws IOException if is not possible to parse the object
 *///from w  ww  .j  av  a 2 s.  c o m
public static String ObjectToXml(Object object) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
    Marshaller marshaller = jaxbContext.createMarshaller();
    StringWriter sw = new StringWriter();
    marshaller.marshal(object, sw);
    return sw.toString();
}

From source file:eu.seaclouds.platform.planner.core.application.agreements.AgreementGenerator.java

private static String serializeToXml(MonitoringInfo monitoringInfo) {
    StringWriter sw = new StringWriter();
    JAXBContext jaxbContext;// w w w.j a v  a 2s .c  o  m
    String marshalledMonitoringRules = null;
    try {
        jaxbContext = JAXBContext.newInstance(MonitoringRules.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        jaxbMarshaller.marshal(monitoringInfo.getApplicationMonitoringRules(), sw);
        marshalledMonitoringRules = sw.toString();
    } catch (JAXBException e) {
        log.error("Monitoring rules {} can not be marshalled by addSeaCloudsPolicy in " + "DamGenerator",
                monitoringInfo.getApplicationMonitoringRules());
    }

    return marshalledMonitoringRules;
}

From source file:edu.duke.cabig.c3pr.rules.common.XMLUtil.java

public static String marshal(Object object) throws RuleException {
    StringWriter writer = new StringWriter();
    try {//  ww  w.j  a  v a2  s.c o  m
        Marshaller marshaller = JAXBContext.newInstance("edu.duke.cabig.c3pr.rules.brxml").createMarshaller();
        marshaller.marshal(object, writer);
        log.debug("Before writing:" + writer.toString());
        return writer.toString();
    } catch (JAXBException e) {
        throw new RuleException(e.getMessage(), e);
    }
}