tags:

views:

1326

answers:

9
+3  Q: 

XML Parser for C

Can you suggest some of the best XML Parser for C ?

+10  A: 

Two of the most widely used parsers are Expat and libxml.

If you are okay with using C++, there's Xerces-C++ too.

Chris Jester-Young
+1  A: 

Expat is pretty decent. It's hard to give good recommendations without more information though.

Hank
+1  A: 

You can try ezxml -- it's a lightweight parser written entirely in C.

For C++ you can check out TinyXML++

Kasprzol
+7  A: 

How about one written in pure assembler :-) Don't forget to check out the benchmarks.

codelogic
Nice, you win. :-)
Chris Jester-Young
Much of the speed factor comes from the fact that it only supports a subset of XML but it looks like it may be useful for many purposes. Very neat.
Robert Gamble
Not having used it, I can't really recommend it, but thought it was worth sharing since it does have a C API :)
codelogic
Now that looks really, REALLY interesting for tasks where the schema and name space is known (i.e. RSS). Thanks for the link!!
Tim Post
+1  A: 

Could you give some indication of what platforms you are writing for? This should weigh heavily on what is 'best'. You might find a super 'xml-foo' library that does not ship commonly on most systems by default .. while its great, the lack of the library might prevent (or at least) annoy users.

Mostly, I use libxml2 .. because its standard or easy to install on the platforms that I target.

As you see, 'best' is also determined by the library being available on your target platforms.

Tim Post
A: 

On Windows, it's native with Win32 api...

Not "native" Win32 as such. You have two choices: for COM applications, you can use MSXML; for .NET, you can use System.Xml. Obviously, the question does not relate to the latter, and I'm not entirely sure that the OP is using COM (in my experience, using COM in C is a bit hairy).
Chris Jester-Young
+3  A: 

Two examples with expat and libxml2. The second one is, IMHO, much easier to use since it creates a tree in memory, a data structure which is easy to work with. expat, on the other hand, does not build anything (you have to do it yourself), it just allows you to call handlers at specific events during the parsing. But expat may be faster (I didn't measure).

With expat, reading a XML file and displaying the elements indented:

/* 
   A simple test program to parse XML documents with expat
   <http://expat.sourceforge.net/&gt;. It just displays the element
   names.

   On Debian, compile with:

   gcc -Wall -o expat-test -lexpat expat-test.c  

   Inspired from <http://www.xml.com/pub/a/1999/09/expat/index.html&gt; 
*/

#include <expat.h>
#include <stdio.h>
#include <string.h>

/* Keep track of the current level in the XML tree */
int             Depth;

#define MAXCHARS 1000000

void
start(void *data, const char *el, const char **attr)
{
    int             i;

    for (i = 0; i < Depth; i++)
     printf("  ");

    printf("%s", el);

    for (i = 0; attr[i]; i += 2) {
     printf(" %s='%s'", attr[i], attr[i + 1]);
    }

    printf("\n");
    Depth++;
}      /* End of start handler */

void
end(void *data, const char *el)
{
    Depth--;
}      /* End of end handler */

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

    char           *filename;
    FILE           *f;
    size_t          size;
    char           *xmltext;
    XML_Parser      parser;

    if (argc != 2) {
     fprintf(stderr, "Usage: %s filename\n", argv[0]);
     return (1);
    }
    filename = argv[1];
    parser = XML_ParserCreate(NULL);
    if (parser == NULL) {
     fprintf(stderr, "Parser not created\n");
     return (1);
    }
    /* Tell expat to use functions start() and end() each times it encounters
     * the start or end of an element. */
    XML_SetElementHandler(parser, start, end);
    f = fopen(filename, "r");
    xmltext = malloc(MAXCHARS);
    /* Slurp the XML file in the buffer xmltext */
    size = fread(xmltext, sizeof(char), MAXCHARS, f);
    if (XML_Parse(parser, xmltext, strlen(xmltext), XML_TRUE) ==
        XML_STATUS_ERROR) {
     fprintf(stderr,
      "Cannot parse %s, file may be too large or not well-formed XML\n",
      filename);
     return (1);
    }
    fclose(f);
    XML_ParserFree(parser);
    fprintf(stdout, "Successfully parsed %i characters in file %s\n", size,
     filename);
    return (0);
}

With libxml2, a program which displays the name of the root element and the names of its children:

/*
   Simple test with libxml2 <http://xmlsoft.org&gt;. It displays the name
   of the root element and the names of all its children (not
   descendents, just children).

   On Debian, compiles with:
   gcc -Wall -o read-xml2 $(xml2-config --cflags) $(xml2-config --libs) \
                    read-xml2.c    

*/

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

int
main(int argc, char **argv)
{
    xmlDoc         *document;
    xmlNode        *root, *first_child, *node;
    char           *filename;

    if (argc < 2) {
     fprintf(stderr, "Usage: %s filename.xml\n", argv[0]);
     return 1;
    }
    filename = argv[1];

    document = xmlReadFile(filename, NULL, 0);
    root = xmlDocGetRootElement(document);
    fprintf(stdout, "Root is <%s> (%i)\n", root->name, root->type);
    first_child = root->children;
    for (node = first_child; node; node = node->next) {
     fprintf(stdout, "\t Child is <%s> (%i)\n", node->name, node->type);
    }
    fprintf(stdout, "...\n");
    return 0;
}
bortzmeyer
+1  A: 

http://www.minixml.org is also pretty good. Small and just ANSI C.

singpolyma
A: 

My personal preference is libxml2. It's very easy to use but I never bothered to benchmark it, as I've only used it for configuration file parsing.

Michael Foukarakis