将具有相同密钥的节点添加到属性树

Dee*_*kor 7 c++ xml boost

我正在使用Boost的属性树来读写XML.我使用电子表格应用程序,我想将电子表格的内容保存到xml.这是一项学校作业,因此我需要对XML使用以下格式:

<?xml version="1.0" encoding="UTF-8"?>
<spreadsheet>
   <cell>
      <name>A2</name>
      <contents>adsf</contents>
   </cell>
   <cell>
      <name>D6</name>
      <contents>345</contents>
   </cell>
   <cell>
      <name>D2</name>
      <contents>=d6</contents>  
   </cell>
</spreadsheet>
Run Code Online (Sandbox Code Playgroud)

对于一个简单的测试程序,我写道:

int main(int argc, char const *argv[])
{
boost::property_tree::ptree pt;

pt.put("spreadsheet.cell.name", "a2");
pt.put("spreadsheet.cell.contents", "adsf");

write_xml("output.xml", pt);

boost::property_tree::ptree ptr;
read_xml("output.xml", ptr);

ptr.put("spreadsheet.cell.name", "d6");
ptr.put("spreadsheet.cell.contents", "345");
ptr.put("spreadsheet.cell.name", "d2");
ptr.put("spreadsheet.cell.contents", "=d6");

write_xml("output2.xml", ptr);

return 0;
}
Run Code Online (Sandbox Code Playgroud)

基于这个问题,我看到该put方法替换了该节点上的任何内容,而不是添加新节点.这正是我所看到的功能:

与Output.xml

<?xml version="1.0" encoding="utf-8"?>
<spreadsheet>
  <cell>
    <name>a2</name>
    <contents>adsf</contents>
  </cell>
</spreadsheet>
Run Code Online (Sandbox Code Playgroud)

Output2.xml

<?xml version="1.0" encoding="utf-8"?>
<spreadsheet>
  <cell>
    <name>d2</name>
    <contents>=d6</contents>
  </cell>
</spreadsheet>
Run Code Online (Sandbox Code Playgroud)

看看文档,我看到了这个add_child方法Add the node at the given path. Create any missing parents. If there already is a node at the path, add another one with the same key.

我无法弄清楚如何使用该add_child方法,有人可以解释如何使用它吗?

是否有更好的方法来实现我想要的文件格式?

Cap*_*ous 16

add_child成员函数允许你插入一个property_tree到另一个的DOM作为子节点.如果您提供的密钥路径已存在,则会添加重复密钥,而子代将插入其中.如果我们稍微改变你的例子,我们可以检查结果.

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>

int main()
{
    // Create the first tree with two elements, name and contents
    boost::property_tree::ptree ptr1;
    ptr1.put("name", "a2");
    ptr1.put("contents", "adsf");

    // Create the a second tree with two elements, name and contents
    boost::property_tree::ptree ptr2;
    ptr2.put("name", "d6");
    ptr2.put("contents", "345");

    // Add both trees to a third and place them in node "spreadsheet.cell"
    boost::property_tree::ptree ptr3;
    ptr3.add_child("spreadsheet.cell", ptr1);
    ptr3.add_child("spreadsheet.cell", ptr2);

    boost::property_tree::write_xml("output.xml", ptr3);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

add_child第一次调用时,键"spreadsheet.cell"的节点不存在并被创建.然后,它将树(namecontents)的内容添加到新创建的节点.当你add_child第二次调用时,它会看到"spreadsheet.cell"已经存在,但与put它不同的是,它会创建一个同样称为"cell"的兄弟节点并将其插入同一位置.

最终输出:

<?xml version="1.0" encoding="utf-8"?>
<spreadsheet>
  <cell>
    <name>a2</name>
    <contents>adsf</contents>
  </cell>
  <cell>
    <name>d6</name>
    <contents>345</contents>
  </cell>
</spreadsheet>
Run Code Online (Sandbox Code Playgroud)

  • @Deekor迭代`电子表格'的孩子们寻找所有类型`cell`的孩子.任何时候你遇到一个得到'name`的内容,如果你找到一个匹配删除它.您需要查看文档以了解具体信息 (2认同)