Using a DTD to Verify an XML Document


DocumentBuilderFactory factory =    DocumentBuilderFactory.newInstance(); factory.setValidating(true); DocumentBuilder builder =    factory.newDocumentBuilder();



A Document Type Definition (DTD) file defines how a particular XML document type should be structured. For example, a DTD will specify which elements, attributes, and so on are permitted in a document. An XML document that conforms to a DTD is considered to be valid. An XML document that is syntactically correct, but does not conform to a DTD, is said to be well-formed.

To validate a document using a DTD, we simply need to call the setValidating() method of the DocumentBuilderFactory instance and pass a value of true. Any XML documents that we parse will then be validated against any DTDs that are specified in the XML document header. The following is a typical DTD declaration at the top of an XML document:

<!DOCTYPE people SYSTEM "file:baseball.dtd">


This declaration would attach the file baseball.dtd stored on the local file system as a DTD to the XML document in which it is declared.

When you specify DTD validation, if the XML document that you are parsing does not conform to the DTD, an exception will be thrown from the parse() method of the DocumentBuilder class.

A newer technology offering the same advantages of using a DTD is the XML Schema standard. An XML Schema defines an XML document's expected markup, just as a DTD does. One advantage of a schema document is that it is also an XML document, so you don't need yet another parser to read it, whereas a DTD is not a valid XML document itself. Instead of XML, DTDs are specified in XBNF (Extended Backus-Naur Form) grammar. To use a schema, you would use the setSchema() method of the DocumentBuilderFactory instead of the setValidating() method. This is shown here:

DocumentBuilderFactory factory =    DocumentBuilderFactory.newInstance(); factory.setSchema(schema); DocumentBuilder builder =    factory.newDocumentBuilder();


The setSchema() method takes an instance of a Schema object. We will not go into detail on using schemas here, but see the DocumentBuilderFactory's JavaDoc for more detail on implementing Schemas in Java: http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/parsers/DocumentBuilderFactory.html

For more detailed information about schemas in general, see the Schema standard documentation at the following site: http://www.w3.org/TR/xmlschema-1/




JavaT Phrasebook. Essential Code and Commands
Java Phrasebook
ISBN: 0672329077
EAN: 2147483647
Year: 2004
Pages: 166

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net