Monday, November 23, 2009

Libxml2 example for howto parse XML files in C

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 -o test -lxml2 -I/usr/include/libxml2

or

# cc parse_xml.c -o parse_xml `pkg-config --cflags --libs libxml-2.0`

Sample XML file
<root>
 <db_server>localhost</db_server>
 <db_user>test_user</db_user>
 <db_pass>test_pass</db_pass>
 <db_name>test_db</db_name>
</root>

Sample code using libxml2

#include 
#include 
#include 
 
static void print_element_names(xmlNode * a_node);
 
int
main()
{
 
        xmlDoc         *doc = NULL;
        xmlNode        *root_element = NULL;
        const char     *Filename = "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);
            printf("node value: Element, name: %s\n", xmlNodeGetContent(cur_node));
        }
 
        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.

http://xmlsoft.org/html/libxml-tree.html#xmlNodeBufGetContent

No comments: