Using Boost to read and write XML files

Viewed 106396

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 a simple sample that uses Boost for reading and writing XML files?

If not Boost, is there any good and simple library to read and write XML files that you can recommend? (it must be a C++ library)

15 Answers

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.

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.

Boost does not provide an XML parser atm.

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

Definatelly use TinyXML *thumbs up*

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.

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.

A warning. I love RapidXML, but it has a very nasty bug when parsing UTF16. Some valid values cause it to crash.

I would love to recommend pugixml - but it lacks namespace support, which I know is going to cause me problems.

<?xml version="1.0"?>
<Settings>
  <GroupA>
      <One>4</One>
      <Two>7</Two>
      <Three>9</Three> 
  </GroupA>
  <GroupA>
      <One>454</One>
      <Two>47</Two>
      <Three>29</Three> 
  </GroupA>
  <GroupB>
      <A>String A</A>
      <B>String B</B>  
  </GroupB>  
</Settings>

There is an easy way to read XML with BOOST. This example is with std::wstring:

#include <string> 
#include <boost/property_tree/xml_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/foreach.hpp>

bool CMyClass::ReadXML(std::wstring &full_path)
{
    using boost::property_tree::wptree;

    // Populate tree structure pt:
    wptree pt;
    std::wstringstream ss; ss << load_text_file(full_path); // See below for ref.
    read_xml(ss, pt);

    // Traverse pt:
    BOOST_FOREACH(wptree::value_type const& v, pt.get_child(L"Settings"))
    {
        if (v.first == L"GroupA")
        {
            unsigned int n1 = v.second.get<unsigned int>(L"One");
            unsigned int n2 = v.second.get<unsigned int>(L"Two");
            unsigned int n3= v.second.get<unsigned int>(L"Three");
        }
        else if (v.first == L"GroupB")
        {
            std::wstring wstrA = v.second.get<std::wstring>(L"A");
            std::wstring wstrB = v.second.get<std::wstring>(L"B");
        }
    };
}

To read attributes is just a little bit more complicated.

-

Just for the reference:

std::wstring load_text_file(std::wstring &full_path)
{
    std::wifstream wif(full_path);

    wif.seekg(0, std::ios::end);
    buffer.resize(wif.tellg());
    wif.seekg(0);
    wif.read(buffer.data(), buffer.size());

    return buffer;
}
Related