添加节点具有相同键到属性树

添加节点具有相同键到属性树

本文介绍了添加节点具有相同键到属性树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Boost的属性树读写XML。用一根S preadsheet应用我做了我想给S preadsheet的内容保存为XML。这是一个作业,所以我需要使用以下格式为XML:

I am using Boost's property tree to read and write XML. Using a spreadsheet application I made I want to save the contents of the spreadsheet to xml. This is a school assignment so I am required to use the following format for the 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>

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

For a simple test program I wrote:

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

在此基础上question我看方法在该节点替换而不是增加一个新的东西。这也正是我所看到的功能:

Based on this question I see the put method replaces anything at that node, instead of adding a new one. Which is exactly the functionality I am seeing:

的Output.xml

<?xml version="1.0" encoding="utf-8"?>
<spreadsheet>
  <cell>
    <name>a2</name>
    <contents>adsf</contents>
  </cell>
</spreadsheet>

Output2.xml

<?xml version="1.0" encoding="utf-8"?>
<spreadsheet>
  <cell>
    <name>d2</name>
    <contents>=d6</contents>
  </cell>
</spreadsheet>

纵观文档我看到这个 add_child 方法,将添加节点在给定的路径。创建任何缺少父母。如果已经有在路径节点,添加具有相同的键一个又一个。

我不能完全弄清楚如何使用 add_child 方法,可能有人解释如何使用它?

I can't quite figure out how to use that add_child method, could someone explain how to use it?

有没有要去这个更好的方式来实现文件格式我想要什么?

Is there a better way of going about this to achieve the file format I want?

推荐答案

add_child 成员函数允许你插入一个 property_tree 到的另一DOM中作为子节点。如果您已经提供的关键路径中存在重复的键将被添加和孩子会代替插入那里。如果我们改变你的例子一点点,我们可以检查结果。

The add_child member function allows you to insert one property_tree into the DOM of another as a child node. If the key path you provide already exists a duplicate key will be added and the child will be inserted there instead. If we change your example a little bit we can examine the results.

#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;
}

当你调用 add_child 在第一时间,对关键的S preadsheet.cell节点不存在,被创建。然后,将树(名称内容)的内容到新创建的节点。当你调用 add_child 第二次它看到S preadsheet.cell已经存在,但不像它创建一个兄弟节点也被称为小区,并在同一位置将其插入

When you call add_child the first time, the node for key "spreadsheet.cell" does not exist and is created. It then adds the contents of the tree (name and contents) to the newly created node. When you call add_child the second time it sees that "spreadsheet.cell" already exists but unlike put it creates a sibling node also called "cell" and inserts it at the same location.

最后的输出:

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

这篇关于添加节点具有相同键到属性树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 12:09