C语言 一个简单的 C XML 解析器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2851983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
A simple C XML parser
提问by Jessica
I need to read an XML formatted document from a C program and extract from it the elements and their values. For example in the following code:
我需要从 C 程序中读取 XML 格式的文档并从中提取元素及其值。例如在以下代码中:
<user name="Mark">
<param name="Age" value="21"/>
<param name="Country" value="NL"/>
</user>
I need to extract: name = Mark, Age = 21and Country = NL.
我需要提取:name = Mark,Age = 21和Country = NL.
Up until today I've been doing this parsing manually which is a pain.
直到今天,我一直在手动进行解析,这很痛苦。
I don't care whether the file a "proper XML" or all that, I don't care about DTD's or other standard XML requirements. I just need to read and parse the values.
我不在乎文件是“正确的 XML”还是其他所有内容,我不在乎 DTD 或其他标准 XML 要求。我只需要读取和解析值。
Does anyone know of a library other than lib eXpat to do this or code to do this?
有没有人知道除 lib eXpat 之外的库可以执行此操作或代码执行此操作?
Thanks!
谢谢!
采纳答案by Jessica
回答by Jessica
The Expatparser is the best I've come across - I use it in my C++ code in preference to the various C++ parsers - but it is written in C. Very easy to use and embed in your application. So I don't see why in your question you say:
该外籍解析器是我遇到的最好的-我用它在我的C ++代码优先于各种C ++解析器-但它是用C写的非常易于使用,并在你的应用程序中嵌入。所以我不明白为什么在你的问题中你说:
(other than lib eXpat)
(除了 lib eXpat)
do you have something against it?
你有什么反对的吗?
回答by code4life
How about Mini-XML? It's lightweight, works with gcc, is ANSI-C compatible...
Mini-XML 怎么样?它是轻量级的,可与 gcc 配合使用,与 ANSI-C 兼容...
http://www.minixml.org/index.php
http://www.minixml.org/index.php
According to the documentation, to search for specific nodes would be as simple as:
根据文档,搜索特定节点将非常简单:
/* Find the first "a" element */
node = mxmlFindElement(tree, tree, "a",
NULL, NULL,
MXML_DESCEND);
Once you get the node, you can manipulate it according to your requirements.
获得节点后,您可以根据需要对其进行操作。

