将具有相同键的节点添加到属性树中
Adding nodes with the same key to a property tree
我正在使用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>
对于一个简单的测试程序,我写道:
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;
}
基于这个问题,我看到put
方法替换了该节点上的任何东西,而不是添加一个新的。这正是我所看到的功能:
输出.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 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
方法,有人能解释一下如何使用吗?
有没有更好的方法来实现我想要的文件格式?
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;
}
当您第一次调用add_child
时,键"电子表格.单元格"的节点不存在,并且已创建。然后,它将树的内容(name
和contents
(添加到新创建的节点。当您第二次调用add_child
时,它会发现"电子表格.单元格"已经存在,但与put
不同,它会创建一个也称为"单元格"的同级节点,并将其插入同一位置。
最终输出:
<?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>
相关文章:
- 如何在Qt C++中向自定义控件添加属性?
- OpenGL - 添加第二个着色器属性会导致空白屏幕
- 递归添加子树以提升属性树
- 将具有 xml 属性的子树添加到提升属性树
- 我创建了deque<CObject*>并添加了不同类型的元素。如何更改此元素的属性?
- 如何向类添加属性
- 如何将按钮添加到节点的属性编辑器面板,该面板调用该节点内的方法?
- 如何在Boost Graph库中的Grid_graph的边缘添加自定义属性
- 如何使用属性包添加边缘以提升::adjacency_matrix
- 如何在虚幻引擎4中将属性添加到变换类别
- 为什么Clang自动将属性添加到我的功能中
- cmake 如何在项目的 vcxproj 属性组中添加自定义条目?
- Clang在发出LLVM IR时向所有函数添加noinline属性
- 将属性添加到QT C 中的Qpushbutton
- 向向下转换结构添加属性值会导致分段错误
- QTreeWidgterItem-隐藏值或添加属性
- 如何使用xerces向xml添加属性
- 如何向lambda函数添加属性
- 为什么c++ 17向名称空间和枚举数添加属性?
- 如何在c++中为已有的IXMLDOMNode添加属性