xml_append: Inserting elements

Previous: xml_prepend: Inserting elements ] [ Top: index ] [ Next: xml_replace and xml_replacecontent: Replacing an element with another. ]

It's appending where we run into problems. And indeed my original solution here (scanning the list) didn't scale well. Now (Dec 16, 2001) I'm correcting that flaw.
 
XMLAPI void xml_append (XML * parent, XML * child)
{
   ELEMENTLIST * list;
   ELEMENTLIST * ch;

   child->parent = parent;

   list = (ELEMENTLIST *) MALLOC (sizeof(struct _list));
   list->element = child;
   list->prev = parent->lastchild;
   if (list->prev) list->prev->next = list;
   list->next = NULL;

   if (parent->children == NULL) {
      parent->children = list;
   }
   parent->lastchild = list;
}
(January 9, 2002): When building up XML which has a need to be read by human eyes, it's nice to add the occasional line break. Since it's scanned with xml_firstelem/xml_nextelem, the linebreaks will be invisible to the scanning code. To build XML this way, use xml_append_pretty, which wraps linebreaks around the inserted XML. (But doesn't duplicate them, so no skipped lines.)
 
XMLAPI void xml_append_pretty (XML * parent, XML * child)
{
   if (!xml_first (parent)) xml_append (parent, xml_createtext ("\n"));
   xml_append (parent, child);
   xml_append (parent, xml_createtext ("\n"));
}
This will save me some work here and there. (November 30, 2002): And of course, one always knows that all variants of a function will eventually be needed....
 
XMLAPI void xml_prepend_pretty (XML * parent, XML * child)
{
   if (!xml_first (parent)) xml_prepend (parent, xml_createtext ("\n"));
   xml_prepend (parent, child);
   xml_prepend (parent, xml_createtext ("\n"));
}
Previous: xml_prepend: Inserting elements ] [ Top: index ] [ Next: xml_replace and xml_replacecontent: Replacing an element with another. ]


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.