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

纵饮孤独 提交于 2019-12-24 13:21:15

问题


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>

回答1:


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>


来源:https://stackoverflow.com/questions/28372268/getting-the-ptree-from-boostproperty-treeptreeiterator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!