So far I understood that when I parse an XML as a DOM object it is represented as a tree. I'm trying to get a list of all the paths from root to any leaf of my XML document (n-ary tree) with the TreeWalker, however I'm wondering if I should do my own implementation or there's any existing implementation defined. I found none in the official documentation.
Given the xml document:
<node id="A">
<node id = "AA">
<node id = "AAA"></node>
</node>
<node id = "AB">
</node>
<node id = "AC">
</node>
</node>
The expected list should contain:
A, AA, AAA
A, AB
A, AC
i would use some algorithm like this:
go down in tree with one function and if you have no child, go up and output every parent node.
function deep(node n){
if(n has childs)
foreach(child c) {
deep(c);
}
else goup(n);
}
function goup(node n){
if(node has no parent) echo n.id
else echo goup(n.parent()) . ", " . n.id
}
Non-recursive TreeWalker
It's fairly simple. Find all leaf elements, then display path by walking the parent chain.
NodeFilter leafElements = new NodeFilter() {
@Override
public short acceptNode(Node node) {
for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling())
if (child.getNodeType() == Node.ELEMENT_NODE)
return NodeFilter.FILTER_SKIP;
return NodeFilter.FILTER_ACCEPT;
}
};
TreeWalker walker = ((DocumentTraversal)document).createTreeWalker(document.getDocumentElement(),
NodeFilter.SHOW_ELEMENT,
leafElements,
false);
for (Element leaf; (leaf = (Element)walker.nextNode()) != null; ) {
Deque<String> path = new ArrayDeque<>();
for (Node node = leaf; node.getNodeType() == Node.ELEMENT_NODE; node = node.getParentNode())
path.addFirst(((Element)node).getAttribute("id"));
System.out.println(path);
}
Output
[A, AA, AAA]
[A, AB]
[A, AC]
Simple Recursion
Also fairly simple. Recurse descendants, maintaining path so far, and print for leaf elements.
showLeafPaths(document.getDocumentElement(), new StringBuilder());
private static void showLeafPaths(Element elem, StringBuilder path) {
final int pathLen = path.length();
if (pathLen != 0)
path.append(", ");
path.append(elem.getAttribute("id"));
boolean hasChild = false;
for (Node child = elem.getFirstChild(); child != null; child = child.getNextSibling())
if (child.getNodeType() == Node.ELEMENT_NODE) {
hasChild = true;
showLeafPaths((Element)child, path);
}
if (! hasChild)
System.out.println(path);
path.setLength(pathLen);
}
Output
A, AA, AAA
A, AB
A, AC
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.