c++ - 将具有 xml 属性的子树添加到 boost 属性树

标签 c++ boost boost-propertytree

我正在使用 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++ - 将具有 xml 属性的子树添加到 boost 属性树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50794871/

相关文章:

c++ - 将数据读入循环缓冲区

代码编辑期间 Qt Creator 100% CPU

c++ - boost::property 树的任何标准库实现

xml - 遍历 boost 属性树

c++ - SuperLU 库 XCode 问题

c++ - 无法获得简单的 2D 着色器以在 C++ openGL 中绘制红色三角形(无编译器错误)

c++ - C++ fmt::print 与 fmt::format_to 命名的技术背景?

C++17 resumable/await:它也可以与 boost::future 一起使用吗?

c++ - 在同一个线程的同一个实例上多次调用 shared_future::get() 是否合法?