You only print the "indent" (actually a lot of numerics) once, at the start of the node's first line.
You then print children with a newline after each. The "indent" will only precede the first child.
You also don't print the node itself you are visiting, but only print the children. This is erroneous, and what with the node itself not showing up/ just a spurious indent, output will be very confusing when there are no children.
Additionally, you iterate over treeNodes
and call into the printChildNodes
function -- but that is recursive. Ideally I would only expect one recursive function to print & descend the entire subtree, with no loop.
printTree( rootNode, 0);
private static void printTree (xmlNode n, int indent) {
StringBuilder ind = new StringBuilder();
for (int i = 0; i < indent; i++)
ind.append(' ');
// print the Node;
//
System.out.println( ind + n.getLabel());
// traverse Descendants.
//
for (xmlNode child : n.getChildren()) {
printTree( child, indent+1);
}
}
This should be pretty foolproof.