2

How do I get a complete copy of a RapidXML xml_document?

There is a clone_node function; how to use to to create a complete copy of an existing document?

Petrus Theron
  • 25,051
  • 30
  • 137
  • 263
  • Most of the time xml libraries will give you the option of cloning just the node, or doing a deep-clone, which you can set to give you the tree underneath it. – James Aug 17 '10 at 15:30

2 Answers2

3

as you already guessed there is the function clone_node method. From the online help:

xml_node* clone_node(const xml_node< Ch > *source, xml_node< Ch > *result=0);

Clones an xml_node and its hierarchy of child nodes and attributes. Nodes and attributes are allocated from this memory pool. Names and values are not cloned, they are shared between the clone and the source. Result node can be optionally specified as a second parameter, in which case its contents will be replaced with cloned source node. This is useful when you want to clone entire document.

The approach proposed by FreshCode is quite simple, but adds an unnecessary overhead to "toString & parseBack" that you may want to avoid.

Community
  • 1
  • 1
Cavaz
  • 2,682
  • 19
  • 33
1

I'm sure there's a cleaner, tree-based approach, but I solved it with the following, where str is the xml output from another doc:

xml_document<> doc;
doc.parse<0>(doc.allocate_string(str));
Petrus Theron
  • 25,051
  • 30
  • 137
  • 263