Filtering XML Documents

The previous example displayed the entire document, but you can be more selective than that through a process called filtering. When you filter a document, you extract only those elements that you're interested in.

Here's an example named searcher.java. In this case, I'll let the user specify what document to search and what element name to search for like this, which will display all <ITEM> elements in customer.xml:

%java searcher customer.xml ITEM

I'll start this program by creating a new class, FindElements, to make the programming a little easier. All I have to do is to pass the document to search and the element name to search for to the constructor of this new class:

import org.w3c.dom.*;
import org.apache.xerces.parsers.DOMParser;

public class searcher
{
    public static void main(String args[])
    {
        FindElements findElements = new FindElements(args[0], args[1]);
    }
}

In the FindElements class constructor, I'll save the name of the element to search for in a string named searchFor and then call the displayDocument method as in the previous example to display the document. That method will fill the displayStrings array with the output strings, which we print:

class FindElements
{
    static String displayStrings[] = new String[1000];
    static int numberDisplayLines = 0;
    static String searchFor;

    public FindElements (String uri, String searchString)
    {

        searchFor = searchString;
        displayDocument(uri);

        for(int loopIndex = 0; loopIndex < numberDisplayLines; loopIndex++){
            System.out.println(displayStrings[loopIndex]);
        }
    }s

In the displayDocument method, we want to display only the elements with the name that's in the searchFor string. To find those elements, I use the getElementsByTagName method, which returns a node list of matching elements. I loop over all elements in that list, calling the display method to display each element and its children:

public static void displayDocument(String uri)
{
    try {
        DOMParser parser = new DOMParser();
        parser.parse(uri);
        Document document = parser.getDocument();

        NodeList nodeList = document.getElementsByTagName(searchFor);

        if (nodeList != null) {
            for (int loopIndex = 0; loopIndex < nodeList.getLength();
                loopIndex++ ) {
                display(nodeList.item(loopIndex), "");
            }
        }
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }
}

The display method is the same as in the previous example.

That's all it takes; here I search customer.xml for all <ITEM> elements:

%java searcher customer.xml ITEM | more

You can see the results in Figure 11.2. The complete code for searcher.java is in Listing 11.2.

Figure 11.2. Filtering an XML document.


Code Listing 11.2. searcher.java
import org.w3c.dom.*;
import org.apache.xerces.parsers.DOMParser;

public class searcher
{
    public static void main(String args[])
    {
        FindElements findElements = new FindElements(args[0], args[1]);
    }
}

class FindElements
{
    static String displayStrings[] = new String[1000];
    static int numberDisplayLines = 0;
    static String searchFor;

    public FindElements (String uri, String searchString)
    {
        searchFor = searchString;
        displayDocument(uri);

        for(int loopIndex = 0; loopIndex < numberDisplayLines; loopIndex++){
            System.out.println(displayStrings[loopIndex]);
        }
    }

    public static void displayDocument(String uri)
    {
        try {
            DOMParser parser = new DOMParser();
            parser.parse(uri);
            Document document = parser.getDocument();

            NodeList nodeList = document.getElementsByTagName(searchFor);

            if (nodeList != null) {
                for (int loopIndex = 0; loopIndex < nodeList.getLength();
                    loopIndex++ ) {
                    display(nodeList.item(loopIndex), "");
                }
            }
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
    }

    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 attrs[] = new Attr[length];
                 for (int loopIndex = 0; loopIndex < length; loopIndex++) {
                     attrs[loopIndex] =
                     (Attr)node.getAttributes().item(loopIndex);
                 }

                 for (int loopIndex = 0; loopIndex < attrs.length;
                     loopIndex++) {
                     Attr attr = attrs[loopIndex];
                     displayStrings[numberDisplayLines] += " ";
                     displayStrings[numberDisplayLines] += attr.getNodeName();
                     displayStrings[numberDisplayLines] += "="";
                     displayStrings[numberDisplayLines] +=
                         attr.getNodeValue();
                     displayStrings[numberDisplayLines] += """;
                 }
                 displayStrings[numberDisplayLines] += ">";

                 numberDisplayLines++;

                 NodeList childNodes = node.getChildNodes();
                 if (childNodes != null) {
                     length = childNodes.getLength();
                     indent += "    ";
                     for (int loopIndex = 0; loopIndex < length; loopIndex++ ) {
                        display(childNodes.item(loopIndex), indent);
                     }
                 }
                 break;
             }

             case Node.CDATA_SECTION_NODE: {
                 displayStrings[numberDisplayLines] = indent;
                 displayStrings[numberDisplayLines] += "<![CDATA[";
                 displayStrings[numberDisplayLines] += node.getNodeValue();
                 displayStrings[numberDisplayLines] += "";
                 numberDisplayLines++;
                 break;
             }

             case Node.TEXT_NODE: {
                 displayStrings[numberDisplayLines] = indent;
                 String newText = node.getNodeValue().trim();
                 if(newText.indexOf("
") < 0 && newText.length() > 0) {
                     displayStrings[numberDisplayLines] += newText;
                     numberDisplayLines++;
                 }
                 break;
             }

             case Node.PROCESSING_INSTRUCTION_NODE: {
                 displayStrings[numberDisplayLines] = indent;
                 displayStrings[numberDisplayLines] += "<?";
                 displayStrings[numberDisplayLines] += node.getNodeName();
                 String text = node.getNodeValue();
                 if (text != null && text.length() > 0) {
                     displayStrings[numberDisplayLines] += text;
                 }
                 displayStrings[numberDisplayLines] += "?>";
                 numberDisplayLines++;
                 break;
            }
        }

        if (type == Node.ELEMENT_NODE) {
            displayStrings[numberDisplayLines] = indent.substring(0,
                indent.length() - 4);
            displayStrings[numberDisplayLines] += "</";
            displayStrings[numberDisplayLines] += node.getNodeName();
            displayStrings[numberDisplayLines] += ">";
            numberDisplayLines++;
            indent+= "    ";
        }
    }
}

The examples we've created so far have all created text-based output using the System.out.println method. However, few browsers these days work that way. In the next section, I'll take a look at creating a windowed browser.

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

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