Here you can find the source of unmarshal(final String xml, Class clazz, InputStream inputSchema)
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 |
@SuppressWarnings("unchecked") public static <T> T unmarshal(final String xml, Class<?> clazz, InputStream inputSchema)
//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); } }