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 you're interested in.

Here's an example named ch11_04.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 ch11_01.xml:

 %java ch11_04 ch11_01.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 pass the document to search and the element name to search for to the contructor of this new class:

 import javax.xml.parsers.*;  import org.w3c.dom.*; public class ch11_04 {     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 call the displayDocument method as we did 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]);   }  } 

In the displayDocument method, we want to display only the elements with the name 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 {        DocumentBuilderFactory dbf =        DocumentBuilderFactory.newInstance();        DocumentBuilder db = null;        try {            db = dbf.newDocumentBuilder();        }        catch (ParserConfigurationException pce) {}        Document document = null;            document = db.parse(uri);  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 ch11_01.xml for all <ITEM> elements:

 %java ch11_04 ch11_01.xml ITEM   more 

You can see the results in Figure 11-2.

Figure 11-2. Filtering an XML document.

graphics/11fig02.gif

Here's the complete code for ch11_04.java:

Listing ch11_04.java
 import javax.xml.parsers.*; import org.w3c.dom.*; public class ch11_04 {     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 {             DocumentBuilderFactory dbf =             DocumentBuilderFactory.newInstance();             DocumentBuilder db = null;             try {                 db = dbf.newDocumentBuilder();             }             catch (ParserConfigurationException pce) {}             Document document = null;                 document = db.parse(uri);             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(). graphics/ccc.gif 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("\n") < 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 topic, I'll take a look at creating a windowed browser.



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net