What is the best open XML parser for C++? [duplicate]
Asked Answered
P

12

252

I am looking for a simple, clean, correct XML parser to use in my C++ project. Should I write my own?

Photophore answered 4/10, 2008 at 17:21 Comment(1)
Note that the much newer StackOverflow posting I reference above has nearly as many upvotes as the current question (as of Dec 2014), and the answer has many more upvotes than the answers here and has a fantastic, easy-to-read flow chart.Lylelyles
F
126

How about RapidXML? RapidXML is a very fast and small XML DOM parser written in C++. It is aimed primarily at embedded environments, computer games, or any other applications where available memory or CPU processing power comes at a premium. RapidXML is licensed under Boost Software License and its source code is freely available.

Features

  • Parsing speed (including DOM tree building) approaching speed of strlen function executed on the same data.
  • On a modern CPU (as of 2008) the parser throughput is about 1 billion characters per second. See Performance section in the Online Manual.
  • Small memory footprint of the code and created DOM trees.
  • A headers-only implementation, simplifying the integration process.
  • Simple license that allows use for almost any purpose, both commercial and non-commercial, without any obligations.
  • Supports UTF-8 and partially UTF-16, UTF-32 encodings.
  • Portable source code with no dependencies other than a very small subset of C++ Standard Library.
  • This subset is so small that it can be easily emulated manually if use of standard library is undesired.

Limitations

  • The parser ignores DOCTYPE declarations.
  • There is no support for XML namespaces.
  • The parser does not check for character validity.
  • The interface of the parser does not conform to DOM specification.
  • The parser does not check for attribute uniqueness.

Source: wikipedia.org://Rapidxml


Depending on you use, you may use an XML Data Binding? CodeSynthesis XSD is an XML Data Binding compiler for C++ developed by Code Synthesis and dual-licensed under the GNU GPL and a proprietary license. Given an XML instance specification (XML Schema), it generates C++ classes that represent the given vocabulary as well as parsing and serialization code.

One of the unique features of CodeSynthesis XSD is its support for two different XML Schema to C++ mappings: in-memory C++/Tree and stream-oriented C++/Parser. The C++/Tree mapping is a traditional mapping with a tree-like, in-memory data structure. C++/Parser is a new, SAX-like mapping which represents the information stored in XML instance documents as a hierarchy of vocabulary-specific parsing events. In comparison to C++/Tree, the C++/Parser mapping allows one to handle large XML documents that would not fit in memory, perform stream-oriented processing, or use an existing in-memory representation.

Source: wikipedia.org://CodeSynthesis XSD

Fortunia answered 4/10, 2008 at 19:49 Comment(10)
I like the headers-only approach (I think you really need one header file). Just throw it in and don't worry about changing anything in your build process.Rubber
Hmmh. if "The parser does not check for character validity" and "The parser does not check for attribute uniqueness", it is, strictly speaking, NOT an xml parser -- these are not optional checks, mandated by xml spec itself. I would not waste my time on such a thing as there are actual good decent parsers too (libxml2 for example)_Kindless
It's the reason I use Rapidxml. One system I work with insists on putting illegal trailing spaces on the element names - rapidXML is the only one that can cope with this (admittedly by not noticing!)Saransk
rapidxml having many functionality to implement a xml ,like msxml .But node traversing is very difficult than other parser...and also file read and write ...Metralgia
+1 for RapidXML. Great for XML message building in my application.Radionuclide
When choosing an XML parser for commercial use (in a certain kind of domain), we need to see if the parser will be maintained for at least 2 or 3 decades. Something like Xerces seems more likely to remain supported and maintained, than RapidXML. So would RapidXML be a wise choice to use?Osteoclast
the beginning of this post is a direct copy from wikipeidaUlrikeulster
RapidXML manual explicitly clarifies that they don't perform string decoding. It seems to be something about XML special character escape, but actually I really don't get what is means.Torques
I have rejected this library from use because - it did not provide API function for loading .xml from file. Also unicode support is questionable - end-user needs to explicitly define which encoding you use - meanwhile it should be probed from xml. It's fast probably as metrics shows, but currently I prefer usability and complete implementation over performance.Trifid
Hasn't had a release since 2009, doesn't compile with GCC if your use the printing... does not inspire confidence at all.Lehmbruck
C
114

pugixml - Light-weight, simple and fast XML parser for C++ Very small (comparable to RapidXML), very fast (comparable to RapidXML), very easy to use (better than RapidXML).

Chloras answered 1/8, 2010 at 3:27 Comment(8)
Wow, that’s a lot of claims. Can you back those up? What makes it better in those areas? Any reference articles?Karie
Reading a bit on the RapidXML as well as pugixml websites I understand what you (probably) mean. RapidXML is based on / inspired by pugixml. It has minimal documentation on parsing. pugixml has good documentation on parsing and nice API. (Only read about parsing so far.)Karie
Pugixml is a lot easier to use, let's take reading xml from file - it's just load_file("file.xml")! I find it a lot more intuitive than rapid_xml. Selecting nodes by xpath also works pretty nice.Boldface
I've been using pugixml for a few years. Works well, easy to integrate into projects, decent docs. BUT, no matter what package you use, XML composing/parsing in C++ is always a messy affair.Individualism
pugixml is an excellent package. The document composition API is a bit clunky (though in fairness this criticism applies to just about every package I've seen!), but the Xpath support is a huge plus.Footfall
I'm looking for a light XML DOM parser I could compile for multiple platforms. In the past I used expat (SAX) for Windows, staying away from the bloated Apache Xerces DOM parser, but I followed @Zbyl's recommendation, and I'm happy I did. Consisting of only one CPP and two H files, with a CMakeList.txt file all ready, has made my life as simple as possible. pugixml's simple API is exactly what I need. Zbyl gets my upvote!Cressida
@Karie I have tested a few XML parsers including a few commercial ones before using [pugixml] (pugixml.org) in a commercial product.Bartholomeo
Simple to integrate (2 headers + source). Unicode is supported. Small fingerprint. I've took this library over rapidxml and tinyxml.Trifid
I
43

Try TinyXML.

http://sourceforge.net/projects/tinyxml

Ichthyoid answered 4/10, 2008 at 17:22 Comment(6)
Used tinyXML several times on VC++ and eVC++ - always worked fineBreechblock
or use TinyXML 2 grinninglizard.com/tinyxml2/index.htmlDiverse
I am trying this out, and for some reason the classes I call from tinyxml2 get a not resolved error. Any idea why? I found the classes in the header file which I included, so they should be available.Raimes
I have rejected this library (Also checked TinyXML2) from use because - library did not provide loading from unicode path names. Also currently I prefer usability and complete implementation over performance.Trifid
@Diverse That link doesn't work anymore ("Account Suspended", d'oh!)Dalton
github.com/leethomason/tinyxml2Diverse
K
16

TiCPP is a "more c++" version of TinyXML.

'TiCPP' is short for the official name TinyXML++. It is a completely new interface to TinyXML (http://www.grinninglizard.com/tinyxml/) that uses MANY of the C++ strengths. Templates, exceptions, and much better error handling. It is also fully documented in doxygen. It is really cool because this version let's you interface tiny the exact same way as before or you can choose to use the new 'ticpp' classes. All you need to do is define TIXML_USE_TICPP. It has been tested in VC 6.0, VC 7.0, VC 7.1, VC 8.0, MinGW gcc 3.4.5, and in Linux GNU gcc 3+

Kenyon answered 4/10, 2008 at 18:27 Comment(1)
The TiCPP link here says to instead use this one, and the tinyxml link doesn't work anymore ("Account Suspended", d'oh!)Dalton
P
14

try this one: http://www.applied-mathematics.net/tools/xmlParser.html
it's easier and faster than RapidXML or PUGXML.
TinyXML is the worst of the "simple parser".

Profusive answered 18/8, 2010 at 9:55 Comment(2)
They made a newer one: applied-mathematics.net/tools/IXMLParser.htmlCircuitry
Just a warning though, to those who are checking it out as I am: the newer version has a really odd license and you can't even download it without first sending him an email. I think I'll go with pugixml.Circuitry
M
12

Do not use TinyXML if you're concerned about efficiency/memory management (it tends to allocate lots of tiny blocks). My personal favourite is RapidXML.

Monteria answered 4/10, 2008 at 19:48 Comment(0)
W
10

How about gSOAP? It is open source and freely available under the GPL license. Despite its name, the gSOAP toolkit is a generic XML data binding tool and allows you to bind your C and C++ data to XML automatically. There is no need to use an XML parser API, just let it read/write your data in XML format for you. If you really need a super-simple C++ XML parser then gSOAP may be an overkill. But for everything else it has worked well as testimonials show for many industrial applications since gSOAP was introduced in 2001.

Here is a brief list of features:

  • Portable: Windows, Linux, Mac OS X, Unix, VxWorks, Symbian, Palm OS, WinCE, etc.
  • Small footprint: 73KB code and less than 2K data to implement an XML web service client app (no DOM to limit memory usage).
  • Fast: do not believe what other tools claim, the true speed should be measured with I/O. For gSOAP it is over 3000 roundtrip XML messages over TCP/IP. XML parsing overhead is negligible as it is a simple linear scan of the input/output while (de)serialization takes place.
  • XML support: XML schema (XSD) import/export, WSDL import/export, XML namespaces, XML canonicalization, XML with attachments (MIME), optional use of DOM, many options to produce XML with indentation, use UTF8 strings, etc.
  • XML validation: partial and full (option)
  • WS support: WS-Security, WS-ReliableMessaging, WS-Addressing, WS-Policy, WS-SecurityPolicy, and other.
  • Debugging: integrated memory management with leak detection, logging.
  • API: no API to learn, only "soap" engine context initialization, then use the read/write interface for your data, and "soap" engine context destruction.

For example:

class Address
{ 
  std::string name;
  std::vector<LONG64> number;
  time_t date;
};

Then run "soapcpp2" on the Address class declaration above to generate the soap_read_Address and soap_write_Address XML reader and writer, for example:

Address *a = new Address();
a = ...;
soap ctx = soap_new();
soap_write_Address(ctx, a);
soap_end(ctx);
soap_free(ctx);`

This produces an XML representation of the Address a object. By annotating the header file declarations with XML namespace details (not shown here), the tools also generate schemas. This is a simple example. The gSOAP tools can handle a very broad range of C and C++ data types, including pointer-based linked structures and even (cyclic) graphs (rather than just trees).

Hope this helps.

Wynd answered 12/5, 2010 at 19:23 Comment(1)
For commercial use you have to pay one time fee for gSoapCulet
R
9

TinyXML can be best for simple XML work but if you need more features then try Xerces from the apache project. Go to the following page to read more about its features.

http://xerces.apache.org/xerces-c/

Ritornello answered 4/10, 2008 at 17:30 Comment(4)
What features does Xerces have that TinyXML doesn't?Photophore
OK, more to the point which of those features doesn't TinyXML have?Photophore
It implements the whole DOM. TinyXML is simpler, but enough for keeping data in XML.Kilderkin
Xerces implments the ENTIRe xml standard. TinyXML implments just enough to be useful. It turns out that 99% or users will only ever use 1% of the XML standard, so TinyXML is usually more that sufficient.Hertz
K
9

TinyXML, and also Boost.PropertyTree. The latter does not fulfill all official requirements, but is very simple.

Kilderkin answered 4/10, 2008 at 18:49 Comment(5)
Boost.PropertyTree was perfect for my kind of simple data storage. This is the page that made it clear how to use it. Wow, I love boost.Guyton
Boost PropertyTree is not that useful except in trivial XML files. The structure doesn't have backward linking so getting to parents of nodes means you really need to roll your own data structure to store the XML after Property Tree reads it. And it has no query support of the xpath nature. All you can do easily is read in an XML file into a tree structure and directly pull out a value if you know the exact path.Flopeared
I like the boost::property_tree too. There are some practical Visual Studio implementations of how to parse XML and JSONPinup
boost::property_tree is very bloated (increases compile time and executable size) and doesn't seem to be maintained anymore. Not recommended.Charlettecharley
I have rejected this library (Also checked TinyXML2) from use because - library did not provide loading from unicode path names. Also currently I prefer usability and complete implementation over performance.Trifid
E
9

I am a C++ newbie and after trying a couple different suggestions on this page I must say I like pugixml the most. It has easy to understand documentation and a high level API which was all I was looking for.

Expeditious answered 9/9, 2010 at 11:45 Comment(0)
U
7

I like the Gnome xml parser. It's open source (MIT License, so you can use it in commercial products), fast and has DOM and SAX based interfaces.

http://xmlsoft.org/

Unformed answered 4/10, 2008 at 17:59 Comment(1)
You happen to be using CodeBlocks? Im trying to get the c++ wrapper for this up and running and it's giving me fits.Mameluke
T
2

Try TinyXML or IrrXML...Both are lightweight XML parsers ( I'd suggest you to use TinyXML, anyway ).

Tarrance answered 4/10, 2008 at 17:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.