Java and XML Basics, Part 3 - Where do We Get a Validating Parser?
(Page 6 of 9 )
JAXP specification requires that any parser conforming to the spec will be able to work in validating and non-validating mode and that the user should be able to dynamically specify whether parsing should perform validation as well or not. And to actually ask our parser to perform validity checking as well, all we have to do is ad one line to the code -- ValidatingDOMParser.java (OK, four, including the comment -- happy now?):
/**
* First instantiate a new factory which we'll use
* to create the DOM parser
*/
try
{
factoryBuilder = DocumentBuilderFactory.newInstance();
}
catch( FactoryConfigurationError fce )
{
System.err.println( "Error creating DOM parser factory:" );
fce.printStackTrace();
System.exit( 3 );
}
/**
* Require that the parsing process will do validation as well
*/
factoryBuilder.setValidating( true );
Now if we compile and run this example, we will get (nearly) what we expected -- an error! (Hmmm, I doubt really there are many programmers out there who will expect -- and pray for -- an error when they run their programs.):
java -classpath "%CLASSPATH%;." ValidatingDOMParser employee.xml
Warning: validation was turned on but an org.xml.sax.ErrorHandler was not set, which is probably not what is desired. Parser will use a default ErrorHandler to print the first 10 errors. Please call the 'setErrorHandler' method to fix this.
Error: URI=null Line=14: Attribute value for "name" is #REQUIRED.
Parsing successful!
Apart from the fact that the program reports that the compilation went well, we got our error. Let's not worry for the moment about what the whole error message means and why the parsing continued afterward and look only at the line which says that there is something wrong with the name attribute - more specifically in our case, it is missing! In order to check that the problem only relies with the name attribute, we will change the
employee.xml file back again by adding the name attribute and then run the program again:
<?xml version="1.0" encoding="UTF-8"? >
<!DOCTYPE Employees [
<!ELEMENT Employees (Employee)*>
<!ELEMENT Employee EMPTY>
<!ATTLIST Employee
name CDATA #REQUIRED
surname CDATA #REQUIRED
dob CDATA #IMPLIED
email CDATA #IMPLIED
phone CDATA #IMPLIED
address CDATA #IMPLIED>
]>
<Employees>
<Employee name="Liviu" surname="Tudor" dob="14/02/1975" email="user@domain.com" address="Coocooland"/>
<Employee name="Janet" surname="Jackson" dob="unpolite to reveal" email="janet@rhythmnation.com" address="Really really secret ;-)"/>
</Employees>
Result:
java -classpath "%CLASSPATH%;." ValidatingDOMParser employee.xml
Parsing successful!
Of course, you can try "unvalidating" the XML file as much as you like (for example add a new element type under Employees that is no declared in the DTD) and you will see an error message every time.
As a side task, you can have a look at all the set functions in the DocumentBuilderFactory class to check what other functionality a JAXP-compliant parser has to offer.
Next: ErrorHandler >>
More XML Articles
More By Liviu Tudor