Featured Post

Trie implementation in C

Simple XML Parser in C using libxml

This is a simple XML parser example in C which uses the libxml library for parsing the XML Documents.
Libxml is a XML processor for the GNOME project. It implements a whole lot of existing standards related to markup languages and a few extras as well.It is open source and can be freely downloaded at ftp://xmlsoft.org/libxml2/

Also some useful documentation about its usage can be found at http://xmlsoft.org/downloads.html

This is just the usage of the library API to get the field values from a XML Document.

For example,

If the XML Doc is somewhat like this :


  
     John Fleck
     June 2, 2002

To get the author name we need to run the program like this -->

Compilation step : gcc xmlParse.c -I/usr/include/libxml2 -L/usr/lib -lxml2 -lz -lpthread -lm

Run : ./a.out test.xml storyinfo author
Output : John Fleck

Remember : This program assumes that the libxml is installed in the system.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>

void die(char *msg)
{
  printf("%s", msg);
  return;
}

void
parseNode (xmlDocPtr doc, xmlNodePtr cur, char *subchild)
{

    xmlChar *key;
    cur = cur->xmlChildrenNode;
    while (cur != NULL)
    {
        if ((!xmlStrcmp(cur->name, (const xmlChar *)subchild)))
        {
            key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);

            printf("%s\n", key);
            xmlFree(key);
        }
        cur = cur->next;
    }
    return;
}

static void
parseDoc(char *docname, char *child, char *subchild)
{

    xmlDocPtr doc;
    xmlNodePtr cur;

    doc = xmlParseFile(docname);

    if (doc == NULL )
        die("Document parsing failed. \n");

    cur = xmlDocGetRootElement(doc); //Gets the root element of the XML Doc

    if (cur == NULL)
    {
        xmlFreeDoc(doc);
        die("Document is Empty!!!\n");
    }

    cur = cur->xmlChildrenNode;
    while (cur != NULL)
    {
        if ((!xmlStrcmp(cur->name, (const xmlChar *)child)))
        {
            parseNode (doc, cur, subchild);
        }
        cur = cur->next;
    }

    xmlFreeDoc(doc);
    return;
}

int
main(int argc, char **argv)
{
    char *docname;

    if (argc != 4)
    {
        printf("Usage: %s <docname> <child> <subchild>\n", argv[0]);
        return(0);
    }

    docname = argv[1];
    parseDoc (docname,argv[2],argv[3]);

    return (1);
}


Comments

  1. i want code for add new child in next line in my xml file and to remove a node from xml file plz help me..
    thanq....

    ReplyDelete
    Replies
    1. below the code which adds the child node to root element


      #include
      #include
      #include
      #include


      int main()
      {

      xmlDocPtr doc;
      xmlNodePtr root_node,cur,node_type;
      doc = xmlParseFile("file_name.xml"); //file name for which you want to add child node
      if (doc == NULL )
      {
      fprintf(stderr,"Document not parsed successfully. \n");
      return;
      }
      root_node = xmlDocGetRootElement(doc);
      cur = root_node->xmlChildrenNode;

      node_type = xmlNewChild(cur, NULL,(const xmlChar *)"adding_node_name","adding_node_content");
      xmlNewProp(node_type,(const xmlChar *) "adding_node_attribute" ,"adding_node_attribute_value" );

      xmlSaveFormatFileEnc ("file_name.xml",doc,"UTF-8", 1);
      xmlFreeDoc(doc);
      return(0);

      }
      /*
      gcc add_child_node.c -I /usr/include/libxml2/ -L /usr/lib -lxml2 -lz -lpthread -lm -o add_child_node
      */

      Delete
  2. i WANT A CODE TO INSERT THE NODES INTO MYSQL DATABASE THANK YOU

    ReplyDelete
  3. I think there are some libraries which allows you to do that.. once you get the nodes from xml, you can insert them into database.
    Sample Reference : http://zetcode.com/db/mysqlc/

    ReplyDelete
  4. i want to implement my own parser

    ReplyDelete
    Replies
    1. I don't think that's a good idea. There are already so many libraries available open-source that you can use for the parsing purpose. This ranges from lightweight libraries(which can be easily used on an embedded device) to heavyweight feature loaded libraries which perform complex tasks. I would suggest to choose as per your need from the existing ones. Re-inventing the wheel doesn't make sense.

      Delete
  5. Hi Varun,

    Sometimes I am getting "Document is Empty!!!", What may be the reason for this.

    The error is observed sometimes only.

    ReplyDelete
    Replies
    1. That happens when the root element is not found in the xml file. Please check if the xml is formatted properly. If it is, you can send me the file for which you got the error.

      Delete
    2. For checking the format ,online XML validator can be used.

      Delete
  6. You should not return (1); you should return (0) -- that indicates success on unix.

    ReplyDelete

Post a Comment

Please post your valuable suggestions