Boost.xml_parser Chinese character problem (go)

Source: Internet
Author: User

When you use Xml_parser to read XML, parsing errors occur if you encounter Chinese characters.

There is a solution on the web that says to use Wptree, but it also goes wrong when you use Wptree to write XML. You can use Ptree to write Chinese without error.

To synthesize the above information, try to use Ptree to write XML, and use Wptree to read. Take a demo to illustrate it.

contains files 2 #include <boost/property_tree/ptree.hpp>3 #include <boost/property_tree/xml_ Parser.hpp>4 #include <boost/property_tree/json_parser.hpp>5 #include <boost/foreach.hpp >6 #include <string>7 #include <exception>8 #include <iostream>  

Define the structure body:

1struct Debug_simple2 {3 int Itsnumber; Span style= "color: #008080;" >4 std::string itsname; //use string here to 5 void Load (const std::string& filename); // 6 void Save (const std::string& filename); // 7};    

To save the function, use Ptree:

  1 void debug_simple::save (const std:: 2 { 3 using boost::p roperty_tree::p tree; 4 Ptree Pt; 5  6 Pt.put ( Debug.number 7 Pt.put (debug.name< Span style= "color: #800000;" > ", itsname);  9 Write_xml (filename,pt);                 

Load function using the Wptree, read the value is wstring, need to convert to string

1void Debug_simple::load (CONST STD::string& filename)2 {3Using boost::p roperty_tree::wptree; 7 Itsnumber = Wpt.int> (L Debug.number ");  8 std::wstring wstr = WPT. get<std::wstring> (L " debug.name ");  9 Itsname = Std::string (Wstr.begin (), Wstr.end ()); Span style= "color: #008000;" >// 10}  

Main function:

1int _tmain (int argc, _tchar* argv[])2 {34Try5 {6 Debug_simple Ds,read;7 Ds.itsname ="Kanji中文版";8 Ds.itsnumber =20;9Ten Ds.save ("Simple.xml");Read.load ("simple.xml "); 12 13 std::cout<<read.itsnumber<< Read.itsname; 14 15}16 catch (std::exception &e) 17 {18 std::cout <<  "error: " << E.what () <<  "\n" ; 19} 20 return 0;21}                 

Boost.xml_parser Chinese character problem (go)

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.