XML Parser for C

CXmlParsing

C Problem Overview


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

C Solutions


Solution 1 - C

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/>. 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> 
*/

#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>. 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;
}

Solution 2 - C

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

Solution 3 - C

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

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

Solution 4 - C

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

For C++ you can check out TinyXML++

Solution 5 - C

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

Solution 6 - C

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.

Solution 7 - C

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

Solution 8 - C

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.

Solution 9 - C

For C++ I suggest using CMarkup.

Solution 10 - C

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncoderView Question on Stackoverflow
Solution 1 - CbortzmeyerView Answer on Stackoverflow
Solution 2 - CcodelogicView Answer on Stackoverflow
Solution 3 - CChris Jester-YoungView Answer on Stackoverflow
Solution 4 - CKasprzolView Answer on Stackoverflow
Solution 5 - CsingpolymaView Answer on Stackoverflow
Solution 6 - CMichael FoukarakisView Answer on Stackoverflow
Solution 7 - CHankView Answer on Stackoverflow
Solution 8 - CTim PostView Answer on Stackoverflow
Solution 9 - CZhang ZhiliangView Answer on Stackoverflow
Solution 10 - CterryView Answer on Stackoverflow