Home arrow Java arrow Page 3 - Extension Interfaces and SAX

Extension Interfaces and SAX

In this conclusion to a three-part article, we look at extension interfaces, XML filters, and more as they relate to the Simple API for XML (SAX). 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). 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: 3 stars3 stars3 stars3 stars3 stars / 6
July 19, 2007
  1. · Extension Interfaces and SAX
  2. · Extension Interfaces
  3. · XMLFilters continued
  4. · XMLWriter

print this article

Extension Interfaces and SAX - XMLFilters continued
(Page 3 of 4 )

Passing an XMLReader instance to the constructor sets that reader as its parent, so the parent reader receives any events passed on from the filter (which is all events, by virtue of the XMLFilterImpl class, unless the NamespaceFilter class overrides that behavior). By supplying two URIs—the URI to be replaced, and the URI to replace that old one with—your filter is ready to use. The three overridden methods handle any needed interchanging of that URI. Once you have a filter like this in place, you supply a reader to it, and then operate upon the filter, not the reader. For example, suppose that the SAXTreeViewer application is used to display XML versions of O’Reilly books, and the O’Reilly namespace URI for these books is universally being changed from http://www.oreilly.com to http://safari.oreilly.com . In that case, you could use the filter like this:

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

      NamespaceFilter filter = new NamespaceFilter(reader,                

      // Register content handler

      // Register error handler

      // Register entity resolver 
.setEntityResolver(new SimpleEntityResolver());

      // Register lexical handler

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

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

      // Parse

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

Of course, you can chain these filters together as well, and use them as standard libraries. When I’m dealing with older XML documents, I often create several of these with old XSL and XML Schema URIs and put them in place so I don’t have to worry about incorrect URIs:

  XMLReader reader = XMLReaderFactory.createXMLReader(vendorParserClass);
NamespaceFilter xslFilter = new NamespaceFilter(reader, 
  NamespaceFilter xsdFilter = new NamespaceFilter(xslFilter,

Here, I’m building a longer pipeline to ensure that no old namespace URIs sneak by and cause my applications any trouble.

Be careful not to build too long a pipeline; each new link in the chain adds some processing time. All the same, this is a great way to build reusable components for SAX.

blog comments powered by Disqus

- 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 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials