XML DTD Validaiton with sax\Writer.java

Q

How to perform XML DTD validation with sax\Writer.java provided in the Apache Xerces package?

✍: FYIcenter

A

You can perform XML DTD validation with the sax\Writer.java program provided in the Apache Xerces package as shown below.

Run the sax\Writer.java program with "-v" option to turn on validation. If the input XML file does not meet the requirements of its DTD, you will see validation errors:

\fyicenter\xerces-2_11_0\samples>\fyicenter\jdk-1.8.0\bin\java 
   -cp .;..\xercesImpl.jar sax.Writer -v UserError.xml
   
<?xml version="1.0" encoding="UTF-8"?>
<User>
    <ID>101</ID>
    <Name>Frank Y. Ivy</Name>
[Error] UserError.xml:14:8: The content of element type "User" must match "(ID+,
BirthDate+,Name+,Sex+)".
</User>

The XML file with DTD used in the above test is:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- Copyright (c) 2017 FYIcenter.com -->
<!DOCTYPE User [
   <!ELEMENT User (ID+, BirthDate+, Name+, Sex+)>
   <!ELEMENT ID (#PCDATA)>
   <!ELEMENT BirthDate (#PCDATA)>
   <!ELEMENT Name (#PCDATA)>
   <!ELEMENT Sex (#PCDATA)>
]>

<User>
    <ID>101</ID>
    <Name>Frank Y. Ivy</Name>
</User>

 

Using Apache Xerces SAX Sample Programs

⇒⇒FAQ for Apache Xerces XML Parser

2017-10-08, 291👍, 0💬