Libxml2 example for howto parse XML files in C


Libxml2 is an XML parser for the C programming language it's a toolkit developed for the Gnome project (but usable outside of the Gnome platform), it's freely available under the MIT License. This howto provides a simple example that shows how to use libxml to parse xml files.


Installing and using the libxml2 library

On the development machine, libxml2-dev must be installed. In Debian based distributions:

apt-get intall libxml2-dev

In Ubuntu, this installs header files under /usr/include/libxml2 so to compile code that uses libxml2, you need to specify the include path accordingly.

gcc test.c -lxml2 -I/usr/include/libxml2

Sample code using libxml2

#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
 
static void print_element_names(xmlNode * a_node);
 
int
main()
{
 
        xmlDoc         *doc = NULL;
        xmlNode        *root_element = NULL;
        const char     *Filename = "/tmp/config.xml";
 
        doc = xmlReadFile(Filename, NULL, 0);
 
        if (doc == NULL)
          {
                  printf("error: could not parse file %s\n", Filename);
          }
        else
          {
 
                  /*
                   * Get the root element node
                   */
                  root_element = xmlDocGetRootElement(doc);
 
                  print_element_names(root_element);
 
                  /*
                   * free the document
                   */
                  xmlFreeDoc(doc);;
          }
        /*
         *Free the global variables that may
         *have been allocated by the parser.
         */
        xmlCleanupParser();
 
        return (0);
 
}
 
/* Recursive function that prints the XML structure */
 
static void
print_element_names(xmlNode * a_node)
{
    xmlNode *cur_node = NULL;
 
    for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
        if (cur_node->type == XML_ELEMENT_NODE) {
            printf("node type: Element, name: %s\n", cur_node->name);
        }
 
        print_element_names(cur_node->children);
    }
}

There are plenty of examples like this in the libxml2 website. For a full list of features, take a look at the reference guide.



Labels: , , Wireless Internet Security Coding Network Monitoring

Comment

Enter your comment (wiki syntax is allowed):
BHKHW

Wireless Internet Security Performance RADIUS server Wireless Internet Security Performance RADIUS server