HomeXML Page 3 - XML Parsing With SAX and Xerces (part 1)
Reaching For The Nailgun - XML
So you've already seen how Perl and PHP handle XML data. But you're a Real Programmer, and Real Programmers don't waste time with scripting languages. Nope, you need something a little more powerful, something with more horsepower under the hood. Something written in Java. Something like Xerces.
Let's begin with a simple XML file, which displays the marked-up inventory statement for a business selling equipment for Quake enthusiasts:
<?xml version="1.0"?>
<inventory>
<item>
<id>758</id>
<name>Rusty,
jagged nails for nailgun</name>
<supplier>NailBarn, Inc.</supplier>
<cost>2.99</cost>
<quantity>10000</quantity>
</item>
<item>
<id>6273</id>
<name>Power
pack for death ray</name>
<supplier>QuakePower.domain.com</supplier>
<cost>9.99</cost>
<quantity>10</quantity>
</item>
</inventory>
Now, we need a simple Java application that will initialize the SAX parser, read
and parse the XML file, and fire the callback functions as it encounters tags.
import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.*;
import java.io.*;
//
the ContentHandler interface handles all the callbacks
public class MyFirstSaxApp
implements ContentHandler {
// constructor
public MyFirstSaxApp (String xmlFile)
{
// create a Xerces SAX parser
SAXParser parser = new SAXParser();
// set the content handler
parser.setContentHandler(this);
//
parse the document
try {
parser.parse(xmlFile);
} catch
(SAXException e) {
System.err.println (e);
} catch (IOException
e) {
System.err.println (e);
}
}
// call this when a start
tag is found
public void startElement (String uri, String local, String qName,
Attributes
atts) {
System.out.println ("Found element: " + local);
}
// the remaining
callback handlers
// they don't do anything right now...but keep reading!
public
void setDocumentLocator(Locator locator) {}
public void startDocument() {}
public
void endDocument() {}
public void characters(char[] text, int start, int length){}
public
void startPrefixMapping(String prefix, String uri) {}
public void endPrefixMapping(String
prefix) {}
public void endElement(String namespaceURI, String localName, String
qualifiedName)
{}
public void ignorableWhitespace(char[] text, int start, int length) throws
SAXException
{}
public void processingInstruction(String target, String data){}
public void
skippedEntity(String name) {}
// everything starts here
public static void
main (String[] args) {
MyFirstSaxApp myFirstExample = new MyFirstSaxApp(args[0]);
}
}
Sure, it looks a little intimidating - but fear not, all will be explained shortly.
Before I get to that, though, it's instructive to see what the output of this looks like. So, how about we compile it and run it?
$ javac MyFirstSaxApp.java
Assuming that all goes well, you should now have a class file named "MyFirstSaxApp.class".
Copy this class file to your Java CLASSPATH, and then execute it, with the name of the XML file as argument.
$ java MyFirstSaxApp /home/me/sax/inventory.xml
And here's what you should see:
Found element: inventory
Found element: item
Found element: id
Found element:
name
Found element: supplier
Found element: cost
Found element: quantity
Found
element: item
Found element: id
Found element: name
Found element: supplier
Found
element: cost
Found element: quantity
What's happening here? Every time the parser encounters a start tag within the
XML document, it calls the startElement()function, which prints the name of the tag to the standard output device. The parser then moves on to the next construct within the document, calling the appropriate callback function to handle it. This process continues until the entire XML document has been processed.