Question

how to remove xml attribute title using boost's ptree? I had one xml, I tried followed code to remove attribute title and save to new xml but failed(new.xml still had title attribute).

xml:

<?xml version="1.0" encoding="utf-8"?>
<tokens title="issues"></tokens>

the code:

 ptree pt;
 read_xml("C://old.xml", pt);

 pt.erase("tokens.<xmlattr>.title"); //try one
 pt.erase("tokens.<xmlattr>"); //try two

 write_xml("C://new.xml", pt);

is there no any methods for boost ptree to remove attribute?

Was it helpful?

Solution

You can try something like this (with proper error handling):

#include <boost/property_tree/xml_parser.hpp>
#include <boost/property_tree/exceptions.hpp>
#include <sstream>
#include <iostream>

using namespace boost;
using namespace  boost::property_tree;
using namespace std;


int main()
{
    ptree pt;
    {
        std::stringstream strm;
        strm << "<?xml version='1.0' encoding='utf-8'?> <tokens title='issues'></tokens>";
        read_xml(strm, pt);
    }

    pt.find("tokens")->second.erase("<xmlattr>");

    write_xml(cout, pt, xml_writer_make_settings(' ', 2));
}

Output:

$ ./test
<?xml version="1.0" encoding="utf-8"?>
<tokens/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top