本博文主要想說明如下兩點:html
1.對於上一篇的《boost::xml——基本操做以及中文亂碼解決方案》解釋,這篇博文基本解決了正確輸入輸出中英文問題,可是好像尚未解決修改中文出現亂碼的問題。app
能夠參看這段代碼post
1 bool read_xmlW(const std::string &xml,boost::property_tree::wptree &a_wptree) 2 { 3 bool rt = true; 4 boost::mutex::scoped_lock s_lock(m_mutex); 5 std::wstring wstr; 6 wsstream.clear(); 7 8 wstr = g_codetrans()->utf8_to_utf16(xml); 9 wsstream << wstr; 10 11 // set facet for std::wstring 12 std::locale current_locale(std::locale(""), new boost::program_options::detail::utf8_codecvt_facet()); 13 14 try 15 { 16 //boost::property_tree::read_xml<boost::property_tree::wptree>(wsstream, a_wptree, boost::property_tree::xml_parser::trim_whitespace, current_locale); 17 boost::property_tree::read_xml<boost::property_tree::wptree>(wsstream, a_wptree, boost::property_tree::xml_parser::trim_whitespace); 18 } 19 catch (const std::exception &e) 20 { 21 #ifdef _DEBUG 22 std::cout << "Error:" << typeid(e).name() << ": "; 23 std::cout << e.what() << std::endl; 24 #endif // DEBUG 25 rt = false; 26 } 27 return rt; 28 29 }
注:裏面寬字符的a_wptree沒有問題,主要是wstr = g_codetrans()->utf8_to_utf16(xml);這個問題。url
此段代碼僅供參考。spa
2.對於xml寫的一點小技巧,這個問題難了我很久,寫在下面以供分享。code
先看輸出的xml文件xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <root value="root_test"> 3 <ceng1 value="ceng1_test"> 4 <ceng2 value="0_ceng2"/> 5 <ceng2 value="1_ceng2"/> 6 <ceng2 value="2_ceng2"/> 7 </ceng1> 8 <ceng1 value="ceng1_test"> 9 <ceng2 value="0_ceng2"/> 10 <ceng2 value="1_ceng2"/> 11 <ceng2 value="2_ceng2"/> 12 </ceng1> 13 <other1 value="other1_test"> 14 <other2 value="0_other2"/> 15 <other2 value="1_other2"/> 16 <other2 value="2_other2"/> 17 </other1> 18 </root>
你們不要小看這幾個小小層次,難了我很久,讀取容易,寫出如出一轍的難。難點在於對於節點層次的把握。2個ceng1節點和1個other1節點並列,他們的定節點都是root,他們還有子節點。htm
下面附上片斷源碼:blog
1 void TestPtree(std::string &o_xml) 2 { 3 tptree pt;//對應第一層的節點(根節點) 4 tptree cccpt;//對應第二層的節點 5 for (int j=0 ; j<2 ; j++) 6 { 7 tptree cpt;//對應第三層的節點 8 for (int i=0 ; i<3 ; i++) 9 { 10 std::stringstream ss; 11 ss << i; 12 string str = ss.str(); 13 str.append("_ceng2"); 14 tptree ccpt;//對應第三層的屬性值 15 ccpt.add<std::string>("<xmlattr>.value", str); 16 cpt.add_child("ceng2", ccpt); 17 } 18 cpt.put<std::string>("<xmlattr>.value", "ceng1_test");//增長第一層的屬性 19 cccpt.add_child("ceng1", cpt); 20 } 21 for (int j=0 ; j<1 ; j++) 22 { 23 tptree cpt; 24 for (int i=0 ; i<3 ; i++) 25 { 26 std::stringstream ss; 27 ss << i; 28 string str = ss.str(); 29 str.append("_other2"); 30 tptree ccpt; 31 ccpt.add<std::string>("<xmlattr>.value", str); 32 cpt.add_child("other2", ccpt); 33 } 34 cpt.put<std::string>("<xmlattr>.value", "other1_test"); 35 cccpt.add_child("other1", cpt); 36 } 37 cccpt.put<std::string>("<xmlattr>.value", "root_test");//增長根節點的屬性 38 pt.add_child("root", cccpt); 39 40 #ifdef CHINESE_CHARSET 41 std::locale current_locale(locale(""), new boost::program_options::detail::utf8_codecvt_facet()); 42 boost::property_tree::xml_parser::xml_writer_settings<wchar_t> settings(L'\t', 1, L"utf-8"); 43 #else 44 std::locale current_locale; 45 boost::property_tree::xml_parser::xml_writer_settings<char> settings('\t', 1, "utf-8"); 46 #endif 47 48 try 49 { 50 boost::property_tree::write_xml(o_xml, pt, current_locale, settings); 51 } 52 catch (const std::exception &e) 53 { 54 cout << "Error:" << typeid(e).name() << ": "; 55 cout << e.what() << endl; 56 } 57 }
注:這是主要的代碼片斷,若是想運行編譯經過能夠參看個人另外一篇博文《boost::xml——基本操做以及中文亂碼解決方案》完整源碼,地址是:點擊這裏utf-8