22

I'm working on a project for an embedded system that's using XML for getting data into and out of the system. I don't want the XML handling to devolve into a bunch of bits that build XML strings using snprintf()/strcat() and friends or parse XML by counting "<" and ">" characters.

I've found several XML libraries, a couple of which might even be small enough, but the closest they come to C is C++, which is not in the cards for this system. I hoping I can find an XML library that meets the following constraints:

  • C source code
  • no dynamic memory allocation
  • cheap. Free is better, but copyleft won't do the trick.

It doesn't have to be a full parser - I just want to be able to pull text out of nested elements and have a reasonably simple way to generate XML that doesn't rely on format strings. Attributes aren't being used (yet), so the library doesn't need to support them even. The XML documents will be pretty small, so something that's DOM-like would be fine, as long as it'll work with client-provided buffers (parsing the raw XML in-place would be nice).

PugXML and TinyXML look to be pretty close, but I'm hoping that someone out there knows about an XML lib tailored just for C-based embedded systems that my googling is missing.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • 1
    It's possible that the authors of some "copyleft" libraries might be willing and able to sell you an alternative commercial use license to their code. – caf Jul 16 '09 at 08:23
  • 7
    If I were in your position, due to the constraints of embedded systems, I'd be seriously considering alternatives to XML. Depending on the nature of the data, there could well be better ways to encapsulate the data that don't require so much processing overhead both for generation and parsing. – Craig McQueen Jul 19 '09 at 05:46
  • We had similar situation and couldn't find any small lib so I ended up writing one for embedded system with binary size < 20K but it uses heap. It supports only ASCII chars with namespace. Wish I could help you but its proprietary :( – Manish Jan 21 '11 at 06:23
  • 1
    I have realized JSON is a good alternative and does require nucht less effert parsing than XML. A parser would not be that compicated to write and you might get along only with stack, but no heap easily. – too honest for this site May 12 '15 at 15:49
  • I've tried [ezxml ][1] it's very easy and perfect C library for embedded system, you can use it for both building and parsing XML [1]: http://ezxml.sourceforge.net/ – Abdessamad Doughri Apr 11 '16 at 15:07

6 Answers6

13

I don't know about dynamic memory allocation, but a standard C XML parser is expat, which is the underlying library for a number of parsers out there.

jvasak
  • 1,843
  • 13
  • 14
  • I have always had good experiences with Expat. It written in standard C so should port to any platform fairly easily. Its a stream parser so it shouldnt eat all your memory building a DOM. You need to get used to the 'callback' API but its pretty simple when you get the hang of it. Recommended! – James Anderson Aug 25 '09 at 07:45
  • Expat does do some dynamic memory allocation. The code is quite localised though, and you might be able to reimplement. It's a very good XML parser. –  Aug 25 '09 at 07:50
  • 1
    If you use the `XML_ParserCreate_MM` to create a parser, it will use the implementations of malloc and free you provide. – Pete Kirkham Sep 18 '10 at 09:41
4

I am not sure but perhaps Mini-XML: Lightweight XML Library will help you:

  1. Mini-XML only requires an ANSI C compatible compiler.
  2. It is freeware.
  3. Its binary size is around 100k.
  4. It parses the whole xml-file and then store all the info into a linked list.
FooF
  • 4,323
  • 2
  • 31
  • 47
Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
1

You could use an ASN.1 XER encoder; there's a free one at http://lionet.info/asn1c/

Christoffer
  • 12,712
  • 7
  • 37
  • 53
1

You could use the one from Gnome.

Joel B
  • 12,082
  • 10
  • 61
  • 69
1

I have written sxmlc to be as simple as possible and I know people use it in routers, to perform in-place parsing of web queries.

Unfortunately (and I'm 5 years late...) it does use memory allocation, though kept at a minimum: one buffer to read each "XML line" (what lies between < and >, sorry ;)), and many small buffer to keep track of the tag name, attributes names and values, and text (though I always wanted to use char[16] or so for those).

And it makes use of strdup/strcpy and such.

As I want that anybody can use it freely, the licence is BSD.

Matthieu
  • 2,736
  • 4
  • 57
  • 87
0

Xerces-C library would be optimal to use, in this scenario.

If it is going to be pretty small XML, why not generate programatically, using sprintf or other stuff and use string extracting functions to parse the same. But as mentioned earlier, if little big, would suggest to use Xerces-c Library, as it is open source

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Roopesh Majeti
  • 556
  • 1
  • 11
  • 23