Search code examples
c++xmlboosttreeboost-propertytree

Getting the ptree from boost::property_tree::ptree::iterator


I have a piece of code that iterates over a boost property tree (XML).
I need a ptree of the current node, not the children of the node.

UPDATE

xml tree

<node id="A.html">
    <subnode> child A1 </subnode>
    <subnode> child A2 </subnode>
</node>

<node id="B.html">
    <subnode> child B1 </subnode>
    <subnode> child B2 </subnode>
</node>

itteration code

void parse_tree(ptree& pt, std::string key)
{
    string nkey;
    if (!key.empty())
    nkey = key + ".";

    ptree::const_iterator end = pt.end();
    for(ptree::iterator it = pt.begin(); it != end; ++it){

        //if the node's id is a .html filname, save the node to file
        string id = it->second.get("<xmlattr>.id","");

        if(id.find("B.html") != std::string::npos){  //Let's just test for "B.html"
            write_xml("test.html", pt);           //saves entire tree
            write_xml("test.html", it->second);   //saves only children of the node
        }

        parse_tree(it->second, nkey + it->first); //recursion
    }
}

Results using write_xml("test.html", pt)

(We get the entire tree, we only want the node)

<node id="A.html">
    <subnode> child A1 </subnode>
    <subnode> child A2 </subnode>
</node>
<node id="B.html">
    <subnode> child B1 </subnode>
    <subnode> child B2 </subnode>
</node>

Results using write_xml("test.html", it->second)

(We have no parent node.. only child nodes)

<subnode> child B1 </subnode>
<subnode> child B2 </subnode>

Desired result

(We want the node, and it's children,.. like so)

<node id="B.html">
    <subnode> child B1 </subnode>
    <subnode> child B2 </subnode>
</node>

Solution

  • UPDATE 2

    Rewritten in response to the comment/updated question.

    There are two ways.

    1. You can use the undocumented function write_xml_element to write the single element (using the key as element name):

          // write the single element: (undocumented API)
          boost::property_tree::xml_parser::write_xml_element(
                  std::cout, it->first, it->second,
                  0, settings
              );
      
    2. or you can create a new ptree object with the single child

          ptree tmp;
          tmp.add_child(it->first, it->second);
          write_xml(std::cout, tmp, settings);
      

    Live On Coliru

    #include <boost/property_tree/ptree.hpp>
    #include <boost/property_tree/xml_parser.hpp>
    
    #include <fstream>
    #include <iostream>
    
    using namespace boost::property_tree;
    
    
    void parse_tree(ptree& pt, std::string key)
    {
        std::string nkey;
        auto settings = xml_parser::xml_writer_make_settings<std::string>('\t', 1);
    
        if (!key.empty()) {
            nkey = key + ".";
        }
    
        ptree::const_iterator end = pt.end();
        for(ptree::iterator it = pt.begin(); it != end; ++it)
        {
            //if the node's id an .html filname, save the node to file
            std::string id = it->second.get("<xmlattr>.id","");
    
            if (id.find(key) != std::string::npos) {
                // write the single element: (undocumented API)
                boost::property_tree::xml_parser::write_xml_element(
                        std::cout, it->first, it->second,
                        0, settings
                    );
    
                // or: create a new pt with the single child
                std::cout << "\n==========================\n\n";
                ptree tmp;
                tmp.add_child(it->first, it->second);
                write_xml(std::cout, tmp, settings);
            }
    
            parse_tree(it->second, nkey + it->first); //recursion
        }
    }
    
    int main() {
        ptree pt;
        read_xml("input.txt", pt);
    
        parse_tree(pt, "B");
    }
    

    Output:

    <node id="B.html">
        <subnode> child B1 </subnode>
        <subnode> child B2 </subnode>
    </node>
    
    ==========================
    
    <?xml version="1.0" encoding="utf-8"?>
    <node id="B.html">    
        <subnode> child B1 </subnode>
        <subnode> child B2 </subnode>
    </node>