簡體   English   中英

將具有xml屬性的子樹添加到boost屬性樹

[英]Adding subtrees with xml attributes to a boost property tree

我正在使用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>

到目前為止, <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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM