Java tutorial
//package com.java2s; //License from project: Apache License import java.io.StringReader; import javax.xml.bind.JAXBContext; import javax.xml.bind.Unmarshaller; import javax.xml.transform.stream.StreamSource; import org.w3c.dom.Document; public class Main { /** * Unmarshal XML data from XML DOM document using XSD string and return the resulting JAXB content tree * * @param dummyJAXBObject * Dummy contect object for creating related JAXB context * @param doc * XML DOM document * @param strXSD * XSD * @return resulting JAXB content tree * @throws Exception * in error case */ public static Object doUnmarshallingFromDOMDocument(Object dummyJAXBObject, Document doc, String strXSD) throws Exception { if (dummyJAXBObject == null) { throw new RuntimeException("No dummy context objekt (null)!"); } if (doc == null) { throw new RuntimeException("No XML DOM document (null)!"); } if (strXSD == null) { throw new RuntimeException("No XSD document (null)!"); } Object unmarshalledObject = null; StringReader reader = null; try { JAXBContext jaxbCtx = JAXBContext.newInstance(dummyJAXBObject.getClass().getPackage().getName()); Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller(); reader = new StringReader(strXSD); javax.xml.validation.Schema schema = javax.xml.validation.SchemaFactory .newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(new StreamSource(reader)); unmarshaller.setSchema(schema); unmarshalledObject = unmarshaller.unmarshal(doc); } catch (Exception e) { // Logger.XMLEval.logState("Unmarshalling failed: " + e.getMessage(), LogLevel.Error); throw e; } finally { if (reader != null) { reader.close(); reader = null; } } return unmarshalledObject; } }