dear all,
 
we're having some wierd stuff happening with treewalkers and the nextnode method.
 
i'm printing which nodes the nodefilter (see attached) gets passed and it seems like something is going wrong.
 
can anyone see what we might be doing wrong? or is this a bug?
 
the funny behaviour *only* seems to happen when we try to find a node that doesn't exist in the xml document, e.g. search for an element called "fred" in the document shown below.
 
input xml doc.
 
<a>
  <b/>
  <c/>
  <f>
    <g>
      <h>
        <i>
          <j/>
        </i> 
      </h>
    </g>
  </f>
</a>
 
output from elementfilter, which does a println as the first line in it's accept method.
 
b
c
f
g
h
i
j
i
h
g
f
a   <= i'm ok down to here. what's the rest of the stuff though?
h
g
f
a
g
f
a
f
a
a
 
 
the treewalker is created with the following code.
 
  org.apache.xerces.dom.DocumentImpl impl = (org.apache.xerces.dom.DocumentImpl)node.getOwnerDocument();
  TreeWalker tw = impl.createTreeWalker( node, (short)NodeFilter.SHOW_ELEMENT, new ElementFilter( nodeName ));
 
thanks,
greg.
 
package com.traveltech.shared;

import org.apache.xerces.dom.*;
import org.w3c.dom.*;
import org.w3c.dom.traversal.*;


/**
 * Base filter used to traverse a DOM
 */
public class ElementFilter implements NodeFilter {

	private String name = null;


	public ElementFilter() {

    	this(null);
    }


	public ElementFilter( String name ) {

		this.name = name;
	}


	public short acceptNode( Node n ) {

		if(( name == null || n.getNodeName().equals( this.name ) ) && n.getNodeType() == Node.ELEMENT_NODE ) {
			return FILTER_ACCEPT;
		}
		return FILTER_SKIP;
	}


	public void setName( String name ) {

		this.name = name;
	}
}

Reply via email to