从boost :: property_tree :: ptree :: iterator获取ptree [英] Getting the ptree from boost::property_tree::ptree::iterator

查看:751
本文介绍了从boost :: property_tree :: ptree :: iterator获取ptree的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码在boost属性树(XML)上进行迭代。

我需要当前节点的ptree,而不是该节点的子节点。

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.

更新

xml树

<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>

错误代码

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
    }
}

结果使用write_xml( test.html,pt)

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>

使用write_xml( test.html,it-> second)的结果

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>


推荐答案

更新2

根据评论/更新的问题进行重写。

Rewritten in response to the comment/updated question.

有两种方法。


  1. 您可以使用未记录的函数 write_xml_element 编写单个元素(使用键作为元素名称) ):

  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
        );


  • 或者您可以创建新的 ptree 带有单个孩子的对象

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


  • 在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");
    }
    

    输出:

    <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>
    

    这篇关于从boost :: property_tree :: ptree :: iterator获取ptree的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆