Search code examples
c++xmlboostboost-propertytree

Boost::ptree parser can't read the container from the xml file


I need to make a simple input-output of the XML file. I'm using the boost::ptree.

struct structure
{
    std::string str;
    int ival;
    std::list<bool> bval;

    void save(const std::string& filename) {
        ptree pt;
        pt.put("main.str", str);
        pt.put("main.ival", ival);

        for (const auto& x : bval)
            pt.put("main.bval.b", x); 

        write_xml(filename, pt);
    }

    void load(const std::string& filename) {
        ptree pt;

        read_xml(filename, pt);

        str = pt.get("main.str", str);
        ival = pt.get("main.ival", ival);

        for(auto& v : pt.get_child("main.bval"))
             bval.insert(v.second.data());
    }
};

int main()
{
    structure b, s;
    s.str = "abc";
    s.ival = 14;
    s.bval = { 1, 0, 0, 1, 0, 1, 0, 0, 0, 1 };

    try
    {
        s.save("zxx.xml");

        b.load("zxx.xml");
    }
    catch (std::exception& exc)
    {
        std::cout << exc.what() << std::endl;
    }
    return 0;
}

The input doesn't work correctly because only one element of the "bval" write in the file. Also, I don't know how to make the output of this container (bval). Please help!


Solution

  • You should simply replace put with add. "Put" assumes that the key should not be recreated.

    With this fix, you can do a roundtrip:

        s.save("zxx.xml");
        b.load("zxx.xml");
        b.save("zxx2.xml");
    

    Now, you can test that zxx.xml is equal to zxx2.xml.

    See it Live On Coliru prints:

    g++ -std=c++11 -Os -Wall -pedantic main.cpp && ./a.out && md5sum *.xml
    8a91a9b67d3ac4518e59d23c90edabb0  zxx.xml
    8a91a9b67d3ac4518e59d23c90edabb0  zxx2.xml
    

    Listing

    #include <boost/property_tree/ptree.hpp>
    #include <boost/property_tree/xml_parser.hpp>
    #include <list>
    #include <iostream>
    
    using boost::property_tree::ptree;
    
    struct structure
    {
        std::string str;
        int ival;
        std::list<bool> bval;
    
        void save(const std::string& filename) {
            ptree pt;
            pt.put("main.str", str);
            pt.put("main.ival", ival);
    
            for (const auto& x : bval)
                pt.add("main.bval.b", x); 
    
            write_xml(filename, pt);
        }
    
        void load(const std::string& filename) {
            ptree pt;
    
            read_xml(filename, pt);
    
            str = pt.get("main.str", str);
            ival = pt.get("main.ival", ival);
    
            for(auto& v : pt.get_child("main.bval"))
            {
                 bval.push_back(v.second.get<bool>(""));
            }
        }
    };
    
    int main()
    {
        structure b, s;
        s.str = "abc";
        s.ival = 14;
        s.bval = { 1, 0, 0, 1, 0, 1, 0, 0, 0, 1 };
    
        try
        {
            s.save("zxx.xml");
    
            b.load("zxx.xml");
    
            b.save("zxx2.xml");
        }
        catch (std::exception& exc)
        {
            std::cout << exc.what() << std::endl;
        }
        return 0;
    }