CHAPTER 9 ■ WRITING AND READING XML
// Deal with the possible exceptions } catch(IOException ioe) {
System.out.println("Couldn't open " + fileName + " for parsing");
} catch(ParserConfigurationException pce) {
System.out.println("Failed to create a SAX parser ");
} catch(SAXException saxe) {
System.out.println("Failed to parse an XML file");
}
}
}
Simple, isn't it? One of the joys of SAX is that it's simple to implement. Create a handler, create a
parser, and pass your input and handler to the factory. Consequently, the exception-handling code is
longer than the code that does the work. Of course, this arrangement is really masking the fact that the
complexity is in the handler class. Still, carving up your code so that complexity is isolated to a single
class is exactly the right way to use an object-oriented language such as Java.
A Word about Factory Classes
You may have noticed that to get a DocumentBuilder object, you had to use a DocumentBuilderFactory
object. Similarly, to get a SAXParser object, you had to use a SAXParserFactory object. The factory pattern
is often used in Java (and other object-oriented languages, such as C++) to permit the creation of objects
that have varying attributes. For example, the SAXParserFactory class includes a way to specify a
separate validator object, to ensure that the XML conforms to a schema (which is a definition of what a
set of data should contain).
The factory pattern is a handy way to present a group of very similar objects that vary only by having
some features turned on or off. Otherwise, you’d have to have a class for every possible combination of
features. In some cases, that would be a lot of very similar classes. The factory pattern offers an easy-to-
use and easy-to-understand solution to that problem. Consequently, Java has a number of factory
objects in its standard libraries. You can also create your own factory classes. When you find yourself
needing to create many very similar (but slightly different) objects, consider creating a factory for them.
Summary
Well, that was a whirlwind tour of how to use Java to work with XML. Several good books have been
written about the subject, as there's a great deal more complexity than what’s been presented here. Still,
this chapter should prepare you for when you have to read and write XML files or streams.
In particular, we covered the basics of Java's two main ways to deal with XML: the Document Object
Model (DOM) and the Simplified API for XML (SAX). You learned that DOM offers great performance
because it loads the entire document into memory. However, loading the entire document into memory
is also DOM's biggest problem, as large documents may exceed the available memory. SAX, on the other
hand, can handle any amount of XML (including gigabyte-size streams of data), but it requires making a
custom handler class for each kind of XML document.
Finally, you looked at the nature of factory classes and learned about why they exist, and when you
might want to use them.