1

I am trying to update the value of totalresult attribute in every test_list node found. The issue is, it will only update the first test_list node found.

The testListCount will increment every time a test_list node is added. Once done adding test_list node, each totalresult value will then be updated in every test_list node.

Here is my code:

BOOST_FOREACH(ptree::value_type const & subTree, mainTree.get_child("my_report"))
{
    auto &nodeTestList = mainTree.get_child("my_report.test_list");
    BOOST_FOREACH(ptree::value_type const & subval, nodeTestList)
    {
        ptree subvalTree = subval.second;
        BOOST_FOREACH(ptree::value_type const & paramNode, subvalTree)
        {
            std::string name = paramNode.first;
            if (name == TestListAttrib[TestListParam::TOTALRESULT])
            {
                wxMessageBox("firing!");
                nodeTestList.put("<xmlattr>." + name, testListCount);
            }
        }
    }
}

Below is the actual result:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="report.xsl"?>
<my_report>
    <test_list overall_status="FAILED" result="1" totalresult="3">
    <test_list overall_status="FAILED" result="2" totalresult=""/>
    <test_list overall_status="FAILED" result="3" totalresult=""/>
</my_report>

Below is the expected result:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="report.xsl"?>
<my_report>
    <test_list overall_status="FAILED" result="1" totalresult="3">
    <test_list overall_status="FAILED" result="2" totalresult="3"/>
    <test_list overall_status="FAILED" result="3" totalresult="3"/>
</my_report>
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • 1
    You really shouldn't use Boost.PropertyTree as a generic XML parser. It's XML parsing functionality mainly exists for serializing PropertyTree's too/from XML, not for reading XML written by arbitrary processes. – Nicol Bolas Aug 15 '20 at 05:15
  • I'll take note of that @NicolBolas. Thanks for the heads up buddy. – eager_coder Aug 16 '20 at 15:27

1 Answers1

0

Like others said, Property Tree is not an XML library (see What XML parser should I use in C++?).

That said, it looks like your error is here:

for (auto const &subTree : mainTree.get_child("my_report")) {
    auto &nodeTestList = mainTree.get_child("my_report.test_list");

The second line doesn't use subTree at all, instead it just matches the first "my_report.test_list" node from mainTree.

Use Modern C++ And Compiler Warnings

I made the code self-contained c++11:

#include <boost/property_tree/xml_parser.hpp>
using boost::property_tree::ptree;

enum TestListParam { OVERALLSTATUS, TOTALRESULT };
std::array<std::string, 2> TestListAttrib{ "overall_status", "totalresult" };

int main() {
    ptree mainTree;
    {
        std::ifstream ifs("input.xml");
        read_xml(ifs, mainTree);
    }

    auto const testListCount = 3;

    for (auto const& subTree : mainTree.get_child("my_report")) {
        auto& nodeTestList = mainTree.get_child("my_report.test_list");
        for (auto& subval : nodeTestList) {
            ptree subvalTree = subval.second;
            for (auto& paramNode : subvalTree) {
                std::string name = paramNode.first;
                if (name == TestListAttrib[TestListParam::TOTALRESULT]) {
                    nodeTestList.put("<xmlattr>." + name, testListCount);
                }
            }
        }
    }
}

If you enable compiler warnings, you will see your error:

Live On Wandbox

prog.cc:16:22: warning: unused variable 'subTree' [-Wunused-variable]
    for (auto const& subTree : mainTree.get_child("my_report")) {
                     ^
1 warning generated.

More Modern C++

Using the niceties of C++17 things become cleaner and easier fixed. Here's a first shot, also adding output printing:

Live On Wandbox

#include <boost/property_tree/xml_parser.hpp>
#include <iostream>
using boost::property_tree::ptree;
auto const pretty = boost::property_tree::xml_writer_make_settings<std::string>(' ', 4);

enum TestListParam { OVERALLSTATUS, TOTALRESULT };
std::array<std::string, 2> TestListAttrib{ "overall_status", "totalresult" };

int main() {
    ptree mainTree;
    {
        std::ifstream ifs("input.xml");
        read_xml(ifs, mainTree);
    }

    auto const testListCount = 3;

    for (auto& [key, subTree] : mainTree.get_child("my_report"))
    for (auto& [name, node] : subTree.get_child("<xmlattr>")) {
        if (name == TestListAttrib[TestListParam::TOTALRESULT]) {
            node.put_value(testListCount);
        }
    }

    write_xml(std::cout, mainTree, pretty);
}

Prints: (whitespace reduced)

<?xml version="1.0" encoding="utf-8"?>
<my_report>
    <test_list overall_status="FAILED" result="1" totalresult="3"/>
    <test_list overall_status="FAILED" result="2" totalresult="3"/>
    <test_list overall_status="FAILED" result="3" totalresult="3"/>
</my_report>

Caveats

Note how because of the way we write the loops the code

  • will fail if <xmlattr> or my_report are not found
  • Conversely, it will erroneously descend all child nodes of my_report even if they have different names than test_list
  • the XSL processing instruction is lost. Once again, this is inherent because Boost Property Tree doesn't know about XML. It uses a subset of XML to implement serialization for property trees.

To fix the first two bullets, I'd suggest making a helper to query nodes from your XML (from Iterating on xml file with boost):

enumerate_nodes(mainTree,
        "my_report.test_list.<xmlattr>.totalresult", 
        back_inserter(nodes));

This doesn't suffer from any of the problems mentioned, and you can elegantly assing all matching nodes:

for (ptree& node : nodes)
    node.put_value(3);

If you really didn't /want/ to require the test_list node name, use a wildcard:

enumerate_nodes(mainTree,
        "my_report.*.<xmlattr>.totalresult", 
        back_inserter(nodes));

Live Demo

Live On Wandbox

#include <boost/property_tree/xml_parser.hpp>
#include <iostream>
using boost::property_tree::ptree;
auto const pretty = boost::property_tree::xml_writer_make_settings<std::string>(' ', 4);

enum TestListParam { OVERALLSTATUS, TOTALRESULT };
std::array<std::string, 2> TestListAttrib{ "overall_status", "totalresult" };

template <typename Ptree, typename Out>
Out enumerate_nodes(Ptree& pt, ptree::path_type path, Out out) {
    if (path.empty())
        return out;

    if (path.single()) {
        auto name = path.reduce();
        for (auto& child : pt) {
            if (child.first == name)
                *out++ = child.second;
        }
    } else {
        auto head = path.reduce();
        for (auto& child : pt) {
            if (head == "*" || child.first == head) {
                out = enumerate_nodes(child.second, path, out);
            }
        }
    }

    return out;
}


int main() {
    ptree mainTree;
    {
        std::ifstream ifs("input.xml");
        read_xml(ifs, mainTree);
    }

    std::vector<std::reference_wrapper<ptree> > nodes;
    enumerate_nodes(mainTree,
            "my_report.test_list.<xmlattr>.totalresult", 
            back_inserter(nodes));

    for (ptree& node : nodes)
        node.put_value(3);

    write_xml(std::cout, mainTree, pretty);
}

Prints

<?xml version="1.0" encoding="utf-8"?>
<my_report>
    <test_list overall_status="FAILED" result="1" totalresult="3"/>
    <test_list overall_status="FAILED" result="2" totalresult="3"/>
    <test_list overall_status="FAILED" result="3" totalresult="3"/>
</my_report>
sehe
  • 374,641
  • 47
  • 450
  • 633