26

I'm after a very tiny XML parser for an embedded project. It needs to compile down to 10-15k, doesn't need to validate, and needs to be simple and portable.

Joe
  • 269
  • 1
  • 3
  • 4
  • 2
    Similar to [ C XML library for Embedded Systems ](http://stackoverflow.com/questions/1131701/c-xml-library-for-embedded-systems), though I don't see any great answers. – Matthew Flaschen Sep 17 '10 at 22:27
  • 1
    Is XML an absolute requirement? 15k is pretty tight for something you don't hand-roll to meet your requirements, but I'd be surprised if you couldn't find an off-the-shelf JSON parser that was that small. – llasram Sep 18 '10 at 15:44
  • 1
    Hi llasram, thanks for your comment. Yes, XML is a requirement. I solved my problem in the end by doing just that: hand rolling an XML parser that met my requirements. The resultant code came in at only a couple of K... but then again my XML parsing requirements turned out to be rather simple when I studied them closely. – Joe Sep 20 '10 at 17:06

5 Answers5

8

I was able to tweak the compilation flags of the following XML parser libraries for C, and cut down more than 50% of their size on my Ubuntu machine. Mini-XML is the only one close to what you requested:

There's a good discussion here:

C XML library for Embedded Systems

Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426
6

I was searching for one recently and I found SimpleXML (http://simplexml.sourceforge.net/) and the slightly larger sxmlc(http://sourceforge.net/projects/sxmlc/)

I find SimpleXML more interesting because it's simpler, I didn't try it but it looks like it matches what I have in mind, a single file(well .h and .c) library that doesn't support exotic XML features.

The simple XML parser is a tiny parser for a subset of XML (everything except entities and namespaces). It uses a simple "one-handler per tag" interface and is suited for use with devices with limited resources.

Diaa Sami
  • 3,249
  • 24
  • 31
6

Try yxml — it's really small and fast non–validating parser.

Community
  • 1
  • 1
5

You can always roll your own implementation. I did this a few years ago, and just now added some interface documentation to the code at mercurial.intuxication.org/hg/cstuff.

Please note that the parser has never been used in a production environment or even been tested more than rudimentarily; comments are non-existent as well, so have fun grokking the code if you need to modify it ;)

Christoph
  • 164,997
  • 36
  • 182
  • 240
  • Thanks Christoph. That's exactly what I did in the end - hand roll my own implementation. I actually implemented it before your post, but thanks anyway - I'll take a look at your implementation and compare with my own. – Joe Sep 20 '10 at 17:04
  • @Christoph, this looks like exactly what I was looking for—thanks for posting it! – mikepurvis Sep 15 '12 at 13:48
1

I developed sxmlc ("Simple XML in C") exactly to be like that: as little files as possible. It's only one file, with an optional "search" file you can add if you need XPath-like search through the document.

It handles DOM-style loading (the whole document tree in memory) or SAX-style loading (calling callbacks whenever a node is read with its attributes, or text was read on a node). If memory is a concern you'll be interested in SAX.

Some people were also interested by the fact that it can parse either files or memory buffers (useful when you get XML as a web reply).

It handles Unicode files since version 4 through #define, so if you don't need Unicode, just don't define the SXMLC_UNICODE and there won't be any weight increase in the binary.

I also have to say it keeps comments when writing back XML to disk! I always felt sorry when people spend time explaining configuration syntax in XML files ("put 'true' to enable special compression..."), which are wiped when saved back by the application.

It compiles under Linux and Windows. I had good feedback from people happily embedding it in routers.

As I want to keep it as simple as possible, I will probably not add new functions but rather improve the existing ones (and correct bugs, of course! :)). I am not very active in its development, unless bugs are reported.

Matthieu
  • 2,736
  • 4
  • 57
  • 87