Java XML JAXB Unmarshaller unmarshal(final String xml, Class clazz, InputStream inputSchema)

Here you can find the source of unmarshal(final String xml, Class clazz, InputStream inputSchema)

Description

Unmarshalls the XML string given the JaxB class.

License

Apache License

Parameter

Parameter Description
T a parameter
xml The xml string as input
clazz The class of the object which shall be unmarshalled
inputSchema The xsd schema

Declaration

@SuppressWarnings("unchecked") 
public static <T> T unmarshal(final String xml, Class<?> clazz, InputStream inputSchema) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
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.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.xml.sax.SAXException;

public class Main {
    private static final Logger logger = LogManager.getRootLogger();

    /**//from  w ww.jav  a 2  s.  c  om
     * Unmarshalls the XML string given the JaxB class. Also performs schema validation based on the given schema.
     * @param <T>
     * @param xml The xml string as input
     * @param clazz The class of the object which shall be unmarshalled
     * @param inputSchema The xsd schema
     * @return 
     */
    @SuppressWarnings("unchecked") // no need to check for the instance, if it is the wrong one the JaxB exception will take care of it
    public static <T> T unmarshal(final String xml, Class<?> clazz, InputStream inputSchema) {
        try {
            JAXBContext ctxt = JAXBContext.newInstance(clazz);
            Unmarshaller u = ctxt.createUnmarshaller();
            if (inputSchema != null) {
                if (inputSchema != null) {
                    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
                    Schema schema = sf.newSchema(new StreamSource(inputSchema));
                    u.setSchema(schema);
                }
            }
            return (T) u.unmarshal(new StringReader(xml));
        } catch (JAXBException | SAXException e) {
            StringWriter trace = new StringWriter();
            e.printStackTrace(new PrintWriter(trace));
            logger.warn("Unmarshalling error:" + System.getProperty("line.separator") + trace.toString());
        }
        return null;
    }

    /**
     * Unmarshalls the XML string given the JaxB class. Does not perform schema validation.
     * @param <T>
     * @param xml The xml string as input
     * @param clazz The class of the object which shall be unmarshalled
     * @return 
     */
    public static <T> T unmarshal(final String xml, Class<?> clazz) {
        return unmarshal(xml, clazz, null);
    }

    /**
     * Unmarshalls the XML document given the JaxB class.
     * @param <T>
     * @param xml The xml document as input
     * @param clazz The class of the object which shall be unmarshalled
     * @param inputSchema The xsd schema
     * @return 
     */
    @SuppressWarnings("unchecked") // no need to check for the instance, if it is the wrong one the JaxB exception will take care of it
    public static <T> T unmarshal(final File xmlFile, Class<?> clazz, InputStream inputSchema) {
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            if (inputSchema != null) {
                if (inputSchema != null) {
                    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
                    Schema schema = sf.newSchema(new StreamSource(inputSchema));
                    jaxbUnmarshaller.setSchema(schema);
                }
            }
            return (T) jaxbUnmarshaller.unmarshal(xmlFile);
        } catch (JAXBException | SAXException e) {
            StringWriter trace = new StringWriter();
            e.printStackTrace(new PrintWriter(trace));
            logger.warn("Unmarshalling error:" + System.getProperty("line.separator") + trace.toString());
        }
        return null;
    }

    /**
     * Unmarshalls the XML document given the JaxB class.
     * @param <T>
     * @param xml The xml document as input
     * @param clazz The class of the object which shall be unmarshalled
     * @return 
     */
    public static <T> T unmarshal(final File xmlFile, Class<?> clazz) {
        return unmarshal(xmlFile, clazz, null);
    }
}

Related

  1. unmarshal(Class clazz, String xml)
  2. unmarshal(Class clazz, String xmlInClassPath)
  3. unmarshal(Class clz, File file)
  4. unmarshal(File f)
  5. unmarshal(final Class clazz, String json)
  6. unmarshal(final String xml, final Class clazz)
  7. unmarshal(InputSource inputSource, Class clazz)
  8. unmarshal(InputStream content, Class expectedType)
  9. unmarshal(InputStream in, Class... boundClasses)