Using Xerces-J to parse and validate XML files

I use Apache's Xerces-J to validate XML files, however it's not necessarily the easiest application to setup and use, and I do have to admit that Xerces does need a little "fettling" to work as expected.
I've put to gether a little HowTo on getting it working and validating XML files.

The first issue is that Xerces-J contains no simple "validate" command! You actually have to run one of the samples (dom.Counter) which first validates it's input (printing errors as it goes) and then counts the number of nodes in an XML file.

Firstly you need to download Xerces-J from here.

Unpack the ZIP file into a folder on your hard disk - I used C:\Program Files\xerces-2_9_0, use 7-Zip if you need a free compression tool that deals with all the compressed formats Xerces is distributed in.

I then wrote a little batch file that automates the process of running this sample (you can just copy and paste this code into a file called "validate.bat" and place it anywhere in your system's path):

@ECHO OFF
SET XERCESFOLDER=C:\Program Files\Xerces-2_9_0
SET CLASSPATH=%XERCESFOLDER%\xercesImpl.jar;%XERCESFOLDER%\xmlParserAPIs.jar;%XERCESFOLDER%\xercesSamples.jar
java dom.Counter -s -v %1 %2 %3 %4 %5 %6 %7 %8

As you can see, this sets up the Java classpath variable and then runs the "dom.Counter" class with any parameters passed to the batch file.
An example of the output would be:
C:\Diggsml\Example DIGGS>validate "Example 1.xml"
Example 1.xml: 1781;16;0 ms (94 elems, 27 attrs, 0 spaces, 1238 chars)

The fact that the dom.Counter class managed to count the file without any errors means it must be valid, good!
For the sake of completeness here's an example that fails validation (however it is a well-formed document so the count still succeeds):
C:\Diggsml\Example DIGGS>validate "Example 2.xml"
[Error] Example%202.xml:42:21: cvc-complex-type.2.4.a: Invalid content was found starting with element 'startDateTime'. 
One of '{"http://www.opengis.net/gml":metaDataProperty, "http://www.opengis.net/gml":description, "http://www.opengis.net/gml":name,
"http://www.opengis.net/gml":boundedBy, "http://www.opengis.net/gml":location, "http://www.diggsml.org":id}' is expected.
[Error] Example%202.xml:92:10: cvc-complex-type.2.4.a: Invalid content was found starting with element 'type'. 
One of '{"http://www.diggsml.org":remarks, "http://www.diggsml.org":associatedFiles}' is expected.
Example 2.xml: 1469;16;0 ms (73 elems, 20 attrs, 0 spaces, 1121 chars)

Here we can see two errors, "startDateTime" and "type" are not valid elements at the position they have been entered.