You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by "Adam L. Menkes" <am...@mindless.com> on 2000/05/27 08:23:47 UTC

Traversal help

Here is a simple traversal method, that works fine with a Document node, but
fails with any other node type:
works:        traverse(doc);
fails:        traverse(doc.getFirstChild());

I was under the impression that there was a way to begin traversing from a
specific node so you don't have to walk the whole document. What am I doing
wrong?

  public static void traverse(Node node) {
    TreeWalker tw = ((DocumentTraversal)node).createTreeWalker(node,
NodeFilter.SHOW_ALL, null, false);
    Node nextNode = tw.nextNode();
    while (nextNode != null) {
      System.out.println("\nName: "  + nextNode.getNodeName() +
                         " Type: "  + nextNode.getNodeType() +
                         " Value: " + nextNode.getNodeValue());
      nextNode = tw.nextNode();
  }