amorlzu / pugixml

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

Problem in appending node to document #191

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi,
I am trying to append node to document but fails.
I have created new document. Creating new node element. Appending one more node 
to this node and then appneding this parent node to document. For this I am 
using append_copy function but fails. Code i am using is as:

   doc.reset(doc);
   childNode = doc.append_copy(child.m_XmlNode);
   child.m_XmlNode = childNode;

Atfer calling reset function, it destroys the tree and replaces it with an 
empty one. If reset function is not called, no node is appended to document.
Please help me.

Kshitija

Original issue reported on code.google.com by kshitija...@gmail.com on 28 Dec 2012 at 7:11

GoogleCodeExporter commented 9 years ago
append_copy will always be able to append the node to the document, as long as 
child.m_XmlNode.type() returns something that's not node_document or node_null. 
Can you confirm the return value of child.m_XmlNode.type()?

Original comment by arseny.k...@gmail.com on 28 Dec 2012 at 8:53

GoogleCodeExporter commented 9 years ago
Return value of child.m_XmlNode.type() is 2 i.e. node_document.

Original comment by kshitija...@gmail.com on 28 Dec 2012 at 9:14

GoogleCodeExporter commented 9 years ago
append_copy can not append the document as a child, since that would result in 
nested documents, which is not allowed.
If the expected type was indeed the document, then you can manually append all 
children like this:

for (pugi::xml_node c = child.m_XmlNode.first_child(); c; c = c.next_sibling())
    doc.append_copy(c);

If, however, you expected child.m_XmlNode to be an element instead of a 
document, you have to check your code to discover why it's not an element.

Original comment by arseny.k...@gmail.com on 28 Dec 2012 at 9:47

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
<node>
  <node1>
    <node2>
    </node2>
  </node1>
<node>
Using code given by you, can we append "node" along with all child nodes?
What about the reset()?

Original comment by kshitija...@gmail.com on 28 Dec 2012 at 10:19

GoogleCodeExporter commented 9 years ago
Yes, this code appends the node with all child nodes - note that append_copy 
appends the entire subtree. The reason the loop is necessary at all is to cope 
with document restriction.

doc.reset(doc) is equivalent to doc.reset() and should not be needed unless you 
do want to remove all nodes from the document before proceeding.

Original comment by arseny.k...@gmail.com on 28 Dec 2012 at 3:42

GoogleCodeExporter commented 9 years ago
If I dont use doc.reset(doc), it is not appending the node. Any solution for 
this?

Original comment by kshitija...@gmail.com on 31 Dec 2012 at 5:29

GoogleCodeExporter commented 9 years ago
Normally doc.reset is not needed. I can't think of a case where it would be 
necessary for append_copy to work - can you provide a larger code example that 
exhibits the problem?

Original comment by arseny.k...@gmail.com on 31 Dec 2012 at 6:18

GoogleCodeExporter commented 9 years ago

Original comment by arseny.k...@gmail.com on 3 Mar 2013 at 3:04

GoogleCodeExporter commented 9 years ago
I want to create a node in document. And again append that same node in that 
document only. I am using append_child to create node and append_copy to append 
node.

Original comment by kshitija...@gmail.com on 20 Sep 2013 at 9:55