Question about boost. xml_parser Chinese Characters

Source: Internet
Author: User

When xml_parser is used to read XML, parsing errors may occur if Chinese characters are encountered.

There are solutions on the Internet that use wptree for implementation. However, when wptree is used to write XML, an error occurs. When ptree is used to write Chinese characters, no error occurs.

 

Based on the above information, try to use ptree to write XML, and use wptree to read. Use a demo to describe it.

1 // contains the file
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 struct:

1 struct debug_simple
2 {
3 int itsnumber;
4 STD: String itsname; // you can use string here.
5 void load (const STD: string & filename); // load the Function
6 void save (const STD: string & filename); // Save the Function
7 };


Save the function, using ptree:

 1 void debug_simple::save( const std::string& filename )
2 {
3 using boost::property_tree::ptree;
4 ptree pt;
5
6 pt.put("debug.number",itsNumber);
7 pt.put("debug.name",itsName);
8
9 write_xml(filename,pt);
10 }

 

The wptree used by the load function. The read value is wstring and must be converted to string.

1 void debug_simple: load (const STD: string & filename)
2 {
3 using boost: property_tree: wptree;
4 wptree WPT;
5 read_xml (filename, WPT );
6
7 itsnumber = WPT. Get <int> (L "Debug. Number ");
8 STD: wstring wstr = WPT. Get <STD: wstring> (L "Debug. Name ");
9 itsname = STD: string (wstr. Begin (), wstr. End (); // convert wstring to string
10}


Main function:

1 int _ tmain (INT argc, _ tchar * argv [])
2 {
3
4 try
5 {
6 debug_simple ds, read;
7 Ds. itsname = "Chinese English ";
8 dS. itsnumber = 20;
9
10 Ds. Save ("Simple. xml ");
11 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}

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.