Java XML JAXB Marshaller marshallXml(final Object object)

Here you can find the source of marshallXml(final Object object)

Description

marshall Xml

License

Open Source License

Declaration

static String marshallXml(final Object object) 

Method Source Code


//package com.java2s;

import java.io.StringWriter;

import java.util.Map;
import java.util.Map.Entry;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class Main {
    static String marshallXml(final Object object) {
        return marschall(object, null);
    }/*from  w  w w .j  av a  2 s. c  o m*/

    private static String marschall(final Object object, final Map<String, Object> marchallerProps) {
        try {
            return marschallImpl(object, marchallerProps);
        } catch (final JAXBException e) {
            throw new RuntimeException(e);
        }
    }

    private static String marschallImpl(final Object object, final Map<String, Object> marchallerProps)
            throws JAXBException {

        final JAXBContext context = JAXBContext.newInstance(object.getClass());
        final Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        if (marchallerProps != null) {
            for (final Entry<String, Object> property : marchallerProps.entrySet()) {
                marshaller.setProperty(property.getKey(), property.getValue());
            }
        }

        final StringWriter stringWriter = new StringWriter();
        marshaller.marshal(object, stringWriter);

        return stringWriter.toString();
    }
}

Related

  1. marshallMessage(Object message, Marshaller marshaller, DataOutputStream dos)
  2. marshallObjectAsString(Class clazz, T object)
  3. marshallObjectToXML(final Object jaxbModel, final URL schemaURL)
  4. marshallToString(Object jaxbElement, Class c)
  5. marshallToXml(String zpackage, Writer writer, Object data)
  6. marshallXMLInConsole(Object obj)
  7. marshalObject(Object obj, File file)
  8. marshalObjectToXml(Object object, String xmlFilePath)
  9. marshalPackage(OutputStream printStream, final Package p)