Closed GoogleCodeExporter closed 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
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
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
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
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
Yes, append_complex_node is a better name.
Original comment by arseny.k...@gmail.com
on 4 Apr 2010 at 1:09
Thanks, everything works fine now. I think that this ticket should be closed.
Original comment by yegor256
on 4 Apr 2010 at 1:34
Original comment by arseny.k...@gmail.com
on 4 Apr 2010 at 2:20
Original issue reported on code.google.com by
yegor256
on 4 Apr 2010 at 12:48