C 的 XML 解析器 [已关闭]

问题描述 投票:0回答:10

您能推荐一些最好的 C XML 解析器吗?

c xml parsing
10个回答
77
投票

两个带有 expatlibxml2 的示例。第二个是, 恕我直言,使用起来更容易,因为它在内存中创建一棵树,一个数据 易于使用的结构。另一方面,外籍人士确实 不构建任何东西(你必须自己做),它只是让你 在解析期间调用特定事件的处理程序。但外籍人士可能会 更快(我没有测量)。

使用 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);
}

使用 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 -lxml2 

*/

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

45
投票

纯汇编器编写一个怎么样:-) 不要忘记查看基准测试


41
投票

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

如果您可以使用 C++,也可以使用 Xerces-C++


10
投票

您可以尝试ezxml——它是一个完全用 C 编写的轻量级解析器。

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


7
投票

http://www.minixml.org也很不错。体积小,只有 ANSI C。


4
投票

我个人的偏好是libxml2。它非常容易使用,但我从未费心对其进行基准测试,因为我只将它用于配置文件解析。


2
投票

Expat相当不错。如果没有更多信息,很难给出好的建议。


2
投票

您能透露一下您正在为哪些平台编写内容吗?这应该对“最好”的影响很大。您可能会发现一个超级“xml-foo”库,默认情况下在大多数系统上并不常见。虽然它很棒,但缺少该库可能会阻止(或至少)惹恼用户。

大多数情况下,我使用 libxml2 .. 因为它是标准的或易于安装在我的目标平台上。

如您所见,“最佳”还取决于目标平台上可用的库。


2
投票

对于 C++,我建议使用 CMarkup


0
投票

在 Windows 上,它是原生的 Win32 api...

© www.soinside.com 2019 - 2024. All rights reserved.