# More Mini-XML Programming Techniques

This chapter shows additional ways to use the Mini-XML library in your programs.

## Load Callbacks

Chapter 2 introduced the `mxmlLoadFile()` and `mxmlLoadString()` functions. The last argument to these functions is a callback function which is used to determine the value type of each data node in an XML document.

Mini-XML defines several standard callbacks for simple XML data files:

- `MXML_INTEGER_CALLBACK` - All data nodes contain whitespace-separated integers.
- `MXML_OPAQUE_CALLBACK` - All data nodes contain opaque strings ("CDATA").
- `MXML_REAL_CALLBACK` - All data nodes contain whitespace-separated floating-point numbers.
- `MXML_TEXT_CALLBACK` - All data nodes contain whitespace-separated strings.

You can provide your own callback functions for more complex XML documents. Your callback function will receive a pointer to the current element node and must return the value type of the immediate children for that element node: `MXML_INTEGER`, `MXML_OPAQUE`, `MXML_REAL`, or `MXML_TEXT`. The function is called after the element and its attributes have been read, so you can look at the element name, attributes, and attribute values to determine the proper value type to return.

The following callback function looks for an attribute named "type" or the element name to determine the value type for its child nodes:

        mxml_type_t
        type_cb(mxml_node_t *node)
        {
          const char *type;

         /*
          * You can lookup attributes and/or use the
          * element name, hierarchy, etc...
          */

          type = mxmlElementGetAttr(node, "type");
          if (type == NULL)
    	type = node->value.element.name;

          if (!strcmp(type, "integer"))
    	return (MXML_INTEGER);
          else if (!strcmp(type, "opaque"))
    	return (MXML_OPAQUE);
          else if (!strcmp(type, "real"))
    	return (MXML_REAL);
          else
    	return (MXML_TEXT);
        }

To use this callback function, simply use the name when you call any of the load functions:

        FILE *fp;
        mxml_node_t *tree;

        fp = fopen("filename.xml", "r");
        tree = mxmlLoadFile(NULL, fp, type_cb);
        fclose(fp);

## Save Callbacks

Chapter 2 also introduced the `mxmlSaveFile()`, `mxmlSaveString()`, and `mxmlSaveAllocString()` functions. The last argument to these functions is a callback function which is used to automatically insert whitespace in an XML document.

Your callback function will be called up to four times for each element node with a pointer to the node and a "where" value of `MXML_WS_BEFORE_OPEN`, `MXML_WS_AFTER_OPEN`, `MXML_WS_BEFORE_CLOSE`, or `MXML_WS_AFTER_CLOSE`. The callback function should return `NULL` if no whitespace should be added and the string to insert (spaces, tabs, carriage returns, and newlines) otherwise.

The following whitespace callback can be used to add whitespace to XHTML output to make it more readable in a standard text editor:

        const char *
        whitespace_cb(mxml_node_t *node,
                      int where)
        {
          const char *name;

         /*
          * We can conditionally break to a new line
          * before or after any element. These are
          * just common HTML elements...
          */

          name = node->value.element.name;

          if (!strcmp(name, "html") ||
              !strcmp(name, "head") ||
              !strcmp(name, "body") ||
    	  !strcmp(name, "pre") ||
              !strcmp(name, "p") ||
    	  !strcmp(name, "h1") ||
              !strcmp(name, "h2") ||
              !strcmp(name, "h3") ||
    	  !strcmp(name, "h4") ||
              !strcmp(name, "h5") ||
              !strcmp(name, "h6"))
          {
           /*
    	* Newlines before open and after
            * close...
    	*/

    	if (where == MXML_WS_BEFORE_OPEN ||
                where == MXML_WS_AFTER_CLOSE)
    	  return ("\n");
          }
          else if (!strcmp(name, "dl") ||
                   !strcmp(name, "ol") ||
                   !strcmp(name, "ul"))
          {
           /*
    	* Put a newline before and after list
            * elements...
    	*/

    	return ("\n");
          }
          else if (!strcmp(name, "dd") ||
                   !strcmp(name, "dt") ||
                   !strcmp(name, "li"))
          {
           /*
    	* Put a tab before <li>'s, * <dd>'s,
            * and <dt>'s, and a newline after them...
    	*/

    	if (where == MXML_WS_BEFORE_OPEN)
    	  return ("\t");
    	else if (where == MXML_WS_AFTER_CLOSE)
    	  return ("\n");
          }

         /*
          * Return NULL for no added whitespace...
          */

          return (NULL);
        }

To use this callback function, simply use the name when you call any of the save functions:

        FILE *fp;
        mxml_node_t *tree;

        fp = fopen("filename.xml", "w");
        mxmlSaveFile(tree, fp, whitespace_cb);
        fclose(fp);

## Custom Data Types

Mini-XML supports custom data types via global load and save callbacks. Only a single set of callbacks can be active at any time, however your callbacks can store additional information in order to support multiple custom data types as needed. The `MXML_CUSTOM` node type identifies custom data nodes.

The load callback receives a pointer to the current data node and a string of opaque character data from the XML source with character entities converted to the corresponding UTF-8 characters. For example, if we wanted to support a custom date/time type whose value is encoded as "yyyy-mm-ddThh:mm:ssZ" (ISO format), the load callback would look like the following:

        typedef struct
        {
          unsigned      year,    /* Year */
                        month,   /* Month */
                        day,     /* Day */
                        hour,    /* Hour */
                        minute,  /* Minute */
                        second;  /* Second */
          time_t        unix;    /* UNIX time */
        } iso_date_time_t;

        int
        load_custom(mxml_node_t *node,
                    const char *data)
        {
          iso_date_time_t *dt;
          struct tm tmdata;

         /*
          * Allocate data structure...
          */

          dt = calloc(1, sizeof(iso_date_time_t));

         /*
          * Try reading 6 unsigned integers from the
          * data string...
          */

          if (sscanf(data, "%u-%u-%uT%u:%u:%uZ",
                     &(dt->year), &(dt->month),
                     &(dt->day), &(dt->hour),
                     &(dt->minute),
                     &(dt->second)) != 6)
          {
           /*
            * Unable to read numbers, free the data
            * structure and return an error...
            */

            free(dt);

            return (-1);
          }

         /*
          * Range check values...
          */

          if (dt->month < 1 || dt->month > 12 ||
              dt->day < 1 || dt->day > 31 ||
              dt->hour < 0 || dt->hour > 23 ||
              dt->minute < 0 || dt->minute > 59 ||
              dt->second < 0 || dt->second > 59)
          {
           /*
            * Date information is out of range...
            */

            free(dt);

            return (-1);
          }

         /*
          * Convert ISO time to UNIX time in
          * seconds...
          */

          tmdata.tm_year = dt->year - 1900;
          tmdata.tm_mon  = dt->month - 1;
          tmdata.tm_day  = dt->day;
          tmdata.tm_hour = dt->hour;
          tmdata.tm_min  = dt->minute;
          tmdata.tm_sec  = dt->second;

          dt->unix = gmtime(&tmdata);

         /*
          * Assign custom node data and destroy
          * function pointers...
          */

          node->value.custom.data    = dt;
          node->value.custom.destroy = free;

         /*
          * Return with no errors...
          */

          return (0);
        }

The function itself can return 0 on success or -1 if it is unable to decode the custom data or the data contains an error. Custom data nodes contain a `void` pointer to the allocated custom data for the node and a pointer to a destructor function which will free the custom data when the node is deleted.

The save callback receives the node pointer and returns an allocated string containing the custom data value. The following save callback could be used for our ISO date/time type:

        char *
        save_custom(mxml_node_t *node)
        {
          char data[255];
          iso_date_time_t *dt;

          dt = (iso_date_time_t *)node->custom.data;

          snprintf(data, sizeof(data),
                   "%04u-%02u-%02uT%02u:%02u:%02uZ",
                   dt->year, dt->month, dt->day,
                   dt->hour, dt->minute, dt->second);

          return (strdup(data));
        }

You register the callback functions using the `mxmlSetCustomHandlers()` function:

        mxmlSetCustomHandlers(load_custom,
                              save_custom);

## Changing Node Values

All of the examples so far have concentrated on creating and loading new XML data nodes. Many applications, however, need to manipulate or change the nodes during their operation, so Mini-XML provides functions to change node values safely and without leaking memory.

Existing nodes can be changed using the `mxmlSetElement()`, `mxmlSetInteger()`, `mxmlSetOpaque()`, `mxmlSetReal()`, `mxmlSetText()`, and `mxmlSetTextf()` functions. For example, use the following function call to change a text node to contain the text "new" with leading whitespace:

        mxml_node_t *node;

        mxmlSetText(node, 1, "new");

## Formatted Text

The `mxmlNewTextf()` and `mxmlSetTextf()` functions create and change text nodes, respectively, using `printf`-style format strings and arguments. For example, use the following function call to create a new text node containing a constructed filename:

        mxml_node_t *node;

        node = mxmlNewTextf(node, 1, "%s/%s",
                            path, filename);

## Indexing

Mini-XML provides functions for managing indices of nodes. The current implementation provides the same functionality as `mxmlFindElement()`. The advantage of using an index is that searching and enumeration of elements is significantly faster. The only disadvantage is that each index is a static snapshot of the XML document, so indices are not well suited to XML data that is updated more often than it is searched. The overhead of creating an index is approximately equal to walking the XML document tree. Nodes in the index are sorted by element name and attribute value.

Indices are stored in `mxml_index_t` structures. The `mxmlIndexNew()` function creates a new index:

        mxml_node_t *tree;
        mxml_index_t *ind;

        ind = mxmlIndexNew(tree, "element",
                           "attribute");

The first argument is the XML node tree to index. Normally this will be a pointer to the `?xml` element.

The second argument contains the element to index; passing `NULL` indexes all element nodes alphabetically.

The third argument contains the attribute to index; passing `NULL` causes only the element name to be indexed.

Once the index is created, the `mxmlIndexEnum()`, `mxmlIndexFind()`, and `mxmlIndexReset()` functions are used to access the nodes in the index. The `mxmlIndexReset()` function resets the "current" node pointer in the index, allowing you to do new searches and enumerations on the same index. Typically you will call this function prior to your calls to `mxmlIndexEnum()` and `mxmlIndexFind()`.

The `mxmlIndexEnum()` function enumerates each of the nodes in the index and can be used in a loop as follows:

        mxml_node_t *node;

        mxmlIndexReset(ind);

        while ((node = mxmlIndexEnum(ind)) != NULL)
        {
          // do something with node
        }

The `mxmlIndexFind()` function locates the next occurrence of the named element and attribute value in the index. It can be used to find all matching elements in an index, as follows:

        mxml_node_t *node;

        mxmlIndexReset(ind);

        while ((node = mxmlIndexFind(ind, "element",
                                     "attr-value"))
                    != NULL)
        {
          // do something with node
        }

The second and third arguments represent the element name and attribute value, respectively. A `NULL` pointer is used to return all elements or attributes in the index. Passing `NULL` for both the element name and attribute value is equivalent to calling `mxmlIndexEnum`.

When you are done using the index, delete it using the `mxmlIndexDelete()` function:

        mxmlIndexDelete(ind);

## SAX (Stream) Loading of Documents

Mini-XML supports an implementation of the Simple API for XML (SAX) which allows you to load and process an XML document as a stream of nodes. Aside from allowing you to process XML documents of any size, the Mini-XML implementation also allows you to retain portions of the document in memory for later processing.

The `mxmlSAXLoadFd`, `mxmlSAXLoadFile`, and `mxmlSAXLoadString` functions provide the SAX loading APIs. Each function works like the corresponding `mxmlLoad` function but uses a callback to process each node as it is read.

The callback function receives the node, an event code, and a user data pointer you supply:

        void
        sax_cb(mxml_node_t *node,
               mxml_sax_event_t event,
               void *data)
        {
          ... do something ...
        }

The event will be one of the following:

- `MXML_SAX_CDATA` - CDATA was just read
- `MXML_SAX_COMMENT` - A comment was just read
- `MXML_SAX_DATA` - Data (custom, integer, opaque, real, or text) was just read
- `MXML_SAX_DIRECTIVE` - A processing directive was just read
- `MXML_SAX_ELEMENT_CLOSE` - A close element was just read (`</element>`)
- `MXML_SAX_ELEMENT_OPEN` - An open element was just read (`<element>`)

Elements are released after the close element is processed. All other nodes are released after they are processed. The SAX callback can retain the node using the `mxmlRetain` function. For example, the following SAX callback will retain all nodes, effectively simulating a normal in-memory load:

        void
        sax_cb(mxml_node_t *node,
               mxml_sax_event_t event,
               void *data)
        {
          if (event != MXML_SAX_ELEMENT_CLOSE)
            mxmlRetain(node);
        }

More typically the SAX callback will only retain a small portion of the document that is needed for post-processing. For example, the following SAX callback will retain the title and headings in an XHTML file. It also retains the (parent) elements like `<html>`, `<head>`, and `<body>`, and processing directives like `<?xml ... ?>` and `<!DOCTYPE ... >`:

        void
        sax_cb(mxml_node_t *node,
               mxml_sax_event_t event,
               void *data)
        {
          if (event == MXML_SAX_ELEMENT_OPEN)
          {
           /*
            * Retain headings and titles...
            */

            char *name = node->value.element.name;

            if (!strcmp(name, "html") ||
                !strcmp(name, "head") ||
                !strcmp(name, "title") ||
                !strcmp(name, "body") ||
                !strcmp(name, "h1") ||
                !strcmp(name, "h2") ||
                !strcmp(name, "h3") ||
                !strcmp(name, "h4") ||
                !strcmp(name, "h5") ||
                !strcmp(name, "h6"))
              mxmlRetain(node);
          }
          else if (event == MXML_SAX_DIRECTIVE)
            mxmlRetain(node);
          else if (event == MXML_SAX_DATA &&
                   node->parent->ref_count > 1)
          {
           /*
            * If the parent was retained, then retain
            * this data node as well.
            */

            mxmlRetain(node);
          }
        }

The resulting skeleton document tree can then be searched just like one loaded using the `mxmlLoad` functions. For example, a filter that reads an XHTML document from stdin and then shows the title and headings in the document would look like:

        mxml_node_t *doc, *title, *body, *heading;

        doc = mxmlSAXLoadFd(NULL, 0,
                            MXML_TEXT_CALLBACK,
                            sax_cb, NULL);

        title = mxmlFindElement(doc, doc, "title",
                                NULL, NULL,
                                MXML_DESCEND);

        if (title)
          print_children(title);

        body = mxmlFindElement(doc, doc, "body",
                               NULL, NULL,
                               MXML_DESCEND);

        if (body)
        {
          for (heading = body->child;
               heading;
               heading = heading->next)
            print_children(heading);
        }