Search code examples
c++xmlrapidxmlpugixml

moving (not copying) nodes using pugixml and rapidxml


I am looking for an XML library that supports a DOM interface. Performance is important for me so I was currently looking at rapidxml and also pugixml.

The thing is that my application needs to modify the DOM tree, including moving nodes. And I cannot see a direct way of doing this (neither with rapidxml nor with pugixml). It seems I need to copy/clone the nodes, which could lead to lots of copy operations if the subtree I want to move is quite deep.

Is there a more efficient way of doing this (either with rapidxml or with pugixml) (e.g. swapping the pointers, etc.)? If not, is there any other lightweight library that allows doing this?

Thanks!


Solution

  • pugixml now (as of an hour ago) has ability to move node subtrees cheaply - see xml_node::prepend_move/append_move/insert_move_before/insert_move_after.

    Note that these operations are not constant time - or rather the operations themselves are, but there is a validation step that guards against moving a node inside its own subtree (that would lead to the node being separated from the rest of the tree and causing memory leaks); this step has to traverse the ancestor chain of the new node location, making the move O(logN).