“Gotcha!”
To continue with the theme of trying to provide some cautionary advice on your path to XML mastery, some additional pitfalls associated with XML validation are included here. These are often problems run into by newer XML developers, as the solutions are not immediately apparent. Take heed of them, as they have caused many a developer long hours of tedious debugging, or simple confusion at unexpected application output.
Handling Validation and Handling DTDs
One
of the most common misunderstandings about using SAX for validation
is thinking that validating an XML document is contingent upon
registering a SAX DTDHandler
implementation with
the XML parser. Often, time and effort are spent to implement the
DTDHandler
interface and register it with the
parser, and time is not spent setting the validation feature of the
parser. This mistake arises from a
mistaken association between handling a DTD and actually using the
DTD for validation. In this case, the DTD would be parsed, and all
DTD callback events would occur (if any were needed). However, the
XML document itself would not be validated, but simply parsed. Keep
in mind that the output from parsing a valid XML document looks
almost identical to output from a non-validated XML document; always
be aware when validation is occurring to avoid application bugs:
try { // Instantiate a parser XMLReader parser = XMLReaderFactory.createXMLReader( "org.apache.xerces.parsers.SAXParser"); // Register the content handler parser.setContentHandler(contentHandler); ...
Get Java and XML now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.