Skip to main content

If you don't have an IBM ID and password, register here.

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. This profile includes the first name, last name, and display name you identified when you registered with developerWorks. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

Tip: Output large XML documents, Part 2

XMLFilter: Filtering XML data using SAX

Brett McLaughlin (brett@oreilly.com), Author, O'Reilly and Associates
Photo of Brett McLaughlin
Brett McLaughlin has been working in computers since the Logo days (Remember the little triangle?). He currently specializes in building application infrastructure using Java-related technologies. He has spent the last several years implementing these infrastructures at Nextel Communications and Allegiance Telecom, Inc. Brett is one of the co-founders of the Java Apache project Turbine, which builds a reusable component architecture for Web application development using Java servlets. He is also a contributor of the EJBoss project, an open source EJB application server, and Cocoon, an open source XML Web-publishing engine.

Summary:  This tip begins to detail ways to handle large XML documents. You will learn what an XMLFilter is, and how it builds upon the core SAX API to offer advanced data filtering. This is the first piece in the puzzle of handling large datasets, allowing you to extract only relevant data from an XML document for output.

View more content in this series

Date:  02 Apr 2003
Level:  Intermediate

Comments:  

In the last tip, I examined several popular options for handling and ultimately outputting large XML datasets. It quickly became apparent that SAX, DOM, JAXP, JDOM, dom4j, and alternative XML APIs (particularly in-memory ones) were all poorly suited to dealing with giant XML datasets -- ones where records numbered in the tens of thousands. The only option that did work was to output characters to raw I/O streams -- and this solution had its own set of problems: It doesn't accurately represent XML, it's error-prone, and you can't navigate the XML tree.

In this tip, I begin the process of laying out a better option, albeit one that is little-understood and rarely used. However, like teaching any new API, it's going to take a little bit of time -- I'll lay out the basics in this installment, and then show you some practical applications of filters. Then you'll look at the basics of XMLWriters, and finally apply that API as well. When finished with these first five tips, you'll be equipped to filter out irrelevant data, and write out the data that is left to you, all within an XML-based framework.

The XMLFilter class

The class that I focus on in this tip is part of the SAX distribution, although it's generally overlooked or flat-out ignored by most developers. Get a distribution of SAX, and look at the included classes; the one you want is org.xml.sax.XMLFilter. You should also ensure that you have the SAX helper classes, found in the org.xml.sax.helpers package. If you don't, you should be able to get these from the SAX project online (see Resources). In that package, you will want to focus on the org.xml.sax.helpers.XMLFilterImpl class. The XMLFilter interface and the XMLFilterImpl implementation of that interface pair add up to powerful filtering for your SAX-based applications.

If you crack open the source for XMLFilter, you'll find that it extends the org.xml.sax.XMLReader interface, and adds two new methods:

  • public void setParent(XMLReader parent);
  • public XMLReader getParent();

This probably doesn't look like much; of course, you also get all the other XMLReader methods like startElement(), endPrefixMapping(), and so on. In each of these methods, you can operate upon the input XML data before an application gets to it.

To put this in perspective, remember that application code doesn't start to work on the XML data from a SAX parse until after that parsing has completed. However, you can insert an XMLFilter into the processing chain before this completion, meaning you get to modify data before the application gets that data (and, for example, outputs it). Since you have all of the SAX callback methods that an XMLReader does, you can work with the elements, the attributes, the prefix mappings, and anything else that SAX can work with.


A simple filter

Let's look at a concrete example of an XMLFilter, so you can start to get an idea of what I'm talking about. Listing 1 shows a very simple SAX filter that removes all of the elements and attributes with a given URI from the input document. This effectively strips out all XML constructs within the given namespace.


Listing 1. A simple XMLFilter
                
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLFilterImpl;

public class RemoveNamespaceFilter extends XMLFilterImpl {

  /** The URI of the namespace to remove */
  private String namespaceURI;

  public RemoveNamespaceFilter(XMLReader reader, String namespaceURI) {
    super(reader);
    this.namespaceURI = namespaceURI;
  }

  public void startElement(String uri, String localName, String qName,
                           Attributes atts)
    throws SAXException {

    // If this element is in the namespace to remove, skip it
    if (namespaceURI.equals(uri)) {
      // Do nothing, which skips this element
    } else {
      // Make a copy that we can modify
      org.xml.sax.helpers.AttributesImpl newAtts =
        new org.xml.sax.helpers.AttributesImpl();

      // Remove (don't add to the new list) any attribute with the given name
      for (int i=0; i<atts.getLength(); i++) {
        String attURI = atts.getURI(i);
        if (attURI.equals(namespaceURI)) {          // skip it
        } else {
          newAtts.addAttribute(attURI, atts.getLocalName(i),
            atts.getQName(i), atts.getType(i), atts.getValue(i));
        }
      }
       
      // Delegate to the normal parsing behavior
      parent.startElement(uri, localName, qName, newAtts);
    }
  }

  public void endElement(String uri, String localName, String qName) 
    throws SAXException {

    if (namespaceURI.equals(uri)) {
      // Do nothing, which skips this element
    } else {
      // Delegate to normal parsing behavior
      parent.endElement(uri, localName, qName);
    }
  }

First, notice that this class extends XMLFilterImpl instead of directly implementing XMLFilter. XMLFilterImpl provides a default version of all the required methods, allowing you to implement (override) only those methods that have customized behavior. This keeps your code cleaner, and requires less work on your part.

The key thing to understand here is how data is affected. If you keep in mind that this filter gets the input data before the registered XMLReader, it's all a piece of cake. This means that if you want the data to be handled by the registered reader, you simply delegate to it. For example, your startElement() method would simply call its parent's version of the same element, much like a constructor calls super(). In fact, all of the default methods in XMLFilterImpl() do just that. Listing 2 shows a couple of methods just so you get the idea.


Listing 2. Sample methods from XMLFilterImpl
                
	public void startElement(String uri, String localName, String qName, 
	Attributes atts) throws SAXException {
		parent.startElement(uri, localName, qName, atts);
	}
	
	public void endElement(String uri, String localName, String qName) 
	throws SAXException {
		parent.endElement(uri, localName, qName);
	}

This just passes on the data, unchanged, to the reader. However, as you saw in Listing 1, you can insert your own logic in these methods, overriding the default versions. To avoid the data being seen by the reader, simply avoid delegating to the reader's methods. In other words, a method with no body effectively ignores all content that the method deals with. However, be careful in these situations -- it is easy to pollute the data being sent back to the reader. For example, consider the case where you don't delegate in the startElement() method for certain data, but forget to do the same in endElement(). The result would be that some elements would never be reported as starting, but would be reported to the reader as ending. This would cause, in the best case, program errors, and in the worst case, data loss or corruption in your application.


Setting up a processing chain

Once you have your filter set up and compiled, you need to create a chain of processing. This should move from input document to filter to reader. You may even have multiple filters, stacked upon each other. As long as input comes first, and your reader (with application-specific callbacks) comes last, things work fine. However, you may have a particular order for your filters, and you should pay attention to that closely. Listing 3 shows how to set up your program for using filters.


Listing 3. Setting the processing chain
                
XMLReader reader = XMLReaderFactory.createXMLReader(vendorParserClass);
RemoveNamespaceFilter filter = new 
    RemoveNamespaceFilter(reader, "http://www.ibm.com/developer");

ContentHandler contentHandler = new ApplicationContentHandler(someDataParameter);
ErrorHandler errorHandler = new 
    ApplicationErrorHandler(someOutputStream, someErrorThreshold);

filter.setContentHandler(contentHandler);
filter.setErrorHandler(errorHandler);

InputSource inputSource = new InputSource(myXmlUri);
filter.parse(inputSource);

Notice that because the one or more filters must sit between input and the reader, all the operations that you would normally invoke on the reader are invoked on the filter. It then delegates any data that passes through the filter to the reader, as you saw in Listing 1. Obviously, you'll need to use your own variables in Listing 3, including content handlers, error handlers, entity resolvers, lexical handlers, and so forth -- however, I've left in dummy names to give you an idea how things fit together.

Between Listing 1, Listing 3, and this discussion, you should have enough information to write your own filters. I encourage you to do so, as SAX is already a bit of a tricky API, and filters sometimes add to the confusion. Get familiar with filters before moving on to the more advanced material in the upcoming tips.


Theory to practice

So far, I've dealt with XMLFilters largely as a matter of theory. You've seen what filters are, how the relevant SAX classes work, and how to attach filters to input chains when reading and parsing XML. However, this probably doesn't help you in cutting down on the data you have to output. The next tip will address that topic specifically, showing some example filters that will pull out attributes in a document, or pull out elements, remove namespaces, and a lot more. So hang in there for a few days, get comfortable with filters, and you'll put them to work in the next tip. Until then, I'll see you online.


Resources

About the author

Photo of Brett McLaughlin

Brett McLaughlin has been working in computers since the Logo days (Remember the little triangle?). He currently specializes in building application infrastructure using Java-related technologies. He has spent the last several years implementing these infrastructures at Nextel Communications and Allegiance Telecom, Inc. Brett is one of the co-founders of the Java Apache project Turbine, which builds a reusable component architecture for Web application development using Java servlets. He is also a contributor of the EJBoss project, an open source EJB application server, and Cocoon, an open source XML Web-publishing engine.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in

If you don't have an IBM ID and password, register here.


Forgot your IBM ID?


Forgot your password?
Change your password


By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. This profile includes the first name, last name, and display name you identified when you registered with developerWorks. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)


By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=XML
ArticleID=12248
ArticleTitle=Tip: Output large XML documents, Part 2
publish-date=04022003
author1-email=brett@oreilly.com
author1-email-cc=

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).