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:ee.ria.xroad.proxy.clientproxy.MetadataClientRequestProcessor.java

private static void marshal(Object object, OutputStream out) throws Exception {
    Marshaller marshaller = JAXB_CTX.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.marshal(object, out);
}

From source file:at.ac.tuwien.dsg.comot.m.common.Utils.java

public static String asJsonString(Object obj, Class<?>... clazz) throws JAXBException {

    List<Object> list = new ArrayList<Object>(Arrays.asList(clazz));
    list.add(obj.getClass());/*from w ww.j  a va2  s.co  m*/

    StringWriter w = new StringWriter();

    Map<String, Object> props = new HashMap<String, Object>();
    // props.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
    props.put(JAXBContextProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);

    JAXBContext context = JAXBContextFactory.createContext(list.toArray(new Class[list.size()]), props);

    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(obj, w);

    return w.toString();
}

From source file:Main.java

/**
 *
 * @param obj/*from   ww  w  . java2 s  . c  o  m*/
 * @param filename
 * @throws JAXBException
 * @throws FileNotFoundException
 */
public static void serialize(Object obj, String filename) throws JAXBException, FileNotFoundException {
    final Marshaller m = JAXBContext.newInstance(obj.getClass()).createMarshaller();
    m.setProperty(JAXB_FRAGMENT, TRUE);
    m.setProperty(JAXB_FORMATTED_OUTPUT, TRUE);
    m.marshal(obj, new FileOutputStream(filename));
}

From source file:Main.java

/**
 *
 * @param obj/* w  w  w .  j a  v  a2s.  c  om*/
 * @param file
 * @throws JAXBException
 * @throws FileNotFoundException
 */
public static void serialize(Object obj, File file) throws JAXBException, FileNotFoundException {
    final Marshaller m = JAXBContext.newInstance(obj.getClass()).createMarshaller();
    m.setProperty(JAXB_FRAGMENT, TRUE);
    m.setProperty(JAXB_FORMATTED_OUTPUT, TRUE);
    m.marshal(obj, new FileOutputStream(file));
}

From source file:Main.java

/**
 *
 * @param obj/*from   www. j ava 2 s . c  o m*/
 * @param w
 * @throws JAXBException
 * @throws FileNotFoundException
 */
public static void serialize(Object obj, Writer w) throws JAXBException, FileNotFoundException {
    final Marshaller m = JAXBContext.newInstance(obj.getClass()).createMarshaller();
    m.setProperty(JAXB_FRAGMENT, TRUE);
    m.setProperty(JAXB_FORMATTED_OUTPUT, TRUE);
    m.marshal(obj, w);
}

From source file:eu.seaclouds.platform.planner.core.application.decorators.SeaCloudsManagementPolicyDecorator.java

private static String encodeBase64MonitoringRules(MonitoringInfo monitoringInfo) {
    StringWriter sw = new StringWriter();
    String encodeMonitoringRules;
    JAXBContext jaxbContext;/*  w  w w  . j av a  2  s  . co  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());
    }
    encodeMonitoringRules = Base64.encodeBase64String(marshalledMonitoringRules.getBytes());
    return encodeMonitoringRules;
}

From source file:gov.nih.nci.coppa.services.client.ClientUtils.java

private static void printXml(Object obj) throws JAXBException {
    JAXBContext jaxbContext = MAP.get(obj.getClass().getPackage().getName());
    if (jaxbContext == null) {
        jaxbContext = JAXBContext.newInstance(obj.getClass().getPackage().getName());
        MAP.put(obj.getClass().getPackage().getName(), jaxbContext);
    }// www  .  j  av a  2s  . c  om
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.marshal(obj, System.out);
}

From source file:Main.java

public static String obj2Xml(Object obj, String encoding) {
    String result = null;/* w  ww  .  j  a  v a2  s.  c om*/
    try {
        JAXBContext context = JAXBContext.newInstance(obj.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
        StringWriter writer = new StringWriter();
        marshaller.marshal(obj, writer);
        result = writer.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

/**
 * /*from w  w w  . j  a v a 2 s  . co  m*/
 * @param object
 * @return
 * @throws JAXBException
 */
public static String pojoToXml(Object object) throws JAXBException {

    JAXBContext context = JAXBContext.newInstance(object.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    // marshaller.setProperty(Marshaller.JAXB_ENCODING, "U");
    StringWriter writer = new StringWriter();
    marshaller.marshal(object, writer);
    String xmlData = writer.toString();
    return xmlData;
}

From source file:Main.java

public static String convertToXml(Object obj, String encoding) {
    String result = null;//  w  w w.  ja  v a 2  s .c om
    try {
        JAXBContext context = JAXBContext.newInstance(obj.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);

        StringWriter writer = new StringWriter();
        marshaller.marshal(obj, writer);
        result = writer.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return result;
}