Java XML JAXB Marshaller marshallerObject(Class c, Object o)

Here you can find the source of marshallerObject(Class c, Object o)

Description

marshaller Object

License

Open Source License

Declaration

public static String marshallerObject(Class c, Object o) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class Main {
    public static String marshallerObject(Class c, Object o) {
        String s = "<?xml version=\"1.0\" ?>";
        try {/*from w ww  . java 2s. c o m*/
            JAXBContext jaxbContext = JAXBContext.newInstance(c);
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_ENCODING, "utf-8");
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

            StringWriter baos = new StringWriter();
            marshaller.marshal(o, baos);
            s += baos.toString();
        } catch (Exception e) {
        }
        return s;
    }
}

Related

  1. marshall(OutputStream os, JAXBElement element)
  2. marshall(String cntxtPkg, Object obj, OutputStream out)
  3. marshall(String file, JAXBElement object, Class context)
  4. marshaller(Object o, Class T)
  5. marshaller(Object obj, File file)
  6. marshallJAXBObject(String namespace, Object o)
  7. marshallMessage(Object message, Marshaller marshaller, DataOutputStream dos)
  8. marshallObjectAsString(Class clazz, T object)
  9. marshallObjectToXML(final Object jaxbModel, final URL schemaURL)