使用boost::property_tree產生帶attribute的xml,boostproperty_tree

來源:互聯網
上載者:User

使用boost::property_tree產生帶attribute的xml,boostproperty_tree
以前寫過一篇"使用Boost property tree來解析帶attribute的xml", 可是還有姐妹篇一直沒貼。看看前一篇貼了都快都快3年了,時間過的真快。
這一小篇就算是下篇吧,即用boost::property_tree產生帶attribute的xml。

直接看demo code:

#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;}

產生的xml如下:

<?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>

!!!!!!!歡迎轉載,請註明本文連結:http://blog.csdn.net/mosaic/article/details/39047327 !!!!!


boost property_tree怎解析多層xml 比如<a><b><c>11</c></b> <b><c>22</c></b>

std::vector
 
boost庫中讀取xml的函數 read_xml的第一個參數不支援中文路徑?

boost內部實際上用的是STL的串流

有時候用ifstream或ofstream開啟帶有中文路徑的檔案會失敗。

解決辦法:
1、使用C語言的函數設定為中文運行環境
setlocale(LC_ALL,"Chinese-simplified");

2、使用STL函數設定為系統語言環境
std::locale::global(std::locale(""));

當然選2啦!
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.