senjuhashirama / pugixml

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

Append child node #215

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi,
I am trying to create new pugi::xml_node in pugi::xml_document. And then 
appending the same node to the same document. 
For creating i am using append_child() and for appending i am using 
append_copy(). But it is failing.

to create node i am using following function
CXmlDocument::CreateElement(const std::wstring& name, CXmlNode& element) 
{   
   pugi::xml_node tempNode;

   //Convert wstring to pugi::char_t*   
   std::string strName =pugi::as_utf8(name);
   const pugi::char_t* bstr_name = strName.c_str();  

   tempNode = m_XmlDocument.append_child(bstr_name);

   //if element node created is null then fail.
   if(NULL == tempNode)
   {
      return ERROR;
   }   

   element.m_XmlNode = tempNode;
   m_XmlDocument.remove_child(tempNode);
   return OK;
}

for appending :

CXmlDocument::AppendChildNode(CXmlNode& child)
{
   pugi::xml_node dstNode;

    //check if node is null then fail
    if(NULL == m_XmlNode)
    {
        return ERROR;
    }
   if(NULL == child.m_XmlNode)
   {
      return ERROR;
   }

   //m_XmlDocument.reset(m_XmlDocument);
   dstNode = m_XmlDocument.append_copy(child.m_XmlNode);
   if(dstNode == NULL)
   {
      return ERROR;
   }
   child.m_XmlNode = dstNode;
   return OK;  
}

If i add doc.reset() it works for some times. But some times fails.
If i remove it is failing.
Sequence to use these functions :
CXmlDocument doc;
CXmlNode node;

doc.CreateElement(L"newNode", node);
doc.AppendChild(node);

Please give me the solution.

Kshitija

Original issue reported on code.google.com by kshitija...@gmail.com on 20 Sep 2013 at 11:01

GoogleCodeExporter commented 9 years ago
Issue 216 has been merged into this issue.

Original comment by arseny.k...@gmail.com on 4 Oct 2013 at 1:00

GoogleCodeExporter commented 9 years ago
Issue 216 has been merged into this issue.

Original comment by arseny.k...@gmail.com on 4 Oct 2013 at 1:00

GoogleCodeExporter commented 9 years ago
This code is invalid.
Nodes can not exist outside of the document. If you remove a node from the 
tree, the node handle becomes invalid. I believe this is described in the 
manual.

What happens is that in this code:

   element.m_XmlNode = tempNode;
   m_XmlDocument.remove_child(tempNode);

it is illegal to use element.m_XmlNode after remove_child returns since 
m_XmlNode and tempNode both point to the same object inside the tree, but the 
object is removed. What you're seeing is unspecified behavior.

Original comment by arseny.k...@gmail.com on 4 Oct 2013 at 1:04