Here you can find the source of marshallObjectToXML(final Object jaxbModel, final URL schemaURL)
public static byte[] marshallObjectToXML(final Object jaxbModel, final URL schemaURL) throws JAXBException, IOException, SAXException
//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(); } }