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.

December 7, 2001: I often wonder at what point all this starts to be code bloat. Anyway, I've introduced a "cleanup" member to the element, to clean up attached binary data when an element is freed.
 
XMLAPI void xml_free (XML * xml)
{
   ATTR * attr;
   ELEMENTLIST * list;

   if (xml == NULL) return;

   if (xml->cleanup && xml->extra) {
      (*(xml->cleanup)) (xml->extra);
   }

   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 copyright (c) 2000-2003, Vivtek. All rights reserved except those explicitly granted under the terms of the GNU license. This presentation was created using LPML.