Java
  Home arrow Java arrow Page 3 - Advanced SAX
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? 
JAVA

Advanced SAX
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2007-07-11

    Table of Contents:
  • Advanced SAX
  • Setting Properties and Features
  • Error Handling
  • Resolving Entities

  • 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


    Advanced SAX - Error Handling


    (Page 3 of 4 )

    Invoking the setFeature() and setProperty() methods can result in SAXNotSupportedException s and SAXNotRecognizedException s.

    Both of these are also in the org.xml.sax package.

    The first, SAXNotSupportedException , indicates that the parser “knows” about the feature or property but doesn’t support it. This is commonly used when a standard property or feature is not yet coded in (such as in alpha or beta versions of parsers). So invoking setFeature("http://xml.org/sax/features/namespaces") on a parser in development might result in a SAXNotSupportedException . The parser recognizes the feature (and probably plans to support it at some point), but doesn’t have the ability to perform the requested processing.

    The second exception, SAXNotRecognizedException , commonly occurs when your code uses vendor-specific features and properties, and then you switch out your parser implementations. The new implementation won’t know anything about the other vendor’s features or properties, and will throw a SAXNotRecognizedException .

    You should always explicitly catch these exceptions so you can report them, rather than treating them as just another generic SAXException . Otherwise, you end up losing valuable information about what happened in your code. This means you may have to write a bit of extra code, but thus is the price for good exception handling; here’s a slightly updated version of the buildTree() method (detailed originally in Chapter 3) that handles these problems gracefully:

    public void buildTree(DefaultTreeModel treeModel ,
                          DefaultMutableTreeNode base, String xmlURI)
      throws IOException, SAXException {

      String featureURI = "";
      XMLReader reader = null;

      try {
        // Create instances needed for parsing
        reader = XMLReaderFactory.createXMLReader(); 
        JTreeHandler jTreeHandler =
         
    new JTreeHandler(treeModel, base);

        // Register content handler
        reader.setContentHandler(jTreeHandler);

        // Register error handler
        reader.setErrorHandler(jTreeHandler);

        // Turn on validation
        featureURI =
    http://xml.org/sax/features/validation;
        reader.setFeature(featureURI, true);

        // Turn on schema validation, as well
        featureURI =
    http://apache.org/xml/features/ validation/schema;
        reader.setFeature(featureURI, true);

        // Parse
        InputSource inputSource = new InputSource(xmlURI);
        reader.parse(inputSource);
      } catch (SAXNotRecognizedException e) {
        System.err.println("The parser class " + reader.getClass().getName() +
          " does not recognize the feature URI '" + featureURI + "'");
        System.exit(-1);
      } catch (SAXNotSupportedException e) {
        System.err.println("The parser class " + reader.getClass().getName() +
          " does not support the feature URI '" + featureURI + "'");
        System.exit(-1);
      }
    }

    More Java Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Java and XML, Third Edition," published by...
     

    Buy this book now. This article is excerpted from chapter four of the book Java and XML, Third Edition, written by Brett McLaughlin and Justin Edelson (O'Reilly, 2006; ISBN: 059610149X). Check it out today at your favorite bookstore. Buy this book now.

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







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