本博文主要想说明如下两点: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