Datasheet
35: r.setContentHandler(bookHandler);
36: r.setErrorHandler(bookHandler);
37: EntityResolver bookResolver = new BookResolver();
38: r.setEntityResolver(bookResolver);
The EntityResolver interface originated in SAX, but it’s also used by the Xerces DOM parser and by the
JAXP DocumentBuilder. All you need to do to make it work is create an instance of a class that imple-
ments the org.xml.sax.EntityResolver interface and then pass that object to the setEntityResolver method
on XMLReader, SAXParser, DOMParser, or DocumentBuilder.
39: try {
40: r.parse(args[0]);
41: System.out.println(bookHandler.getBook().toString());
42: } catch (SAXException se) {
43: System.out.println("SAX Error during parsing " +
44: se.getMessage());
45: se.printStackTrace();
46: } catch (IOException ioe) {
47: System.out.println("I/O Error during parsing " +
48: ioe.getMessage());
49: ioe.printStackTrace();
50: } catch (Exception e) {
51: System.out.println("Error during parsing " +
52: e.getMessage());
53: e.printStackTrace();
54: }
55: }
56: }
The real work happens in a class that implements the EntityResolver interface. This is a simple interface
with only one method, resolveEntity. This method tries to take an entity that is identified by a Public ID,
System ID, or both, and provide an InputSource the parser can use to grab the contents of the entity:
1: /*
2: *
3: * BookResolver.java
4: *
5: * This file is part of the "Apache XML Tools" Book
6: *
7: */
8: package com.sauria.apachexml.ch2;
9:
10: import java.io.FileReader;
11: import java.io.IOException;
12:
13: import org.xml.sax.EntityResolver;
14: import org.xml.sax.InputSource;
15: import org.xml.sax.SAXException;
16:
17: public class BookResolver implements EntityResolver {
18: String schemaURI =
19: "http://www.sauria.com/schemas/apache-xml-book/book.xsd";
20:
30
Chapter 1
01 543555 Ch01.qxd 11/5/03 9:40 AM Page 30