1

I saw a few libraries through a quick Google search. What's generally the most commonly used XML implementation for C++?

I'm planning on using XML as a means for program configuration. I liked XML because I'll be making use of its tree-like structure. If you think you have a more suitable solution for this, feel free to mention it. I want something lightweight and simple. Maybe XML is too much?

Edit: Cross-platform would be preferable, but to answer a question, I'm programming this in Linux.

Scott
  • 5,135
  • 12
  • 57
  • 74
  • Which os platform do you intend to use? //Buzzz – Buzzzz Oct 09 '09 at 06:25
  • Linux, but cross-platform would be preferable. – Scott Oct 09 '09 at 06:27
  • This really shouldn't matter unless you plan on recommending AsmXML: http://mkerbiquet.free.fr/asm-xml/ – Chris Lutz Oct 09 '09 at 06:53
  • Well if you are on a windows platform you can use MSXML which I guess isn't available under linux. – Buzzzz Oct 12 '09 at 06:33
  • I've used [CMarkup](http://www.firstobject.com/dn_markup.htm) and found it to be easy to understand and very efficient. But, this question is off-topic. – Adrian Mole Jun 13 '22 at 12:09
  • In public interest, I would like to revive this question. It is year 2022, boost still doesn't have any support for xml parsers and general features you can expect. Standard library has missed the bus like so many other things in Cpp land. Despite all the claims of how json superseded xml years ago, for those of us which still need to integrate to legacy systems it is no consolation to hear the virtues of other data formats when you are stuck with xml. – Tanveer Badar Sep 21 '22 at 05:39

6 Answers6

7

See if TinyXML helps you

TinyXML is a simple, small, C++ XML parser that can be easily integrating into other programs.

Satbir
  • 6,358
  • 6
  • 37
  • 52
  • There is a more C++ friendly version too, TiCPP. http://code.google.com/p/ticpp/ – Rob Oct 09 '09 at 07:47
  • Before choosing TinyXML take a look at the http://pugixml.org/benchmark.html. I would choose [pugixml](http://pugixml.org/) a light-weight C++ XML processing library. The library is extremely portable, easy to integrate and use! – sg7 Jun 09 '16 at 00:53
3

There are several out there:

Xercers   Big    http://xerces.apache.org/xerces-c/  
expat     small  http://expat.sourceforge.net/

I like expat. But that's a totally personal opinion.
I use it because it is small and it was simple to write a C++ wrapper for.

Xerces is like the full blown XML parser with all the knobs a whistles. But consequently it is slightly more complex to use.

Martin York
  • 257,169
  • 86
  • 333
  • 562
  • Expat is purely a parser - it reads the input and calls callbacks for particular kinds of syntax (element, attribute etc). You register the callbacks for the events you're interested in. It's at a similar level to SAX, but doesn't follow SAX conventions. There's no output support, but output is a simpler job anyway - though there are obvious character set/encoding issues. –  Feb 20 '10 at 08:56
2

Not quite the question you asked, but there are two major flavors of XML parsers, SAX and DOM.

SAX parsers are event driven parsers. As the parser sees various elements with the XML document (node, properties, etc.), the parser calls some function or method that you have defined.

DOM parsers on the other hand parse the entire XML document and return a tree structure that represents the entire document. Your code can then poke through the structure in any order it sees.

SAX parsers are more memory efficient because they do not need to represent the entire document in memory. DOM parsers are easier to work with because you are not limited to processing the document in a linear fashion.

R Samuel Klatchko
  • 74,869
  • 16
  • 134
  • 187
  • Worthwhile note - Expat is SAX-like in this scheme, but is not SAX. SAX was originally a Java library, but it's API became the model for a kind of multi-language standard. Expat was always C, and (I think) predates SAX. –  Oct 09 '09 at 08:01
2

I would recommend not using XML.

I know this is a matter of opinion but XML really clutters the information with a lot of tags. Also, even though it is human-readable, the clutter actually hampers readability (and I say it from experience since we have some 134 XML configuration files at the moment...). Furthermore, it is quite difficult to read because of the mix between attributes and plain-text. You never know which one you are going to need.

I would recommend using JSON, if you want a language that already has well-defined parsers.

For parsing, a simple look at json.org and you have a long list of C++ libraries.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
  • Use or not use xml much depends on the application and not the taste of the programmer. – lsalamon Oct 09 '09 at 12:39
  • If the author wanted to know answer on **your** question (Should I use XML?) he would have asked this question not the one he asked. -1 – Piotr Dobrogost Oct 09 '09 at 13:40
  • Piotr, actually I did leave the question open for an answer like this. Read it again. :) – Scott Oct 09 '09 at 21:28
  • Incidentally, I think I'm going to use JSON, Matthieu. I'm embedding Javascript, so JSON really makes sense for configuration. – Scott Oct 09 '09 at 22:39
  • Though XML may have originally been designed as Human readable its not really (But the XML readers which are ubiqitos make it trivial to read). But that is not why XML is usefull. It because of its machine readability that it is usfull and the fact that if hard pressed you can go into and hack the XML if pushed and be pretty secure you have not invalidated the whole file. Thes same can not be said for Jason which is definately not Human readable or hackable without major concentration. – Martin York Oct 12 '09 at 18:52
0

I highly recommend pugixml

"pugixml is a C++ XML processing library, which consists of a DOM-like interface with rich traversal/modification capabilities, an extremely fast XML parser which constructs the DOM tree from an XML file/buffer, and an XPath 1.0 implementation for complex data-driven tree queries. Full Unicode support is also available, with Unicode interface variants and conversions between different Unicode encodings."

I have tested a few XML parsers including a few commercial ones before choosing and using pugixml in a commercial product.

pugixml was not only the fastest (sometimes a few times faster) parser but also had the most mature and friendly API. I highly recommend it. It is very stable product! I have started to use it since version 0.8. Now it is 1.7.

The great bonus in this parser is XPath 1.0 implementation! For any more complex tree queries the XPath is a God sent feature!

DOM-like interface with rich traversal/modification capabilities is extremely useful to tackle a real life "heavy" XML files.

It is small and fast parser. It is good choice for iOS or Android app if you do not mind linking C++ code.

I also tested TinyXML. It was not only slower but it had problems with my XML files.

Benchmarks tell a lot: http://pugixml.org/benchmark.html

sg7
  • 6,108
  • 2
  • 32
  • 40
0

The XML libraries I've used and are still using are:

If you don't need to use XML then I would suggest not doing so.

I would also avoid modelling what you are reading/writing as C++ classes unless you are using a code generator.

I would also look at using a 'schema to code' generating for reading/writing, though make sure that the licence fits what you are doing.

Damian Dixon
  • 889
  • 8
  • 21