2015-02-06 165 views
1

我有一段迭代遍歷boost屬性樹(XML)的代碼。
我需要當前節點的ptree,而不是節點的子節點。從boost獲取ptree :: property_tree :: ptree :: iterator

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> 

itteration代碼使用write_xml( 「的test.html」,PT)

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

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

結果( 「test.html的」,它 - >第二)

(我們沒有父節點..唯一的孩子節點)

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

期望的結果

(我們想要的節點,它的孩子,......像這樣)

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

回答

2

更新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> 
+0

獲取價值是不是我的問題。我所需要的電流itteration的ptree中.. node.second提供子節點的ptree中..我需要這些子節點的父ptree ...我需要像'boost :: property_tree :: ptree MyPtree = node.second.get_parent();'或類似的東西。謝謝 – aquawicket 2015-02-06 19:34:02

+0

這應該是你的問題然後:)更新答案。 – sehe 2015-02-06 20:44:45

+0

抱歉太模糊了..我已經更新完全展示了我正在處理的事情。我非常感謝你的幫助sehe :) – aquawicket 2015-02-06 22:03:45

相關問題