Use rapidxml to write XML files in Unicode Mode

Source: Internet
Author: User
Tags traits

Rapidxml Introduction: omitted

After you download rapidxml, you may want to use it in Unicode mode, but the compilation will fail and an error will be prompted. The error prompt is as follows:
Error c2440: '<function-style-cast>': cannot convert from 'std: basic_ostream <_ ELEM, _ traits> 'to 'std: ostream_iterator <_ ty>'
Since a prompt is given, it means that the given parameter type and the type required by the function are incorrect, we will solve it.

BelowCodeAll rows in the rapidxml_print.hpp file:

First, let's take a look at the print parameter template:
Template <class _ ty, class _ ELEM = char, class _ traits = char_traits <_ ELEM>
Class ostream_iterator

Then let's take a look at the print function template:
Template <class ch>
Inline STD: basic_ostream <ch> & print (STD: basic_ostream <ch> & out, const xml_node <ch> & node, int flags = 0)
For the CH type, it is to specify the parsed Character Set of the function. here we can use chat, wchar_t, and of course the automated tchar.

However, let's take a look at where the function is converting the parameter template:
Print (STD: ostream_iterator <ch> (out), node, flags );
In the conversion iterator, although the character set is specified, the function conversion set is not specified, because we need to change it:
Print (STD: ostream_iterator <ch, ch> (out), node, flags );
Because the second template parameter of ostream_iterator is Char by default, when we need to use wchar_t resolution, the parameter type here is definitely incorrect.
I don't know whether the bug was written by the author or not. At this point, you can write the XML file with rapidxml in Unicode mode.

The method for writing files is as follows:

# Include <iostream> <br/> # include <tchar. h> <br/> # include "rapidxml. HPP "<br/> # include" rapidxml_print.hpp "<br/> # include" rapidxml_utils.hpp "</P> <p> void main () <br/>{< br/> rapidxml: xml_document <tchar> Doc; <br/> rapidxml: xml_node <tchar> * node; <br/> node = Doc. allocate_node (rapidxml: node_element, _ T ("developer information"), null); <br/> Doc. append_node (node); <br/> node-> append_attribute (Doc. allocate_attribute (_ T (""), _ T ("http://blog.csdn.net/showlong"); <br/> node-> append_attribute (Doc. allocate_attribute (_ T ("author"), _ T ("showlong"); <br/> node-> append_attribute (Doc. allocate_attribute (_ T ("posting date"), _ T ("2010.12.06"); </P> <p> # ifdef Unicode <br/> STD :: wofstream xml_file (_ T ("config. XML "); <br/> xml_file.imbue (STD: locale (" CHS "); <br/> # else <br/> STD :: ofstream xml_file (_ T ("config. XML "); <br/> # endif <br/> // Method 1: <br/>{< br/> rapidxml: Print (STD: basic_ostream <tchar> &) xml_file, doc, rapidxml: print_no_indenting); <br/>}< br/> // Method 2: directly write a file <br/>{< br/> xml_file <Doc; <br/>}< br/> // method 3: write directly to the memory block <br/>{< br/> tchar dst_data [4096] = {0 }; <br/> tchar * dst_end = rapidxml: Print (dst_data, Doc, 0); <br/> size_t file_size = dst_end-dst_data; <br/>}</P> <p> system ("pause"); <br/>}

!!! Please note !!!:
If the code above is compiled in UNICODE, modify the code as follows:
row 403 in the rapidxml_print.hpp file:
Print (STD :: ostream_iterator (out), node, flags);
changed to:
Print (STD: ostream_iterator (out), node, flags);

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.