Import the right classes:
import org.apache.xerces.util.*; import org.apache.xerces.xs.*; import org.apache.xerces.xni.*; import org.apache.xerces.xni.parser.*; import org.apache.xerces.parsers.*;
Create the parser and set the right features (much like JAXP):
XMLParserConfiguration parser = new StandardParserConfiguration(); parser.setFeature("http://xml.org/sax/features/validation",true); parser.setFeature("http://apache.org/xml/features/validation/schema",true); parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking",true);
Setup the catalog:
String [] catalogs = { "catalog.xml" }; XMLCatalogResolver resolver = new XMLCatalogResolver(catalogs); parser.setEntityResolver(resolver);
Set your document and error handlers
parser.setDocumentHandler(new MyDocumentHandler()); // DefaultErrorHandler is a utility class from Xerces that sends // errors to stderr. parser.setErrorHandler(new DefaultErrorHandler());
Parse your document:
String uri = new File("doc.xml").toURI().toString(); XMLInputSource source = new XMLInputSource(null,uri,uri); parser.parse(source);