Home arrow Java arrow Page 3 - Advanced SAX
JAVA

Advanced SAX


This article, the first of three parts, takes a look at the Simple API for XML (SAX) that goes beyond basic parsing and content handling. It 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). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 4 stars4 stars4 stars4 stars4 stars / 4
July 11, 2007
TABLE OF CONTENTS:
  1. · Advanced SAX
  2. · Setting Properties and Features
  3. · Error Handling
  4. · Resolving Entities

print this article
SEARCH DEVARTICLES

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);
  }
}


blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- 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 ...

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