c++ - How to manually create a boost ptree with XML attributes? -


i've been using boost libraries parse xml files , have create ptree manually. need add xml attribute ptree. boost documentation suggests:

ptree pt; pt.push_back(ptree::value_type("pi", ptree("3.14159"))); 

that adds element content, need add attribute element.

the code above produces:

<pi>3.14</pi> 

i need add this:

<pi id="pi_0">3.14</pi>  

what need change, add attribute id="pi_0"?

you use "fake" node <xmlattr>: http://www.boost.org/doc/libs/1_46_1/doc/html/boost_propertytree/parsers.html#boost_propertytree.parsers.xml_parser

live on coliru

#include <boost/property_tree/ptree.hpp> #include <boost/property_tree/xml_parser.hpp> #include <iostream>  using boost::property_tree::ptree;  int main() {      ptree pt;     pt.push_back(ptree::value_type("pi", ptree("3.14159")));     pt.put("pi.<xmlattr>.id", "pi_0");      write_xml(std::cout, pt); } 

prints

<?xml version="1.0" encoding="utf-8"?> <pi id="pi_0">3.14159</pi> 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -