Java XML JAXB Unmarshaller unmarshallXMLtoObject(final byte[] xmlObject, final Class objectClass, final URL schemaURL)

Here you can find the source of unmarshallXMLtoObject(final byte[] xmlObject, final Class objectClass, final URL schemaURL)

Description

unmarshall XM Lto Object

License

Open Source License

Declaration

public static <T> T unmarshallXMLtoObject(final byte[] xmlObject, final Class<T> objectClass,
            final URL schemaURL) throws JAXBException, IOException, SAXException 

Method Source Code

//package com.java2s;
/**/*  w w w. j  a  va  2s  . co  m*/
 * 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.ByteArrayInputStream;

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

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

import javax.xml.bind.Unmarshaller;

import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.xml.sax.SAXException;

public class Main {
    public static <T> T unmarshallXMLtoObject(final byte[] xmlObject, final Class<T> objectClass,
            final URL schemaURL) throws JAXBException, IOException, SAXException {
        if (xmlObject == null) {
            return null;
        }
        if (schemaURL == null) {
            throw new IllegalArgumentException("schemaURL is null");
        }
        final SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        final Schema schema = sf.newSchema(schemaURL);
        final JAXBContext contextObj = JAXBContext.newInstance(objectClass);
        final Unmarshaller um = contextObj.createUnmarshaller();
        um.setSchema(schema);
        final ByteArrayInputStream bais = new ByteArrayInputStream(xmlObject);
        final StreamSource ss = new StreamSource(bais);
        try {
            final JAXBElement<T> jaxbElement = um.unmarshal(ss, objectClass);
            return jaxbElement.getValue();
        } finally {
            bais.close();
        }
    }
}

Related

  1. unmarshaller(String xml, Class T)
  2. unmarshallJAXBElement(JAXBElement v)
  3. unMarshallRequest(String xmlString)
  4. unmarshallString(String str, Class c)
  5. unmarshallXml(final String string, final Class type)
  6. unmarshalObject(final InputStream input, final Class clazz)
  7. unmarshalPackage(InputStream pkgStream)
  8. unmarshalXML(String xmlString, Class classType)
  9. unmarshalXmlToObject(String xmlFilePath, Class clazz)