Home arrow XML arrow Page 6 - Java and XML Basics, Part 3

Java and XML Basics, Part 3

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.

Author Info:
By: Liviu Tudor
Rating: 5 stars5 stars5 stars5 stars5 stars / 25
April 20, 2004
  1. · Java and XML Basics, Part 3
  2. · Which One is the Better One to Use?
  3. · Running the Parser
  4. · Problems with Big XML Files
  5. · Validating Parsers - DOM
  6. · Where do We Get a Validating Parser?
  7. · ErrorHandler
  8. · Validating Parsers - SAX
  9. · Conclusion

print this article

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

FactoryConfigurationError fce )
.err.println"Error creating DOM parser factory:" );
.exit( );
 * Require that the parsing process will do validation as well

.setValidatingtrue );

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)*>
<!ATTLIST Employee
  name  CDATA 
  address CDATA #IMPLIED>
 <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 ;-)"/>


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.

blog comments powered by Disqus

- Open XML Finally Supported by MS Office
- XML Features Added to Two Systems
- Using Regions with XSL Formatting Objects
- Using XSL Formatting Objects
- More Schematron Features
- Schematron Patterns and Validation
- Using Schematron
- Datatypes and More in RELAX NG
- Providing Options in RELAX NG
- An Introduction to RELAX NG
- Path, Predicates, and XQuery
- Using Predicates with XQuery
- Navigating Input Documents Using Paths
- XML Basics
- Introduction to XPath

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials