JDK (Java Development Kit) Tutorials
Dr. Herong Yang, Version 5.00

Testing DOM XML DTD Validator

This section provides a tutorial example on how to test the DOM XML DTD Validator. All errors are generated with line numbers and explanations.

To test the DOMValidator.java program described in the previous section, I created the following XML file with DTD statements, invalid_dtd.xml:

<?xml version="1.0"?>
<!-- dictionary_dtd.xml
     Copyright (c) 2002 by Dr. Herong Yang
-->
<!DOCTYPE dictionary [
 <!ELEMENT dictionary (note, word+)>
 <!ELEMENT note ANY>
 <!ELEMENT word (update?, name, definition+, usage*)>
 <!ELEMENT update EMPTY>
 <!ATTLIST update 
  date CDATA #REQUIRED 
  editor CDATA #IMPLIED
 >
 <!ELEMENT name (#PCDATA)>
 <!ATTLIST name is_acronym (true | false) "false">
 <!ELEMENT definition (#PCDATA)>
 <!ELEMENT usage (#PCDATA | i)*>
 <!ELEMENT i (#PCDATA)>
 <!ENTITY herong "Dr. Herong Yang">
]>
<dictionary>
 <note>Copyright (c) 2002 by &herong;</note>
 <word>
  <name is_acronym="true" language="EN">POP</name>
  <definition>Post Office Protocol</definition>
  <definition>Point Of Purchase</definition>
 </word>
 <word>
  <update date="2002-12-23"/> 
  <name is_acronym="yes">XML</name>
  <definition>eXtensible Markup Language</definition>
  <note>XML comes from SGML</note>
 </word>
 <word>
  <update editor="Herong Yang"/> 
  <name>markup</name>
  <definition>The amount added to the cost price to calculate 
the selling price - <i>Webster</i></definition>
 </word>
</dictionary>

After running, java DOMValidator invalid_dtd.xml, I got:

Error:
   Public ID: null
   System ID: file:C:/herong/invalid_dtd.xml
   Line number: 24
   Column number: -1
   Message: Attribute "language" is not declared for element "name".
Error:
   Public ID: null
   System ID: file:C:/herong/invalid_dtd.xml
   Line number: 30
   Column number: -1
   Message: Value "yes" is not one of the enumerated values 
      for this attribute.
Error:
   Public ID: null
   System ID: file:C:/herong/invalid_dtd.xml
   Line number: 32
   Column number: -1
   Message: Element "word" does not allow "note" here.
Error:
   Public ID: null
   System ID: file:C:/herong/invalid_dtd.xml
   Line number: 35
   Column number: -1
   Message: Attribute value for "date" is #REQUIRED.
Error:
   Public ID: null
   System ID: file:C:/herong/invalid_dtd.xml
   Line number: 38
   Column number: -1
   Message: Element "definition" does not allow "i" -- (#PCDATA)

This is perfect. It tells you where the error is, and why it's an error.

Last update: 2006.

Sections in This Chapter

DOMValidator.java - XML DTD Validation with DOM

Testing DOM XML DTD Validator

SAXValidator.java - XML DTD Validation with SAX

Dr. Herong Yang, updated in 2008
Testing DOM XML DTD Validator