2014-01-08 29 views
2

我有一個以下格式的文件,我試圖使用boost :: property_tree :: read_ini和boost :: property_tree來解析。閱讀ini文件使用boost :: property_tree不與形式A.x的孩子工作

示例配置文件(有些值包含空格)

[Config] 
A = 1000 
B.x = Test 
B.y = Test By 
C.x.y = Test_Cxy 
C.x.z = Test Cxz 
[Config2] 
... 

示例代碼

boost::property_tree::ptree config; 
boost::property_tree::read_ini (name, config); 

// Correctly Iterates through and displays correct pairs 
for (ptree::const_iterator it = pt.begin(); it != end; ++it) { 
     std::cout << it->first << ": " << it->second.get_value<std::string>() << std::endl; 
     print(it->second); 
    } 

const boost::property_tree::ptree& configTree = config.get_child("Config"); 


// Correctly gets A 
std::string test_ = configTree.get<std::string>("A", "Default"); 

// Doesn't get B.x 
std::string test_ = configTree.get<std::string>("B.x", "Default"); 

我在做什麼錯?我如何正確得到B.x,B.y等? B.x被認爲是B的孩子嗎?因此,我需要get_child B?

回答

0

屬性樹是一個分層數據結構,其中每個節點可以有一個有序的子節點序列。在這個意義上,屬性樹比平坦的ini文件更類似於XML。雖然它可以從ini文件和XML初始化,但它代表內部數據完全相同,並且「x.y.z」查詢具有選擇x的y子元素的z子元素的特殊含義。

它歸結到string_path類有.默認的分隔符屬性樹的,

/// Default path class. A path is a sequence of values. Groups of values 
/// are separated by the separator value, which defaults to '.' cast to 
/// the sequence's value type. The group of values is then passed to the 
/// translator to get a key. 

總體而言,無論是避免在您的作品的名字點,當您使用的ini文件,或更改通過明確構建路徑的分隔符:

configTree.get<std::string>(ptree::path("B.x", '/')); // using non-default separator