Use boost: property_tree to generate xml with attribute, boostproperty_tree
I have previously written an article "using Boost property tree to parse xml with attribute", but my sister-in-law has never posted this article. It's almost three years since I posted the previous article. It's so fast.
This is the next article. Use boost: property_tree to generate xml with attribute.
View the demo code directly:
#include <iostream>#include <sstream>#include <boost/property_tree/xml_parser.hpp>using namespace std;int main(){using boost::property_tree::ptree; ptree pt; ptree tab1; ptree tab2; tab1.put("attr1", "value1"); tab1.put("attr1.<xmlattr>.code", "ABC"); tab1.put("attr2", "value2"); tab2.put("attr3", "value3"); tab2.put("attr3.<xmlattr>.code", "XYZ"); ptree array; auto& a = array.add_child("table1", tab1); a.put("<xmlattr>.tabid", "1"); auto& b = array.add_child("table2", tab2); b.put("<xmlattr>.tabid", "2"); pt.put_child("demoxml", array); ostringstream oss; write_xml(oss, pt, xml_writer_settings<char>(' ', 2)); cout << oss.str() << endl; return 0;}
The generated xml is as follows:
<?xml version="1.0" encoding="utf-8"?><demoxml> <table1 tabid="1"> <attr1 code="ABC">value1</attr1> <attr2>value2</attr2> </table1> <table2 tabid="2"> <attr3 code="XYZ">value3</attr3> </table2></demoxml>
!!!!!!! Welcome to reprint, please indicate the link of this article: http://blog.csdn.net/mosaic/article/details/39047327 !!!!!
How to parse multi-layer xml using boost property_tree, such as <a> <B> <c> 11 </c> </B> <c> 22 </c> </B>
Std: vector
The first parameter of the read_xml function reading xml in the boost library does not support the Chinese path?
Actually, boost uses STL streaming internally.
Sometimes, opening a file with a Chinese path using ifstream or ofstream fails.
Solution:
1. Use C language functions to set the Chinese Runtime Environment
Setlocale (LC_ALL, "Chinese-simplified ");
2. Use STL functions to set the environment for the System Language
Std: locale: global (std: locale (""));
Of course, select 2!