C的XML解析器

cod*_*der 46 c xml parsing

你能为C推荐一些最好的XML Parser吗?

bor*_*yer 67

使用expatlibxml2的两个例子.第二个是,恕我直言,更容易使用,因为它在内存中创建一个树,一个易于使用的数据结构.另一方面,expat不构建任何东西(你必须自己做),它只允许你在解析过程中调用特定事件的处理程序.但外籍人士可能会更快(我没有衡量).

使用expat,读取XML文件并显示缩进的元素:

/* 
   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);
}
Run Code Online (Sandbox Code Playgroud)

使用libxml2,一个显示根元素名称及其子名称的程序:

/*
   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;
}
Run Code Online (Sandbox Code Playgroud)

  • +1用于编译命令的好例子. (9认同)
  • 对于你的`libxml2`例子,我认为你应该在完成解析时调用`xmlFreeDoc(document);`. (3认同)
  • 我只需要在该命令的末尾添加-lxml2即可使其工作。 (2认同)

Chr*_*ung 39

两个最广泛使用的解析器是Expatlibxml.

如果您对使用C++没问题,那么也有Xerces-C++.


cod*_*gic 39

如何用纯汇编语言编写:-)不要忘记查看基准测试.

  • 很好,你赢了.:-) (8认同)

小智 9

你可以试试ezxml - 它是一个完全用C语言编写的轻量级解析器.

对于C++,您可以查看TinyXML ++


sin*_*yma 7

http://www.minixml.org也很不错.小而且只是ANSI C.