【问题标题】:Adding subtrees with xml attributes to a boost property tree将具有 xml 属性的子树添加到 boost 属性树
【发布时间】:2018-11-20 12:50:50
【问题描述】:

我正在使用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 属性放入 &lt;sometag&gt; 标记中。

而不是这个:

<sometag>
  ...

我想要这个:

<sometag someattribute="somevalue">
  ...

如何指定属性? boost 文档对此非常不清楚。

【问题讨论】:

  • 你的样本没有加起来。

标签: c++ boost ptree


【解决方案1】:

你应该使用&lt;xmlattr&gt;特殊的子节点命名空间:

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

【讨论】:

    猜你喜欢
    • 2019-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    • 1970-01-01
    相关资源
    最近更新 更多