Recursive interate and skip some tags #154
-
I need interate over xml, but, every time i found an specific tag, i need skip all this tag and this tag childs. I try to use How can i do this? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Probably the easiest is to verify that none of the ancestors of your matches also satisfy the condition: final nodes = document.descendants
// Find all the nodes that satisfy the condition.
.where((node) => predicate(node))
// Exclude the nodes that have parents satisfying the condition.
.where((node) => !node.ancestors.any(predicate)); If you are only interested in elements, you might want to replace |
Beta Was this translation helpful? Give feedback.
-
A more manual way would be to traverse the tree recursively: List<XmlNode> find(XmlNode node, bool Function(XmlNode) predicate) {
if (predicate(node)) {
// Return a matching node, ...
return [node];
} else {
// otherwise recurse into the children.
return [
...node.attributes.expand((child) => find(child, predicate)),
...node.children.expand((child) => find(child, predicate)),
];
}
} This code could be further transformed into an iterative solution, and eventually into a generally reusable |
Beta Was this translation helpful? Give feedback.
Probably the easiest is to verify that none of the ancestors of your matches also satisfy the condition:
If you are only interested in elements, you might want to replace
descendants
andancestors
withdescendantElements
andancestorElements
. Also you might want to combine thewhere
clauses into a single expression.