XML
  Home arrow XML arrow Page 2 - Java and XML Basics, Part 2
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 2
By: Liviu Tudor
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 38
    2004-03-10

    Table of Contents:
  • Java and XML Basics, Part 2
  • DefaultHandler class
  • Parser Reports
  • Simple State Machine
  • Using SAX for XML Processing
  • One Last SAX Trick

  • 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 2 - DefaultHandler class


    (Page 2 of 6 )

    DefaultHandler class implements the following 4 handler interfaces:

    • ContentHandler
    • DTDHandler
    • EntityResolver
    • ErrorHandler
    While the DTDHandler and the EntityResolver are not our concern at the moment (we will be dealing with them later on, for the moment we just need to know the that DTDHandler receives DTD notifications and the EntityResolver is responsible for locating entities – like external references, DTDs etc.), we will make use of the ContentHandler and the ErrorHandler.

    As the names implies, the ContentHandler class receives notifications about the actual content of the XML document that has been parsed so far – for example, once an element has been encountered, the SAX parser will send 2 messages to the ContentHandler class, one to inform of the start of the element, and the second to notify of the end of this element tag. The ErrorHandler on the other side will receive messages regarding errors/problems encountered during parsing; from SAX’s point of view, there are 3 categories of problems in terms of parsing:

    • Warnings - when the parser has found irregularities in place, but they are minor and they don’t affect the rest of the parsing
    • Errors - when the parser has found major irregularities in place that might affect the rest of the parsing, if this continues
    • Fatal Errors - when major problems have been encountered and they prevent the parsing process to go any further.

    When one of these notifications is received by the ErrorHandler class (the DefaultHandler in our case), it is up to the program to decide whether the parsing should continue or not – the XML purists for example might want to stop the parsing process even if a warning is fired, while people like me probably wouldn’t want to bother even with the fatal errors!
     
    Now, let’s change our code a little and monitor the errors and the fatal errors encountered during parsing (SimpleSAXParser2.java). In order to achieve this, we need to override the following 2 functions:


    ...
    public void error
    SAXParseException e throws 
    SAXException
    {
      System
    .err.println"Following error occured during 
    parsing:" 
    );
      e
    .printStackTrace();
      System
    .err.println
    "Parsing will continue..." );
    }
    public 
    void fatalErrorSAXParseException e throws 
    SAXException
    {
      System
    .err.println"Following fatal error occured 
    during parsing:" 
    );
      e
    .printStackTrace();
      System
    .err.println
    "Parsing will stop." );
      
    throw new SAXException
    );
    }
    ...

    If we run this against our simple2.xml file, we get the following result:


    java -classpath "%CLASSPATH%;." SimpleSAXParser2 
    simple2
    .xml
    Following fatal error occured during 
    parsing
    :
    org
    .xml.sax.SAXParseExceptionExpected "</duration>" to 
    terminate element start
    ing on line 5.
    at 
    org
    .apache.crimson.parser.Parser2.fatal(Parser2.java:3182)
    at 
    org
    .apache.crimson.parser.Parser2.fatal(Parser2.java:3176)
    at 
    org
    .apache.crimson.parser.Parser2.maybeElement(Parser2.java:1513)
    at 
    org
    .apache.crimson.parser.Parser2.content(Parser2.java:1779)
    at 
    org
    .apache.crimson.parser.Parser2.maybeElement(Parser2.java:1507)
    at 
    org
    .apache.crimson.parser.Parser2.content(Parser2.java:1779)
    at 
    org
    .apache.crimson.parser.Parser2.maybeElement(Parser2.java:1507)
    at 
    org
    .apache.crimson.parser.Parser2.content(Parser2.java:1779)
    at 
    org
    .apache.crimson.parser.Parser2.maybeElement(Parser2.java:1507)
    at 
    org
    .apache.crimson.parser.Parser2.parseInternal(Parser2.java:500)
    at 
    org
    .apache.crimson.parser.Parser2.parse(Parser2.java:305)
    at 
    org
    .apache.crimson.parser.XMLReaderImpl.parse(XMLReaderImpl.java:442)
    at 
    javax
    .xml.parsers.SAXParser.parse(SAXParser.java:345)
    at 
    javax
    .xml.parsers.SAXParser.parse(SAXParser.java:143)
    at 
    SimpleSAXParser2
    .main(SimpleSAXParser2.java:98)
    Parsing will stop
    .
    Parsing 
    error
    :
    org
    .xml.sax.SAXParseExceptionExpected "</duration>" to 
    terminate element start
    ing on line 5.
    at 
    org
    .apache.crimson.parser.Parser2.fatal(Parser2.java:3182)
    at 
    org
    .apache.crimson.parser.Parser2.fatal(Parser2.java:3176)
    at 
    org
    .apache.crimson.parser.Parser2.maybeElement(Parser2.java:1513)
    at 
    org
    .apache.crimson.parser.Parser2.content(Parser2.java:1779)
    at 
    org
    .apache.crimson.parser.Parser2.maybeElement(Parser2.java:1507)
    at 
    org
    .apache.crimson.parser.Parser2.content(Parser2.java:1779)
    at 
    org
    .apache.crimson.parser.Parser2.maybeElement(Parser2.java:1507)
    at 
    org
    .apache.crimson.parser.Parser2.content(Parser2.java:1779)
    at 
    org
    .apache.crimson.parser.Parser2.maybeElement(Parser2.java:1507)
    at 
    org
    .apache.crimson.parser.Parser2.parseInternal(Parser2.java:500)
    at 
    org
    .apache.crimson.parser.Parser2.parse(Parser2.java:305)
    at 
    org
    .apache.crimson.parser.XMLReaderImpl.parse(XMLReaderImpl.java:442)
    at 
    javax
    .xml.parsers.SAXParser.parse(SAXParser.java:345)
    at 
    javax
    .xml.parsers.SAXParser.parse(SAXParser.java:143)
    at 
    SimpleSAXParser2
    .main(SimpleSAXParser2.java:98)

    Maybe you didn’t see that one coming, but indeed the error we have introduced was a fatal error – the duration tag not being closed destroyed the whole DOM model (after all, could YOU tell at this point based on this XML document whether the intention was to include another duration tag inside the duration tag, which in turn was meant to have another files, session and so on tags inside, or was it the case that we just wanted to append another duration tag after having closed the first one or is it simply that a /   is missing?) – which means in turn that parsing cannot continue anymore as the parser can’t figure out in which state it is.

    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 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek