Use boost: property_tree to generate xml with attribute, boostproperty_tree

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.