boost ptree add_child创建了不需要的嵌套元素

时间:2015-09-26 20:30:21

标签: c++ xml boost

我需要帮助将子ptree节点添加到已经存在的现有ptree节点 包含一个值。我面临的问题是我最终得到了一个 current_module嵌套在现有current_module

我试图实现这一目标的方式如下:

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 个答案:

答案 0 :(得分: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>