Re: [xml] Problem using xmlUnlinkNode



On 12/28/2011 02:19 AM, John J. Boyer wrote:
I need to delete some nodes from a parse tree. So I use xmlUnlinkNode 
and then xmlFree. However, when I output the tree with xmlDumpDoc the 
nodes are still there. What am I missing?

It's difficult to say without seeing what you're doing exactly.
Here's what works for me (sample program which deletes the first child
element of the document root):

  xmlDocPtr pDoc = xmlParseFile(pczFileName);
   //error checking....
  xmlNodePtr pRoot = xmlDocGetRootElement(pDoc);
   //error checking...

  xmlNodePtr pNext = pRoot->children;

  int iDeleted = 0;

  while (pNext)
    {
      if (pNext->type == XML_ELEMENT_NODE && !iDeleted)
        {
          xmlNodePtr pDelNode = pNext;

          pNext = pNext->next;

          xmlUnlinkNode(pDelNode);

          xmlFree(pDelNode);

          iDeleted = 1;
        }
      else
        {
          pNext = pNext->next;
        }

    }

  xmlChar *pxBuf;
  int iBufSize = 0;

  xmlDocDumpFormatMemory(pDoc, &pxBuf, &iBufSize, 1);

  fprintf(stdout, "Buffer out: %s\n", (const char*)pxBuf);


The input file:
$ cat t.xml
<people_list>
  <person/>
  <person/>
</people_list>

Sample output:
$ ./del t.xml
Buffer out: <?xml version="1.0"?>
<people_list>

  <person/>
</people_list>

Is that what you're doing in your application?

All the best!
Piotr



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]