|
dear all,
we have encountered a problem with using the
treewalker.
not sure if it's something we're doing wrong but
the nextNode
method seems like it's not finding the end of the
document
and just keeps looping back to the start of the
doc.
if you remove any of the <c> elements in
lockup.xml then it all
seems to work and
nextNode correctly returns the <b> node.
i'd like to know if anyone can see what we're doing
wrong or
if this might be a bug.
usage of attached java program: java
Loop lockup.xml
any help much appreciated,
thanks
greg
platform: windows 2000, jdk1.2.2, and *not* using
hotspot.
|
import java.io.*; import org.w3c.dom.*; import org.apache.xerces.domx.traversal.*; import org.apache.xerces.dom.traversal.*; import org.apache.xerces.dom.*;
public class Loop {
private static Document readFile( String fileName ) throws Exception {
org.apache.xerces.parsers.DOMParser parser = new org.apache.xerces.parsers.DOMParser();
parser.parse( fileName );
Document doc = parser.getDocument();
return doc;
}
private static TreeWalker createTreeWalker( Node node, String nodeName ) {
org.apache.xerces.dom.DocumentImpl docImpl = (org.apache.xerces.dom.DocumentImpl)node.getOwnerDocument();
TreeWalker tw = docImpl.createTreeWalker( node, (short)NodeFilter.SHOW_ELEMENT, new ElementFilter( nodeName ));
return tw;
}
public static void main( String args[] ) {
try {
Document doc = readFile( args[0] );
TreeWalker tw = createTreeWalker( doc, "b" );
while( true ) {
System.out.print( "." );
Element el = (Element)tw.nextNode();
if( el == null ) break;
//DEBUG
System.out.println( "! " + el );
}
} catch( Exception e ) {
e.printStackTrace();
}
}
}
class ElementFilter implements NodeFilter {
private String name = null;
public ElementFilter() {
this(null);
}
public ElementFilter( String name ) {
this.name = name;
}
public short acceptNode( Node n ) {
// System.out.println( n.getNodeName());
if( n.getNodeName().equals( this.name ) && n.getNodeType() == Node.ELEMENT_NODE ) {
return FILTER_ACCEPT;
}
return FILTER_SKIP;
}
}
<a>
<b>
<c>
<d>
<e/>
</d>
<f>
<g/>
<g/>
</f>
<h>
<i/>
</h>
<j>
<k/>
<k/>
</j>
</c>
<c>
<d>
<e/>
</d>
<f>
<g/>
<g/>
</f>
<h>
<i/>
</h>
<j>
<k/>
</j>
</c>
<c>
<d>
<e/>
</d>
<l>
<m/>
</l>
<f>
<g/>
</f>
<h>
<i/>
</h>
<j>
<k/>
</j>
</c>
<c>
<d>
<e/>
</d>
<l>
<m/>
</l>
<f>
<g/>
<g/>
</f>
<h>
<i/>
</h>
<j>
<k/>
</j>
</c>
<c>
<d>
<e/>
<e/>
</d>
<j>
<k/>
<k/>
</j>
</c>
</b>
<n/>
</a>
