Make sure your XML document, DTD, copyright file (if you created one), and compiled classes are assembled. You may then run the example program, and you might be surprised at the output (shown in Example 5.5).
Example 5-5. SAXParserDemo Output
D:\prod\JavaXML> java SAXParserDemo D:\prod\JavaXML\contents\contents.xml Parsing XML File: D:\prod\JavaXML\contents\contents.xml * setDocumentLocator( ) called Parsing begins... **Parsing Error** Line: 13 URI: file:/D:/prod/JavaXML/contents/contents.xml Message: Document root element "JavaXML:Book", must match DOCTYPE root "JavaXML:Book".
This rather cryptic error is a significant problem when using DTDs
and namespaces together. The error seems to be stating that the root
specified in the
JavaXML:Book) does not match the root element of
the document itself. But the root element is
JavaXML:Book, right? Actually, it’s not! By
SAX 2.0 specifies that parsers must enable
their namespace feature, making all SAX 2.0 parsers namespace-aware
unless this feature is explicitly set to false. We did not change this default, so our
XMLReader implementation is namespace aware. The
unexpected result of this is that our root element is seen (by the
Book, with the namespace prefix of
JavaXML. But remember that XML 1.0 and DTDs cannot
distinguish between a prefix and element name, so the root element
the DTD expects to find is
JavaXML:Book. When it
Book, it reports the error above.