c++ - 将具有 xml 属性的子树添加到 boost 属性树
问题描述
我boost::ptree
用于创建 xml 文件
ptree tree;
ptree & subtree = tree.add("sometag", "");
ptree & subsubtree = tree.add("someothertag", "");
...
write_xml(stfilename, declarationTree, std::locale(),
xml_writer_settings<std::string>(' ', 4));
这将创建以下 XML 文件
<sometag>
<someothertag>
...
</someothertag>
</sometag>
到目前为止一切顺利,但我需要将 xml 属性放入<sometag>
标签中。
而不是这个:
<sometag>
...
我要这个:
<sometag someattribute="somevalue">
...
如何指定属性?boost 文档对此非常不清楚。
解决方案
您应该使用<xmlattr>
特殊的子节点命名空间:
#include <boost/property_tree/xml_parser.hpp>
#include <iostream>
int main() {
boost::property_tree::ptree tree;
tree.put("sometag.someothertag.<xmlattr>.someattribute", "somevalue");
write_xml(std::cout, tree,
boost::property_tree::xml_writer_settings<std::string>(' ', 4));
}
印刷
<?xml version="1.0" encoding="utf-8"?>
<sometag>
<someothertag someattribute="somevalue"/>
</sometag>
推荐阅读
- c++ - VSCode 和 SDL 设置
- javascript - 试图理解为什么增强的 for 循环不适用于向对象添加键值对
- javascript - 使用ajax jquery提交表单时readyState = 0错误
- regex - 如何从我的正则表达式提取中排除特定匹配?
- typescript - 如何正确检查快速路由处理程序响应
- typescript - 打字稿和使用新地图在输入第二个参数作为组件时遇到问题
- c# - 如何动态访问 EF Core 中实体的属性?
- botframework - LUIS 没有响应 Web 聊天中的测试和聊天机器人在模拟器和直线上正常工作
- java - 流式传输两个列表并返回描述
- reactjs - 未处理的拒绝(TypeError):BlocContract.methods.read_task 不是函数