Java XML JAXB Marshaller marshallObjectToXML(final Object jaxbModel, final URL schemaURL)

Here you can find the source of marshallObjectToXML(final Object jaxbModel, final URL schemaURL)

Description

marshall Object To XML

License

Open Source License

Declaration

public static byte[] marshallObjectToXML(final Object jaxbModel, final URL schemaURL)
            throws JAXBException, IOException, SAXException 

Method Source Code

//package com.java2s;
/**/* w  w w .jav  a2s.  c om*/
 * Copyright (C) 2015 BonitaSoft S.A.
 * BonitaSoft, 32 rue Gustave Eiffel - 38000 Grenoble
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation
 * version 2.1 of the License.
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
 * Floor, Boston, MA 02110-1301, USA.
 **/

import java.io.ByteArrayOutputStream;

import java.io.IOException;
import java.net.URL;

import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;

import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.xml.sax.SAXException;

public class Main {
    public static byte[] marshallObjectToXML(final Object jaxbModel, final URL schemaURL)
            throws JAXBException, IOException, SAXException {
        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);
        final Schema schema = sf.newSchema(schemaURL);
        try {
            final JAXBContext contextObj = JAXBContext.newInstance(jaxbModel.getClass());
            final Marshaller m = contextObj.createMarshaller();
            m.setSchema(schema);
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            m.marshal(jaxbModel, baos);
        } finally {
            baos.close();
        }
        return baos.toByteArray();
    }
}

Related

  1. marshaller(Object obj, File file)
  2. marshallerObject(Class c, Object o)
  3. marshallJAXBObject(String namespace, Object o)
  4. marshallMessage(Object message, Marshaller marshaller, DataOutputStream dos)
  5. marshallObjectAsString(Class clazz, T object)
  6. marshallToString(Object jaxbElement, Class c)
  7. marshallToXml(String zpackage, Writer writer, Object data)
  8. marshallXml(final Object object)
  9. marshallXMLInConsole(Object obj)