C中的XML API?

T.T*_*.T. 4 c xml api

他们都这么复杂吗?:http://msdn.microsoft.com/en-us/library/ms766497(VS.85).aspx

只需要一些基本的东西来用C生成XML.

bor*_*yer 7

我喜欢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);

}
Run Code Online (Sandbox Code Playgroud)

使用'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&amp;tent and &lt;tag&gt;</othernode>
</root>
Run Code Online (Sandbox Code Playgroud)

有关实际示例,请参阅RFC 5388的实现.