So far, during this series of articles (part 1, part 2) we've looked at DOM and SAX, and I suppose most of you are thinking which one of the two approaches is preferable? Well, there is no general rule of thumb, but this article might help you make the right decision when you’ll have to.
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.):
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:
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.