为boost::property_tree元素添加子树
Adding subtree to boost::property_tree element
我想要的是:
<tree>
<objects>
<object id="12345678">
<AdditionalInfo>
<Owner>Mr. Heik</Owner>
<Health>37/100</Health>
</AdditionalInfo>
</object>
</objects>
</tree>
我得到的是:
<tree>
<objects>
<object id="12345678"/>
<AdditionalInfo>
<Owner>Mr. Heik</Owner>
<Health>37/100</Health>
</AdditionalInfo>
</objects>
</tree>
我尝试的是:
using boost::property_tree::ptree;
ptree pt;
boost::property_tree::ptree nodeObject;
nodeObject.put("object.<xmlattr>.id", 12345678);
boost::property_tree::ptree nodeInfo;
nodeInfo.put("Owner", "Mr. Heik");
nodeInfo.put("Health", "37/100");
// Put everything together
nodeObject.put_child("AdditionalInfo", nodeInfo);
pt.add_child("tree.objects", nodeObject);
write_xml("output.xml", pt);
我试图通过使用put/add/add_child/等来获得所需的结果。但是没有成功。我必须使用哪些增强功能?
这一行:
nodeObject.put("object.<xmlattr>.id", 12345678);
用给定的属性向当前节点的子路径"object"添加一个新的子路径。
在Node上设置你的属性:
nodeObject.put("<xmlattr>.id", 12345678);
并将节点直接添加到树的正确路径中:
pt.add_child("tree.objects.object", nodeObject);
最终代码:
ptree pt;
boost::property_tree::ptree nodeObject;
nodeObject.put("<xmlattr>.id", 12345678);
boost::property_tree::ptree nodeInfo;
nodeInfo.put("Owner", "Mr. Heik");
nodeInfo.put("Health", "37/100");
nodeObject.put_child("AdditionalInfo", nodeInfo);
pt.add_child("tree.objects.object", nodeObject);
write_xml("output.xml", pt);
输出:<?xml version="1.0" encoding="utf-8"?>
<tree>
<objects>
<object id="12345678">
<AdditionalInfo>
<Owner>Mr. Heik</Owner>
<Health>37/100</Health>
</AdditionalInfo>
</object>
</objects>
</tree>
相关文章:
- 有没有办法向这个向量添加元素?
- 如何在提升间进程中构建具有给定计数的向量并向其添加元素
- 如何在 Arduino 字符串的开头添加元素.类似于 JS unshift();
- C++向数组添加元素并调整数组大小
- 我无法向 c++ 添加元素
- BST 不添加元素
- 调整数组大小并添加元素 C++
- 向向量添加元素
- 如何在不使用标准算法的情况下在排序向量中添加 c 元素?
- 双链接列表添加元素不起作用,不知何故它总是保持为空
- 当我向 vector<int *> 添加元素时,我遇到了一些问题
- 动态地添加元素
- 静态2D数组,添加元素
- 在一个类中向向量添加元素不适用于其他类
- 在数组中添加元素将不接受类似的元素
- STD :: MAP-使用下标操作员与插入方法添加元素
- 动态数组添加元素而不创建结构变量/对象
- 以下向 c++ 向量添加元素的方法有什么区别
- 如何在 std::map 中添加元素自己进行分配
- C++在向量中添加元素的双精度