#15 [xhtml]

XNI Parsing Example

  1. 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.*;
  2. 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);
    
  3. Setup the catalog:

    String [] catalogs = { "catalog.xml" };
    XMLCatalogResolver resolver = new XMLCatalogResolver(catalogs);
    parser.setEntityResolver(resolver);
    
  4. 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());
    
  5. Parse your document:

    String uri = new File("doc.xml").toURI().toString();
    XMLInputSource source = new XMLInputSource(null,uri,uri);
    parser.parse(source);