tags:

views:

8899

answers:

11
+15  Q: 

Boost and XML (c++)

Hi,

Is there any good way (and a simple way too) using boost to read and write xml files?

I can't seem to find any simple sample to read xml files using boost? (can you point me some simple sample that use boost for reading and writing xml files)

If not boost, is out there any good and simple library to read and write xml files that you can recommend? (it must be a c++ library)

Thanks Nuno

+3  A: 

Well there is no specific library in boost for XML parsing, but there are lots of alternatives, here are a couple: libxml, Xerces, Expat

Of course you could use some of the other libraries in boost to aid you in making your own library, but that will probably be quite an undertaking.

And here is a whole article on the subject by IBM.

Skurmedel
+11  A: 

There's also TinyXML, which is a nice and small C++ library. If you are looking for a lower-level library, RapidXML is a great starting point.

Anteru
+3  A: 

Definatelly use TinyXML *thumbs up*

StfnoPad
+1  A: 

From my experiences lurking on the Boost mailing list, it appears that every time XML comes up as a subject, it is diverted into a discussion about Unicode. However, since there is a potential Unicode library looming right now, I don't think it will take too long for an XML library to appear there.

In the meantime, I too have been using TinyXML.

Interesting link about RapidXML. I'll take a look at that.

Kaz Dragon
A: 

Boost does not provide an XML parser atm.

Poco XML (part of the Poco C++ libs) is good and simple.

StackedCrooked
+5  A: 

TinyXML is probably a good choice. As for Boost:

There is the Property_Tree library in the Boost Repository. It has been accepted, but support seems to be lacking at the moment (EDIT: Property_Tree is now part of Boost since version 1.41, read the documentation regarding its XML functionality).

Daniel Nuffer has implemented an xml parser for Boost Spirit.

stephan
+1  A: 

Take a look at Arabica

Nemanja Trifunovic
+1  A: 

If you are looking for DOM functionality only, there are some suggestions already in this thread. I personally would probably not bother with a library lacking XPath support, and in C++, would use Qt. There's also TinyXPath, and Arabica claims to have XPath support, but I cannot say anything at all about those.

Vladimir Prus
+13  A: 

You should Try pugixml Light-weight, simple and fast XML parser for C++

The nicest thing about pugixml is the XPath support, which TinyXML and RapidXML lack.

Quoting RapidXML's author "I would like to thank Arseny Kapoulkine for his work on pugixml, which was an inspiration for this project" and "5% - 30% faster than pugixml, the fastest XML parser I know of" He had tested against version 0.3 of pugixml, which has reached recently version 0.42.

Here is an excerpt from pugixml documentation:

The main features are:

  • low memory consumption and fragmentation (the win over pugxml is ~1.3 times, TinyXML - ~2.5 times, Xerces (DOM) - ~4.3 times 1). Exact numbers can be seen in Comparison with existing parsers section.
  • extremely high parsing speed (the win over pugxml is ~6 times, TinyXML - ~10 times, Xerces-DOM - ~17.6 times 1
  • extremely high parsing speed (well, I'm repeating myself, but it's so fast, that it outperforms Expat by 2.8 times on test XML) 2
  • more or less standard-conformant (it will parse any standard-compliant file correctly, with the exception of DTD related issues)
  • pretty much error-ignorant (it will not choke on something like You & Me, like expat will; it will parse files with data in wrong encoding; and so on)
  • clean interface (a heavily refactored pugxml's one)
  • more or less Unicode-aware (actually, it assumes UTF-8 encoding of the input data, though it will readily work with ANSI - no UTF-16 for now (see Future work), with helper conversion functions (UTF-8 <-> UTF-16/32 (whatever is the default for std::wstring & wchar_t))
  • fully standard compliant C++ code (approved by Comeau strict mode); the library is multiplatform (see reference for platforms list)
  • high flexibility. You can control many aspects of file parsing and DOM tree building via parsing options.

Okay, you might ask - what's the catch? Everything is so cute - it's small, fast, robust, clean solution for parsing XML. What is missing? Ok, we are fair developers - so here is a misfeature list:

  • memory consumption. It beats every DOM-based parser that I know of - but when SAX parser comes, there is no chance. You can't process a 2 Gb XML file with less than 4 Gb of memory - and do it fast. Though pugixml behaves better, than all other DOM-based parser, so if you're stuck with DOM, it's not a problem.
  • memory consumption. Ok, I'm repeating myself. Again. When other parsers will allow you to provide XML file in a constant storage (or even as a memory mapped area), pugixml will not. So you'll have to copy the entire data into a non-constant storage. Moreover, it should persist during the parser's lifetime (the reasons for that and more about lifetimes is written below). Again, if you're ok with DOM - it should not be a problem, because the overall memory consumption is less (well, though you'll need a contiguous chunk of memory, which can be a problem).
  • lack of validation, DTD processing, XML namespaces, proper handling of encoding. If you need those - go take MSXML or XercesC or anything like that.
  • lack of UTF-16/32 parsing. This is not implemented for now, but this is the features for the next release.
Cristian Adam
A: 

I'm trying to understand why there can't be a SAX-like parser that runs at these speeds. I need speed the most, when the documents are too big to fit in memory! Any advice? Expat is too slow .... ;) Thanks.

solrize
+2  A: 

It would appear that boost serialization can read from and write-to archives in XML, if that's sufficient for your purposes.

Easier XML with Boost

Stuart
graham.reeds