Java Utililty Methods XML JAXB Marshaller

List of utility methods to do XML JAXB Marshaller

Description

The list of methods to do XML JAXB Marshaller are organized into topic(s).

Method

StringmarshallJAXBObject(String namespace, Object o)
Marshall a JAXB object and return the XML as a string.
return marshallJAXBObject(namespace, o, true);
voidmarshallMessage(Object message, Marshaller marshaller, DataOutputStream dos)
marshall Message
ByteArrayOutputStream baos = new ByteArrayOutputStream();
marshaller.marshal(message, baos);
byte[] buff = baos.toByteArray();
dos.writeInt(buff.length);
dos.write(buff);
dos.flush();
StringmarshallObjectAsString(Class clazz, T object)
marshall Object As String
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter writer = new StringWriter();
jaxbMarshaller.marshal(object, writer);
return writer.toString();
byte[]marshallObjectToXML(final Object jaxbModel, final URL schemaURL)
marshall Object To XML
if (jaxbModel == null) {
    return null;
if (schemaURL == null) {
    throw new IllegalArgumentException("schemaURL is null");
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
...
StringmarshallToString(Object jaxbElement, Class c)
marshall To String
initMarshaller(c);
ByteArrayOutputStream fos = new ByteArrayOutputStream();
m.marshal(jaxbElement, fos);
fos.flush();
String res = fos.toString("UTF-8");
fos.close();
return res;
voidmarshallToXml(String zpackage, Writer writer, Object data)
marshall To Xml
JAXBContext jc;
try {
    jc = JAXBContext.newInstance(zpackage);
    Marshaller m = jc.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(data, writer);
} catch (JAXBException e) {
    throw new RuntimeException(e);
...
StringmarshallXml(final Object object)
marshall Xml
return marschall(object, null);
voidmarshallXMLInConsole(Object obj)
marshall XML In Console
try {
    JAXBContext context = JAXBContext.newInstance(obj.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(obj, System.out);
} catch (Exception e) {
    e.printStackTrace();
voidmarshalObject(Object obj, File file)
marshal Object
try {
    JAXBContext jaxbContext = JAXBContext.newInstance("com.osafe.feeds.osafefeeds");
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    jaxbMarshaller.marshal(obj, file);
} catch (JAXBException e) {
    e.printStackTrace();
voidmarshalObjectToXml(Object object, String xmlFilePath)
Creates a new XML file based on the contents and schema contained in the specified JAXB object.
if (object == null) {
    return;
JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
File xmlFile = new File(xmlFilePath);
jaxbMarshaller.marshal(object, xmlFile);
...