Recipe 21.5 Verifying Structure with a DTD


Up to now, I have simply provided XML and asserted that it is valid. Now you want to verify the structure using a Document Type Definition (DTD).


Write the DTD and refer to it in one or more XML documents.


This is not the place for a full dissertation on Document Type Definition syntax. Briefly, a DTD is a means of restricting the structure of an XML document by listing all the elements allowed, where they are permitted, and what attributes they have, if any. The DTD uses a special syntax inherited from SGML. Example 21-8 is people.dtd, a DTD for the people.xml file shown earlier in this chapter.

Example 21-8. people.dtd
<!ELEMENT people (person)*> <!ELEMENT person (name, email, country)> <!ELEMENT name (#PCDATA)> <!ATTLIST email type CDATA #IMPLIED> <!ELEMENT email (#PCDATA)> <!ELEMENT country (#PCDATA)>

To verify that a file conforms to a DTD that is, to validate the file you do two things:

  1. Refer to the DTD from within the XML file, as is sometimes seen in HTML documents. The <!DOCTYPE> line should follow the <?xml ...> line but precede any actual data:

    <?xml version="1.0"?> <!DOCTYPE people SYSTEM "people.dtd"> <people> <person>         <name>Ian Darwin</name>         <email>someone@someplace.dom</email>         <country>Canada</country> </person>

  2. Pass true as a second argument to the createXMLDocument( ) method; true means "enforce document validity":

    XmlDocument doc = XmlDocument.createXmlDocument(uri, true);

Now any elements found in the document that are not valid according to the DTD result in an exception being thrown.

See Also

Document Type Definitions are simpler to write than XML Schemas. In some parts of the industry, people seem to be going on the assumption that XML Schemas will completely replace DTDs. But many other developers continue to use DTDs. There are other options for constraining structure and data types, including RelaxNG (an ISO standard).

Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: