Datasheet
If you’re using the grammar-caching mechanism to cache DTDs, be aware that it can only cache external
DTD subsets (DTDs in an external file). In addition, any definitions in an internal DTD subset (DTD
within the document) will be ignored.
Entity Handling
Earlier in the chapter we mentioned that we’d be looking at a mechanism that can do the same job as the
Xerces properties for xsi:schemaLocation and xsi:noNamespaceSchemaLocation. That mechanism is the
SAX entity resolver mechanism. Although it isn’t Xerces specific, it’s very useful, because all external
files are accessed as entities in XML. The entity resolver mechanism lets you install a callback that is run
at the point where the XML parser tries to resolve an entity from an ID into a physical storage unit
(whether that unit is on disk, in memory, or off on the network somewhere). You can use the entity
resolver mechanism to force all references to a particular entity to be resolved to a local copy instead of a
network copy, which simultaneously provides a performance improvement and gives you control over
the actual definition of the entities.
Let’s look at how to extend the example program to use an entity resolver:
1: /*
2: *
3: * EntityResolverMain.java
4: *
5: * Example from "Professional XML Development with Apache Tools"
6: *
7: */
8: package com.sauria.apachexml.ch1;
9:
10: import java.io.IOException;
11:
12: import org.apache.xerces.parsers.SAXParser;
13: import org.xml.sax.EntityResolver;
14: import org.xml.sax.SAXException;
15: import org.xml.sax.SAXNotRecognizedException;
16: import org.xml.sax.SAXNotSupportedException;
17: import org.xml.sax.XMLReader;
18:
19: public class EntityResolverMain {
20:
21: public static void main(String[] args) {
22: XMLReader r = new SAXParser();
23: try {
24: r.setFeature("http://xml.org/sax/features/validation",
25: true);
26: r.setFeature(
27: "http://apache.org/xml/features/validation/schema",
28: true);
29: } catch (SAXNotRecognizedException e1) {
30: e1.printStackTrace();
31: } catch (SAXNotSupportedException e1) {
32: e1.printStackTrace();
33: }
34: BookHandler bookHandler = new BookHandler();
29
Xerces
01 543555 Ch01.qxd 11/5/03 9:40 AM Page 29