2015-09-26 48 views
1

我需要幫助將子ptree節點添加到已有 包含值的現有ptree節點。我現在面臨的問題是,我結束了一個 current_module嵌套我試圖做到這一點是如下現有current_moduleboost ptree add_child創建不需要的嵌套元素

方式中:

std::vector<moduleStatus> moduleStatii = { 
    {"mod1", "file1.TXT", 0x0002}, 
    {"mod2", "file2.TXT", 0x0003} 
}; 

ptree pt; 
pt.add("status.session_id", sessionID); 
pt.put("status.start_time", timeStringUTC); 
pt.put("status.load_file", loadFile); 
pt.put("status.upload.estimated_loadtime", 1983); 
pt.put("status.upload.time_remaining", 1613); // not finished - non-zero 
// for each over the modules... 
for (const auto& next : moduleStatii) { 
    ptree moduleStatus; 
    moduleStatus.put("current_module", next.moduleName); 
    moduleStatus.put("current_module.current_file", next.currentFile); 
    moduleStatus.put("current_module.status_code", next.statusCode); 
    pt.add_child("status.upload.current_module", moduleStatus); 
} 
// this is overall status - operation in progress 0x0002 
pt.put("status.status_code", ss.str()); 
write_xml(std::cout, pt, settings); 

不幸的是,輸出如下:

<?xml version="1.0" encoding="utf-8"?> 
<status> 
    <session_id>123</session_id> 
    <start_time>Sat Sep 26 20:12:46 2015</start_time> 
    <load_file>/tmp/filename.zip</load_file> 
    <upload> 
    <estimated_loadtime>1983</estimated_loadtime> 
    <time_remaining>0</time_remaining> 
    <current_module> 
     mod1 
     <current_file>file1.TXT</current_file> 
     <status_code>3</status_code> 
    </current_module> 
    <current_module> 
     mod2 
     <current_file>file2.TXT</current_file> 
     <status_code>3</status_code> 
    </current_module> 
    </upload> 
    <status_code>0x0002</status_code> 
</status> 

我活demo顯示我的電流輸出如下:

<?xml version="1.0" encoding="utf-8"?> 
<status> 
    <session_id>123</session_id> 
    <start_time>Sat Sep 26 20:23:38 2015</start_time> 
    <load_file>/tmp/filename.zip</load_file> 
    <upload> 
    <estimated_loadtime>1983</estimated_loadtime> 
    <time_remaining>1613</time_remaining> 
    <current_module> 
     <current_module> 
     mod1 
     <current_file>file1.TXT</current_file> 
     <status_code>2</status_code> 
     </current_module> 
    </current_module> 
    <current_module> 
     <current_module> 
     mod2 
     <current_file>file2.TXT</current_file> 
     <status_code>3</status_code> 
     </current_module> 
    </current_module> 
    </upload> 
    <status_code>0x0002</status_code> 
</status> 

不幸的是它包含了一個額外的嵌套層次,我似乎無法避免使用 。關鍵要注意的是,存在與 </current_module>相關的值 - 在示例mod1mod2中。

回答

1

只是不指定額外的父路徑節點:

ptree moduleStatus; 
    moduleStatus.put_value(next.moduleName); 
    moduleStatus.put("current_file", next.currentFile); 
    moduleStatus.put("status_code", next.statusCode); 
    pt.add_child("status.upload.current_module", moduleStatus); 

看到它Live On Coliru

打印

<?xml version="1.0" encoding="utf-8"?> 
<status> 
    <session_id>123</session_id> 
    <start_time>Sat Sep 26 21:29:06 2015</start_time> 
    <load_file>/tmp/filename.zip</load_file> 
    <upload> 
    <estimated_loadtime>1983</estimated_loadtime> 
    <time_remaining>1613</time_remaining> 
    <current_module> 
     mod1 
     <current_file>file1.TXT</current_file> 
     <status_code>2</status_code> 
    </current_module> 
    <current_module> 
     mod2 
     <current_file>file2.TXT</current_file> 
     <status_code>3</status_code> 
    </current_module> 
    </upload> 
    <status_code>0x0002</status_code> 
</status>