Recursive, pre-order traversal
getFirstChild()
Indexed navigation is the key
No iterators; no siblings
import java.io.IOException;
import nu.xom.*;
public class TreeReporter {
public static void main(String[] args) {
if (args.length <= 0) {
System.out.println("Usage: java TreeReporter URL");
return;
}
TreeReporter iterator = new TreeReporter();
try {
Builder parser = new Builder();
// Read the entire document into memory
Node document = parser.build(args[0]);
// Process it starting at the root
iterator.followNode(document);
}
catch (IOException ex) {
System.out.println(ex);
}
catch (ParsingException ex) {
System.out.println(ex);
}
} // end main
private PropertyPrinter printer = new PropertyPrinter();
// note use of recursion
public void followNode(Node node) throws IOException {
printer.writeNode(node);
for (int i = 0; i < node.getChildCount(); i++) {
followNode(node.getChild(i));
}
}
}