<title section="Resin : XML Tutorial : ">
Scanning Elements
</title>

<p/>This Example extracts and print the "b" node in the following XML.

<example title="Input XML">
&lt;top>
  &lt;a/>
  &lt;b>
    &lt;b1/>
    &lt;b2/>
  &lt;/b>
  &lt;c/>
&lt;/top>
</example>

<p/>In XML, the document contains a single top element.  We'll just extract
that and store it.

<p/>Each DOM node has a <var/name/> and a <var/value/> retrieved using
<var/getNodeName()/> and <var/getNodeValue()/>.  For an Element, the
<var/name/> is the tag name and the <var/value/> is null.

<p/>Traversing nodes uses a combination of:
<ul>
<li><var/getFirstChild()/>
<li><var/getLastChild()/>
<li><var/getNextSibling()/>
<li><var/getPreviousSibling()/>
<li><var/getParentNode()/>
</ul>
Since those return a <var/Node/>, you'll often need to cast the results to
the correct type.  Remember, that the child nodes also includes <var/Text/>
nodes, including whitespace-only text nodes.  You can't assume that any
child is an Element, So you'll always need to test the node name or
the node type.

<p/>The following pattern is a common method for finding
a node as the child.

<example title="Finding Elements with the DOM">
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import com.caucho.xml.*;

...

// Create a new parser using the JAXP API (javax.xml.parsers)
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();

// Create a new document
Document doc = builder.parse("test.xml");

// Get the top element
Element top = doc.getDocumentElement();

// Find the B element
Node ptr;
for (ptr = top.getFirstChild();
     ptr != null && ! ptr.getNodeName().equals("b");
     ptr = ptr.getNextSibling()) {
}
</example>
