XML
  Home arrow XML arrow Page 8 - Java and XML Basics, Part 3
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

Java and XML Basics, Part 3
By: Liviu Tudor
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 22
    2004-04-20

    Table of Contents:
  • Java and XML Basics, Part 3
  • Which One is the Better One to Use?
  • Running the Parser
  • Problems with Big XML Files
  • Validating Parsers - DOM
  • Where do We Get a Validating Parser?
  • ErrorHandler
  • Validating Parsers - SAX
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


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

    More XML Articles
    More By Liviu Tudor


     

    XML ARTICLES

    - 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
    - Simple Web Syndication with RSS 2.0
    - Java UI Design with an IDE







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek