【问题标题】:Getting the ptree from boost::property_tree::ptree::iterator从 boost::property_tree::ptree::iterator 获取 ptree
【发布时间】:2015-02-06 18:20:19
【问题描述】:

我有一段代码可以迭代提升属性树 (XML)。
我需要当前节点的 ptree,而不是节点的子节点。

更新

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)的结果

(我们得到整棵树,我们只想要节点)

<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) 的结果

(我们没有父节点..只有子节点)

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

想要的结果

(我们想要节点,它是孩子,.. 像这样)

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

【问题讨论】:

    标签: c++ xml boost tree boost-propertytree


    【解决方案1】:

    更新 2

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

    有两种方法。

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

          // write the single element: (undocumented API)
          boost::property_tree::xml_parser::write_xml_element(
                  std::cout, it->first, it->second,
                  0, settings
              );
      
    2. 或者您可以创建一个新的 ptree 对象与单个子对象

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

    输出:

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

    【讨论】:

    • 获取值不是我的问题..我需要当前迭代的 pTree.. node.second 提供子节点的 ptree.. 我需要这些子节点的父 ptree...我需要类似boost::property_tree::ptree MyPtree = node.second.get_parent(); 或类似的东西。谢谢
    • 那应该是你的问题 :) 更新了答案。
    • 抱歉含糊不清。我已经更新以充分展示我正在处理的内容。我非常感谢你的帮助sehe :)
    • 啊。所以你对信息没有问题gettting。您想要一种编写 XML 片段的方法。让我更新答案
    • @aquawicket 完成了。下一次,一定要把问题说清楚,你会更快得到更好的答案,不会浪费任何人的时间,甚至可能会获得点赞。
    猜你喜欢
    • 1970-01-01
    • 2011-12-30
    • 2011-12-30
    • 1970-01-01
    • 2013-07-14
    • 1970-01-01
    • 2015-03-19
    • 2013-05-20
    • 1970-01-01
    相关资源
    最近更新 更多