它们都这么复杂吗? : http://msdn.microsoft.com/en-us/library/ms766497(VS.85).aspx
只需要一些基本的东西就可以在 C 中生成 XML。
最佳答案
我喜欢libxml .这是一个使用示例:
#include <libxml/parser.h>
int
main(void)
{
xmlNodePtr root, node;
xmlDocPtr doc;
xmlChar *xmlbuff;
int buffersize;
/* Create the document. */
doc = xmlNewDoc(BAD_CAST "1.0");
root = xmlNewNode(NULL, BAD_CAST "root");
/* Create some nodes */
node = xmlNewChild(root, NULL, BAD_CAST "node", NULL);
node = xmlNewChild(node, NULL, BAD_CAST "inside", NULL);
node = xmlNewChild(root, NULL, BAD_CAST "othernode", NULL);
/* Put content in a node: note there are special characters so
encoding is necessary! */
xmlNodeSetContent(node,
xmlEncodeSpecialChars(doc, BAD_CAST "text con&tent and <tag>"));
xmlDocSetRootElement(doc, root);
/* Dump the document to a buffer and print it for demonstration purposes. */
xmlDocDumpFormatMemory(doc, &xmlbuff, &buffersize, 1);
printf((char *) xmlbuff);
}
用'gcc -Wall -I/usr/include/libxml2 -c create-xml.c && gcc编译 -lxml2 -o create-xml create-xml.o',这个程序会显示:
% ./create-xml
<?xml version="1.0"?>
<root>
<node>
<inside/>
</node>
<othernode>text con&tent and <tag></othernode>
</root>
有关真实示例,请参阅 my implementation的 RFC 5388 .
关于c - C 中有哪些 XML API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/592954/