xml_replace and xml_replacecontent: Replacing an element with another.

Previous: xml_append: Inserting elements ] [ Top: index ] [ Next: Bookmarking things: xml_loc and xml_getloc ]

This is pretty straightforward; the linked list of the parent is traversed until the current XML is found, and it's swapped in. The parent pointer of the new child is modified, and the old child is deleted.
 
XMLAPI void xml_replace (XML * xml, XML * newxml)
{
   ELEMENTLIST * list;

   if (xml == NULL) return;
   if (xml->parent == NULL) return;
   if (newxml == NULL) xml_delete (xml);

   list = xml->parent->children;
   while (list != NULL && list->element != xml) list = list->next;
   if (list == NULL) return;

   newxml->parent = xml->parent;
   list->element = newxml;

   xml_free (xml);
}
To replace all contents, we just delete all the contents, then append the new piece.
 
XMLAPI void xml_replacecontent (XML * parent, XML * child)
{
   XML * first;

   if (parent == NULL) return;
   while (first = xml_first (parent)) {
      xml_delete (first);
   }
   if (child != NULL) xml_prepend (parent, child);
}
Previous: xml_append: Inserting elements ] [ Top: index ] [ Next: Bookmarking things: xml_loc and xml_getloc ]


This code and documentation are released under the terms of the GNU license. They are additionally copyright (c) 2000, Vivtek. All rights reserved except those explicitly granted under the terms of the GNU license. This presentation was created using LPML.