Java XML JAXB Object to XML toXML(final Object obj, final boolean prettyPrint)

Here you can find the source of toXML(final Object obj, final boolean prettyPrint)

Description

Converts object to XML and returns the byte array of the XML text created.

License

Open Source License

Parameter

Parameter Description
obj the object to be converted into its XML format
prettyPrint specifies whether or not to set property JAXB_FORMATTED_OUTPUT on the marshaller

Return

byte array which represents the XML output for the object passed in

Declaration

public static byte[] toXML(final Object obj, final boolean prettyPrint) 

Method Source Code


//package com.java2s;

import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import javax.xml.namespace.QName;

public class Main {
    /**//from ww  w .  j a va2  s.com
     * Map of classes and respective JAXBContexts.
     */
    private static Map<Class<?>, JAXBContext> serMap = new HashMap<Class<?>, JAXBContext>();

    /**
     * Converts object to XML and returns the byte array of the XML text created.
     * @param obj 
     *  the object to be converted into its XML format
     * @param prettyPrint
     *  specifies whether or not to set property JAXB_FORMATTED_OUTPUT on the marshaller
     * @return
     *  byte array which represents the XML output for the object passed in
     */
    public static byte[] toXML(final Object obj, final boolean prettyPrint) {
        return toXML(obj, prettyPrint, null);
    }

    /**
     * Converts object to XML and returns the byte array of the XML text created.
     * @param obj
     *  the object to be converted into its XML format
     * @param prettyPrint
     *  specifies whether or not to set property JAXB_FORMATTED_OUTPUT on the marshaller
     * @param rootElementName
     *  if object does not have a root element annotation a root element name must be specified
     * @return
     *  byte array which represents the XML output for the object passed in 
     */
    public static byte[] toXML(final Object obj, final boolean prettyPrint, final String rootElementName) {
        try {
            final Class<?> theClass = obj.getClass();

            final JAXBContext context = getContext(theClass);

            final Marshaller marshaller = context.createMarshaller();

            if (prettyPrint) {
                marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            }

            final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();

            if (rootElementName == null) {
                marshaller.marshal(obj, byteStream);
            } else {
                @SuppressWarnings({ "rawtypes", "unchecked" })
                final JAXBElement jaxbElement = new JAXBElement(new QName(rootElementName), theClass, obj);

                marshaller.marshal(jaxbElement, byteStream);
            }

            return byteStream.toByteArray();
        } catch (final JAXBException exception) {
            throw new IllegalStateException(exception);
        }
    }

    /**
     * Gets the JAXBContext for the passed in class.
     * @param theClass
     *  the class to retrieve the JAXBContext for
     * @return
     *  the JAXBContext that has been found or created
     * @throws JAXBException
     *  throws a JAXBException if context cannot be created
     */
    private static JAXBContext getContext(final Class<?> theClass) throws JAXBException {
        if (serMap.containsKey(theClass)) {
            return serMap.get(theClass);
        } else {
            final JAXBContext jContext = JAXBContext.newInstance(theClass);
            serMap.put(theClass, jContext);
            return jContext;
        }
    }
}

Related

  1. ObjectToXml(Object object)
  2. objectToXml(Object source, Class... type)
  3. toString(E e)
  4. toString(Object o, Class clazz)
  5. toXml(Class className, Object object)
  6. toXml(final T dto)
  7. toXML(Object o)
  8. toXML(Object o, OutputStream os)
  9. toXml(Object o, OutputStream output)