tags:

views:

707

answers:

6
+3  Q: 

XML APIs in C?

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.

+4  A: 

Xerces is known to be easy, give it a try

hhafez
Nice, I just found it.
Tommy
+3  A: 

MiniXML might be what you're looking for, if you need something simple, easy, and C:

Mini-XML: Lightweight XML Library

Mini-XML is a small XML library that you can use to read and write XML and XML-like data files in your application without requiring large non-standard libraries. Mini-XML only requires an ANSI C compatible compiler (GCC works, as do most vendors' ANSI C compilers) and a 'make' program.

However, there are a ton of them ranging in complexity and needs. MiniXML is nice in that it doesn't require more than a simple Ansi C compiler. A lot of them require other libraries, or specific compilers.

Adam Davis
+2  A: 

The lightest way to produce XML in C is printf. There are definitely plenty of possible issues that could arise by doing it all yourself though, such as properly escaping xml entities.

consultutah
bortzmeyer
No disagreement about the likelihood of errors - you are just echoing what I said.
consultutah
+1  A: 

The easiest way to make XML in C is the high-quality and free genx from Tim Bray: http://www.tbray.org/ongoing/When/200x/2004/02/20/GenxStatus

dajobe
A: 

[link text][1]

[1]: http://xmlsoft.org/ "libxml2" is an extensive C API for XML. [link text][1]

[1]: http://www.aleksey.com/xmlsec/ "xmlsec" is a nice security compliment on top of libxml2.

Crowley
Ther markup of this answer would deserve a check...
bortzmeyer
Yep, but the fact that I'm too lame to figure out how to use the links shouldn't detract from looking into both these libraries. They're both quite good.
Crowley
+6  A: 

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.

bortzmeyer
Very cool, thanks.
Tommy