簡體   English   中英

從boost :: property_tree :: ptree :: iterator獲取ptree

[英]Getting the ptree from boost::property_tree::ptree::iterator

我有一段代碼在boost屬性樹(XML)上進行迭代。
我需要當前節點的ptree,而不是該節點的子節點。

UPDATE

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>

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

生活在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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM