Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML APIs in C?

Tags:

c

xml

api

Are they all this complex? : http://msdn.microsoft.com/en-us/library/ms766497(VS.85).aspx

Just need something basic to produce XML in C.

like image 360
T.T.T. Avatar asked Dec 05 '22 06:12

T.T.T.


2 Answers

I like libxml. Here is an example of use:

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

}

Compiled with 'gcc -Wall -I/usr/include/libxml2 -c create-xml.c && gcc -lxml2 -o create-xml create-xml.o', this program will display:

% ./create-xml   
<?xml version="1.0"?>
<root>
  <node>
    <inside/>
  </node>
  <othernode>text con&amp;tent and &lt;tag&gt;</othernode>
</root>

For a real example, see my implementation of RFC 5388.

like image 119
bortzmeyer Avatar answered Jan 01 '23 19:01

bortzmeyer


Xerces is known to be easy, give it a try

like image 22
hhafez Avatar answered Jan 01 '23 21:01

hhafez