Free Support Forum -

Manipulating Tree during TreeTraversal

Hi there
I need to manipulate the document tree (append nodes, remove nodes, replace nodes) while traversing it (using a DocumentVisitor) and I am wondering what pitfalls I have to be aware of doing this.

An example:
If I append a new Node to the parent of the current Node (within traversal), will that new Node be visited later during the traversal or not? This may look like a stupid question at first glance, but it actualy heavily depends on internal implementation details I am not aware of :wink: Well in the worst case I could of course abort and restart the traversal for the subree in which I did the manipulation every time I do some manipulation, but I am actualy trying to avoid this.

Best regards
Samuel Lörtscher

Hi Samuel,

Thanks for your inquiry.

The DocumentVisitor is farily versatile when it comes to these sorts of things, I have noticed you can remove nodes during traversal with no problem. The visitor keeps track of where it came from and continues from there.

I have done a quick test and it look like in your example, the visitor does not visit a newly inserted node. If you have any further queries please feel free to ask.