2

I'm looking for a simple non-validating XML parser in either C or C++.

Several years back I found one that was just a single file solution but I can't find it anymore.

I'm after some links and suggested ones that are very small and lightweight ideally suited for an embedded platform.

hookenz
  • 36,432
  • 45
  • 177
  • 286

5 Answers5

2

Expat

You can work with or without validation and in "streaming mode". It is very lightweight.

mmmmmm
  • 32,227
  • 27
  • 88
  • 117
Yuval
  • 276
  • 2
  • 6
  • I'm not sure about the memory footprint, but the code size is a good 150k (on x86), which isn't what I'd call lightweight... For memory, the big question would be whether it allocates tons of tiny chunks for each string and each DOM object (which would lead to heavy overhead and possibly memory fragmentation), or whether it keeps lots of data together in larger allocations. Ideally a lightweight XML parser would modify the input string in-place, inserting null terminators where needed, and use that for the output. – R.. GitHub STOP HELPING ICE Dec 16 '10 at 03:31
  • Thanks, but it's too big for my needs. – hookenz Dec 16 '10 at 07:35
1

What about something like pugixml. From their site...

pugixml is a light-weight C++ XML processing library. It features:

  • DOM-like interface with rich traversal/modification capabilities
  • Extremely fast non-validating XML parser which constructs the DOM tree from an XML file/buffer
  • XPath 1.0 implementation for complex data-driven tree queries
  • Full Unicode support with Unicode interface variants and automatic encoding conversions

The library is extremely portable and easy to integrate and use.

pugixml is developed and maintained since 2006 and has many users. All code is distributed under the MIT license, making it completely free to use in both open-source and proprietary applications.

Also, this answer has more info.

There is also tinyxml and RapidXml.

Community
  • 1
  • 1
Andrew White
  • 52,720
  • 19
  • 113
  • 137
1

There is definitely a pure C, tiny xml parser available. It was cited in an earlier answer on SO, but I can't find it right now. If I remember right, it's just a few hundred lines of code.

Update: Here's the question/answer that references it:

Is there a good tiny XML parser for an embedded C project?

And the actual code:

http://mercurial.intuxication.org/hg/cstuff/file/tip/tinyxml

Community
  • 1
  • 1
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
0

RapidXML is a single-header (multiple headers if you want extra functionality) ultra-lightweight, ultra-fast implementation. It can operate in "destructive" mode, that means by setting pointers right into the XML and possibly overwriting some, avoiding all extra memory allocations and data copies.

tinyxml is not precisely single-header, but it is still fairly lightweight compared to other parsers. I've used it for half a decade without ever encountering an issue. The author has recently started with "tinyxml-2", which is supposedly better and even more lightweight, but I've not had occasion to actually try that one yet.

Damon
  • 67,688
  • 20
  • 135
  • 185
-2

http://mercurial.intuxication.org/hg/cstuff/file/tip/tinyxml

can this parser work with nested XML like

<CServiceType>
   <serno>61</serno>
   <caption1 />
   <caption2>Satelite</caption2>
   <caption3 />
</CServiceType>