Navigating in XML Documents

As you see in Table 11-5, the Node interface contains all the standard W3C DOM methods for navigating in a document that we've already used with JavaScript in Chapter 7. This includes getNextSibling , getPreviousSibling , getFirstChild , getLastChild , and getParent . You can put those methods to work here as easily as in Chapter 7. For example, here's the XML document that we navigated through in Chapter 7, which I'll call ch11_08.xml here:

Listing ch11_08.xml
 <?xml version="1.0"?> <MEETINGS>    <MEETING TYPE="informal">        <MEETING_TITLE>XML In The Real World</MEETING_TITLE>        <MEETING_NUMBER>2079</MEETING_NUMBER>        <SUBJECT>XML</SUBJECT>        <DATE>6/1/2002</DATE>        <PEOPLE>            <PERSON ATTENDANCE="present">                <FIRST_NAME>Edward</FIRST_NAME>                <LAST_NAME>Samson</LAST_NAME>            </PERSON>            <PERSON ATTENDANCE="absent">                <FIRST_NAME>Ernestine</FIRST_NAME>                <LAST_NAME>Johnson</LAST_NAME>            </PERSON>            <PERSON ATTENDANCE="present">                <FIRST_NAME>Betty</FIRST_NAME>                <LAST_NAME>Richardson</LAST_NAME>            </PERSON>        </PEOPLE>    </MEETING> </MEETINGS> 

In Chapter 7, we navigated through this document to display the third person's name , and I'll do the same here. The main difference between the Java and the JavaScript implementations in this case is that the Java implementation treats all text as text nodesincluding the spacing used to indent meetings.xml. That means I can use essentially the same code to navigate through the document here that we used in Chapter 7, bearing in mind that we must step over the text nodes that contain only indentation text. Here's what that looks like in a program named ch11_09.java:

Listing ch11_09.java
 import javax.xml.parsers.*; import org.w3c.dom.*; public class ch11_09 {     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);             display(document);         } catch (Exception e) {             e.printStackTrace(System.err);         }     }     public static void display(Node node)     {         Node textNode;         Node meetingsNode = ((Document)node).getDocumentElement();         textNode = meetingsNode.getFirstChild();         Node meetingNode = textNode.getNextSibling();         textNode = meetingNode.getLastChild();         Node peopleNode = textNode.getPreviousSibling();         textNode = peopleNode.getLastChild();         Node personNode = textNode.getPreviousSibling();         textNode = personNode.getFirstChild();         Node first_nameNode = textNode.getNextSibling();         textNode = first_nameNode.getNextSibling();         Node last_nameNode = textNode.getNextSibling();         System.out.println("Third name: " +             first_nameNode.getFirstChild().getNodeValue() + ' '             + last_nameNode.getFirstChild().getNodeValue());     }     public static void main(String args[])     {         displayDocument("ch11_08.xml");     } } 

And here are the results of this program:

 %java ch11_09  Third name: Betty Richardson 


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