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

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
TABLE OF CONTENTS:
  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
SEARCH DEVARTICLES

Java and XML Basics, Part 3 - Validating Parsers - SAX
(Page 8 of 9 )

After seeing the above example, I suppose you have all guessed that turning on validation in SAX is similar, and you are all looking at the setValidating function member in the SAXParserFactory. Indeed, is just as simple, and since we already implement the ErrorHandler in our class, we don't have to supply a separate ErrorHandler at parse time (ValidatingSAXParser.java - based on SimpleSAXParser2.java):


/**
 * First instantiate a new factory which we'll use
 * to create the SAX parser
 */

try
{
 factorySAX 
SAXParserFactory.newInstance();
}
catch
FactoryConfigurationError fce )
{
 System
.err.println"Error creating SAX parser factory:" );
 fce
.printStackTrace();
 System
.exit( );
}
/**
 * Require validation
 */

factorySAX
.setValidatingtrue );

and the result:

java -classpath "%CLASSPATH%;." ValidatingSAXParser employee.xml
Following error occurred during parsing:
org.xml.sax.SAXParseException: Attribute value for "name" is #REQUIRED.
        at org.apache.crimson.parser.Parser2.error(Unknown Source)
        at org.apache.crimson.parser.Parser2.defaultAttributes(Unknown Source)
        at org.apache.crimson.parser.Parser2.maybeElement(Unknown Source)
        at org.apache.crimson.parser.Parser2.content(Unknown Source)
        at org.apache.crimson.parser.Parser2.maybeElement(Unknown Source)
        at org.apache.crimson.parser.Parser2.parseInternal(Unknown Source)
        at org.apache.crimson.parser.Parser2.parse(Unknown Source)
        at org.apache.crimson.parser.XMLReaderImpl.parse(Unknown Source)
        at javax.xml.parsers.SAXParser.parse(Unknown Source)
        at javax.xml.parsers.SAXParser.parse(Unknown Source)
        at ValidatingSAXParser.main(ValidatingSAXParser.java:102)
Parsing will stop...


blog comments powered by Disqus
XML ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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