Chapter 12. Java and SAX

The previous chapter was all about using Java and the XML DOM. Some people, however, find using the DOM difficult and see the whole concept of treating an XML document as a tree unnecessarily complex. Rather than having to navigate through the whole document, they say, wouldn't it be great if the whole document came to you? That's the idea behind the Simple API for XML (SAX), and this chapter is dedicated to it. SAX really is a lot easier to use for many—possibly even most—XML parsing that you have to do.

You may be surprised to learn that we've already been putting the idea behind SAX to work throughout the entire previous chapter. You may recall that in that chapter, I set up a recursive method named display that was called for every node in the DOM tree. In display, I used a switch statement to make things easier. That switch statement had case statements to handle different types of nodes:

public static void display(Node node, String indent)
{
    if (node == null) {
        return;
    }

    int type = node.getNodeType();

    switch (type) {
        case Node.DOCUMENT_NODE: {
            displayStrings[numberDisplayLines] = indent;
            displayStrings[numberDisplayLines] +=
                "<?xml version="1.0" encoding=""+
                "UTF-8" + ""?>";
            numberDisplayLines++;
            display(((Document)node).getDocumentElement(), "");
            break;
         }

         case Node.ELEMENT_NODE: {
             displayStrings[numberDisplayLines] = indent;
             displayStrings[numberDisplayLines] += "<";
             displayStrings[numberDisplayLines] += node.getNodeName();

             int length = (node.getAttributes() != null) ?
                 node.getAttributes().getLength() : 0;
             Attr attributes[] = new Attr[length];
             for (int loopIndex = 0; loopIndex < length; loopIndex++) {
                 attributes[loopIndex] = (Attr)node.getAttributes().item(loopIndex);
             }
             .
             .
             .

I was able to add the code that handled elements to one case statement, the code to handle processing instructions to another case statement, and so on.

In essence, we were handling XML documents the same way that SAX does. Instead of navigating through the document ourselves, we let the document come to us, having the code call various case statements for the various nodes in the document. That's what SAX does. It's event-based, which means that when the SAX parser encounters an element, it treats that as an event and calls the code that you specify should be called for elements; when it encounters a processing instruction, it treats that as an event and calls the code that you specify should be called for processing instructions, and so on. In this way, you don't have to navigate through the document yourself—it comes to you. The fact that we've already based a significant amount of programming on this technique indicates how useful it is.

The XML for Java package from alphaWorks that we used in the previous chapter (http://www.alphaworks.ibm.com/tech/xml4j) also supports SAX. That means you can use the same JAR files in this chapter that we used in the previous chapter; just make sure that they're added to your CLASSPATH like this in Windows (and make the command all one line):

C:>SET
CLASSPATH=%CLASSPATH%;C:xmlparserXML4J_3_0_1xerces.jar;
C:xmlparserXML4J_3_0_1xercesSamples.jar

Or, use the -classpath switch, as discussed in the previous chapter:

%javac -classpath C:xmlparserXML4J_3_0_1xerces.jar;
C:xmlparserXML4J_3_0_1xercesSamples.jar browser.java
%java -classpath C:xmlparserXML4J_3_0_1xerces.jar;
C:xmlparserXML4J_3_0_1xercesSamples.jar browser

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.121.251