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);. 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 !!!!!
Use boost: property_tree to generate xml with attribute