Java tutorial
//package com.java2s; import java.io.File; import java.io.InputStream; import javax.xml.XMLConstants; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import javax.xml.validation.SchemaFactory; import org.xml.sax.SAXException; public class Main { /** * Generic method to Validate XML file while unmarshalling against their schema. * * @param context * @param schemaFile * @param object * @return * @throws SAXException * @throws JAXBException */ public static Object validateAndUnmarshallXML(JAXBContext context, String schemaFile, InputStream fio) throws SAXException, JAXBException { if (context != null && (schemaFile != null && schemaFile.trim().length() > 0)) { Unmarshaller unMarshaller = context.createUnmarshaller(); SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); // thread- safe unMarshaller.setSchema(sf.newSchema(new File(schemaFile))); // validate jaxb context against schema return unMarshaller.unmarshal(fio); } return null; } }