XML Validation Tool

This is a very simple XML validation tool, that can check an XML file for well-formedness, or validate the document against an XML schema. The zip file includes two batch files: xmlv.bat and xmlwf.bat.

Checking for well-formedness

The xmlwf.bat batch file simply checks for well-formedness, and prints the elements and line numbers, indented to show containment relationships. It's normally quite simple to spot an error.

Run it like this:

xmlwf xml-file
Here's the kind of output it produces:
C:\temp>xmlwf document.xml

C:\temp>c:\software\jdk1.5.0_01\bin\java -classpath c:\bin\xmlv.jar net.alethis.
xmlvalidate.XMLWellFormed document.xml
<doc:root> (3, 66)
  <doc:title> (5, 12)
  </doc:title> (5, 34)
  <doc:section> (7, 38)
    <doc:subsection> (9, 41)
      <doc:item> (10, 11)
      </doc:item> (10, 54)
      <doc:item> (11, 11)
      </doc:item> (11, 81)
      <doc:item> (12, 11)
      </doc:item> (12, 68)
      <doc:example> (13, 14)
      </doc:example> (15, 30)
      <doc:item> (16, 11)
      </doc:item> (18, 88)
      <doc:example> (19, 14)
      </doc:example> (23, 30)
    </doc:subsection> (24, 18)
    <doc:subsection> (26, 49)
      <doc:item> (27, 11)
      </doc:item> (27, 59)
      <doc:list> (28, 11)
        <doc:listitem> (29, 15)
        </doc:listitem> (29, 36)
        <doc:listitem> (30, 15)
        </doc:listitem> (30, 37)
        <doc:listitem> (31, 15)
        </doc:listitem> (31, 38)
      </doc:list> (32, 12)
      <doc:item> (33, 11)
      </doc:item> (33, 58)
      <doc:list> (34, 11)
        <doc:listitem> (35, 15)
        </doc:listitem> (35, 38)
        <doc:listitem> (36, 15)
          <doc:listitem> (36, 39)
            <doc:listitem> (37, 15)
            </doc:listitem> (37, 41)
            <doc:listitem> (38, 15)
            </doc:listitem> (38, 39)
            <doc:listitem> (39, 15)
            </doc:listitem> (39, 44)
            <doc:listitem> (40, 15)
            </doc:listitem> (40, 35)
            <doc:listitem> (41, 15)
            </doc:listitem> (41, 37)
Parse exception at (42,3)

It looks pretty clear that at line 36, the doc:listitem start tag should have been an end tag!

Validating against an XML schema

The xmlv.bat batch file checks well-formedness, and prints out the same trace as xmlwf.bat, but it also validates the XML document against an XML schema.

Use it like this:

xmlv xml-file [xsd-file]

You can supply the XML schema document as the second parameter, or else it can be referenced from within the XML file.

NOTE: you need to use a Java 1.5 JRE to run the validator, since earlier versions of Java do not include JAXP 1.2, which is the first that supports XML Schema validation.

Here's typical output:

C:\temp>xmlv document.xml document.xsd

C:\temp>c:\software\jdk1.5.0_01\bin\java -classpath c:\bin\xmlvalidate.jar net.a
lethis.xmlvalid.XMLValidate document.xml document.xsd
<doc:root> (3, 66)
  <doc:title> (5, 12)
  </doc:title> (5, 34)
  <doc:section> (7, 38)
    <doc:subsection> (9, 41)
      <doc:item> (10, 11)
      </doc:item> (10, 54)
      <doc:item> (11, 11)
      </doc:item> (11, 81)
      <doc:item> (12, 11)
      </doc:item> (12, 68)
      <doc:example> (13, 14)
      </doc:example> (15, 30)
      <doc:item> (16, 11)
      </doc:item> (18, 88)
      <doc:example> (19, 14)
      </doc:example> (23, 30)
    </doc:subsection> (24, 18)
    <doc:subsection> (26, 49)
      <doc:item> (27, 11)
      </doc:item> (27, 59)
      <doc:list> (28, 11)
Error: URI=null Line=29: cvc-complex-type.2.4.a: Invalid content was found start
ing with element 'doc:section'. One of '{"http://www.alethis.net/namespaces/docu
ment":listitem}' is expected.
SAX exception: Error: URI=null Line=29: cvc-complex-type.2.4.a: Invalid content
was found starting with element 'doc:section'. One of '{"http://www.alethis.net/
namespaces/document":listitem}' is expected.

Line 29 contains an element which is not permitted according to the schema.

Get the classes and batch files: xmlvalidate.zip

Get the source files: xmlvalidate-src.zip