XML
  Home arrow XML arrow Page 7 - 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 - ErrorHandler


    (Page 7 of 9 )

    Now let's return to the error message we have just seen - it mentions something about an ErrorHandler and a setErrorHandler function. Looking through the DocumentBuilder class documentation, we find indeed such a function and the docco says:

    public abstract void setErrorHandler(ErrorHandler eh)
    Specify the ErrorHandler to be used to report errors present in the XML document to be parsed. Setting this to null will result in the underlying implementation using it's own default implementation and behavior.
    Parameters:
    eh - The ErrorHandler to be used to report errors present in the XML document to be parsed
    .

    Now, at this point you have probably spotted the analogy with the SAX parsing, and you have probably remembered the error and fatalError functions we have written in the SimpleSAXParser classes. It is the same ErrorHandler interface that the DefaultHandler class implements in SAX and it is the actual class (interface) that is to receive notifications from the parser in case of warnings, errors and fatal errors.

    Let's modify now our code (ValidatingDOMParser.java) and add an ErrorHandler and assign it to the parser:


    /**
     * Next, using the factory we create a DOM parser
     */

    try
    {
     builder 
    factoryBuilder.newDocumentBuilder();
    }
    catch
    ParserConfigurationException pce )
    {
     System
    .err.println"Error creating DOM parser:" );
     pce
    .printStackTrace();
     System
    .exit( );
    }
    /**
     * Set the error handler
     */
    builder.setErrorHandler( new DOMErrorHandler() );
     
    ... ... ...
     
    /**
     * XML parsing/validating error handler
     */

    static 
    class DOMErrorHandler implements ErrorHandler
    {
     
    /**
      * Error -- more than likely a validation error;
      * which means our XML is invalid and we should stop execution
      */

     
    public void errorSAXParseException exception )
     
    {
      System
    .out.println"Validation error: " exception.getMessage() );
      System
    .out.println"Execution will stop." );
      System
    .exit( );
     
    }
      
    /**
      * Fatal error -- execution should stop!
      */

     
    public void fatalErrorSAXParseException exception )
     
    {
      System
    .out.println"FATAL error: " exception.getMessage() );
      System
    .out.println"Execution will stop." );
      System
    .exit( );
     
    }
      
    /**
      * Warning, execution can continue, the user could be warned though
      */

     
    public void warningSAXParseException exception )
     
    {
      System
    .out.println"Warning: " exception.getMessage() );
     
    }
    }

    If we run this code, we will get the following:

    java -classpath "%CLASSPATH%;." ValidatingDOMParser2 employee.xml
    Validation error: Attribute value for "name" is #REQUIRED.
    Execution 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-2010 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek