falcong / pugixml

Automatically exported from code.google.com/p/pugixml
0 stars 0 forks source link

can I add a node to the document without making it a child before? #47

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I'm trying this scenario:

pugi:xml_document xml;
pugi::xml_node node = xml.append_child().set_name("document");
pugi::xml_node x;
x.set_name("abc");
node.append_copy(x);

it doesn't work. Is there any method/approach to do what I want to do? thanks!

Original issue reported on code.google.com by yegor256 on 4 Apr 2010 at 12:48

GoogleCodeExporter commented 9 years ago
workaround I found so far looks like this:

pugi:xml_document xml;
pugi::xml_node node = xml.append_child().set_name("document");
pugi::xml_node x = xml.append_child().set_name("abc");
node.append_copy(x);
xml.remove_child(x);

but it looks ugly...

Original comment by yegor256 on 4 Apr 2010 at 12:50

GoogleCodeExporter commented 9 years ago
Nodes do not exist without document, since all memory management & node 
ownership is 
done by the document. Therefore the only way to create new node is to create it 
in 
context of some document (of course you can create nodes in some other document 
and 
then copy them to the destination document).

However I don't understand the actual problem - why don't you do

pugi::xml_document xml;

pugi::xml_node node = xml.append_child();
node.set_name("document");

pugi::xml_node x = node.append_child();
x.set_name("abc");

Original comment by arseny.k...@gmail.com on 4 Apr 2010 at 12:56

GoogleCodeExporter commented 9 years ago
Because my case looks like this:

xml_node create_complex_node() {
  xml_node node;
  /* fill it with data */
  return node;
}
xml_document xml;
for (…) {
  xml.append_copy(create_complex_node());
}

Now I have to do it like this, and it looks very inefficient:

xml_node create_complex_node(xml_node& root) {
  xml_node node = root.append_child();
  /* fill it with data */
  return node;
}
xml_document xml;
for (…) {
  xml_node just_added = xml.append_copy(create_complex_node(xml));
  xml.remove_child(just_added);
}

See the problem?

Original comment by yegor256 on 4 Apr 2010 at 1:03

GoogleCodeExporter commented 9 years ago
I think that I should refactor it:

void append_complex_node(xml_node& root) {
  xml_node node = root.append_child();
  /* fill it with data */
}
xml_document xml;
for (…) {
  append_complex_node(xml);
}

Now looks better :) Thanks for your hints!

Original comment by yegor256 on 4 Apr 2010 at 1:06

GoogleCodeExporter commented 9 years ago
The correct design here is to create all nodes exactly where you'll need them. 
In your 
example it's sufficient to call create_complex_node(node) in a loop, assuming 
you have 
to add several complex nodes as 'node' children.

Original comment by arseny.k...@gmail.com on 4 Apr 2010 at 1:09

GoogleCodeExporter commented 9 years ago
Yes, append_complex_node is a better name.

Original comment by arseny.k...@gmail.com on 4 Apr 2010 at 1:09

GoogleCodeExporter commented 9 years ago
Thanks, everything works fine now. I think that this ticket should be closed.

Original comment by yegor256 on 4 Apr 2010 at 1:34

GoogleCodeExporter commented 9 years ago

Original comment by arseny.k...@gmail.com on 4 Apr 2010 at 2:20