xml_free: Cleaning up afterwards

Previous: xml_createtext: a shortcut for plain text ] [ Top: index ] [ Next: Deleting pieces: xml_delete ]

To free an XML element, we free its name, each of its attributes (and their names and values), each child (recursively) and the list element which held the child, and finally we can free the XML element itself.
 
XMLAPI void xml_free (XML * xml)
{
   ATTR * attr;
   ELEMENTLIST * list;

   if (xml == NULL) return;

   if (xml->name != NULL) FREE ((void *) (xml->name));
   while (xml->attrs) {
      attr = xml->attrs;
      xml->attrs = xml->attrs->next;
      if (attr->name != NULL) FREE ((void *) (attr->name));
      if (attr->value != NULL) FREE ((void *) (attr->value));
      xml->attrs = attr->next;
      FREE ((void *) attr);
   }

   while (xml->children) {
      list = xml->children;
      xml->children = list->next;
      if (list->element != NULL) xml_free (list->element);
      FREE ((void *) list);
   }
   FREE ((void *) xml);
}
Previous: xml_createtext: a shortcut for plain text ] [ Top: index ] [ Next: Deleting pieces: xml_delete ]


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.