他们都这么复杂吗? :http://msdn.microsoft.com/zh-cn/library/ms766497(VS.85).aspx
只需一些基本的C代码来生成XML。
只需一些基本的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>
在 C 语言中生成 XML 最简单的方法是使用 Tim Bray 的高质量且免费的 genx:http://www.tbray.org/ongoing/When/200x/2004/02/20/GenxStatus