Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read Unicode XML values with rapidxml

RapidXML is one of the available libraries for parsing XML in c++. For getting the values, we can use something like:

node->first_node("xmlnode")->value()

This command returns a char* data type. Is there any way to read the value as Unicode so I can assign it in a WCHAR or wstring variable?

like image 532
Ali Avatar asked May 12 '26 10:05

Ali


2 Answers

From the manual

RapidXml is character type agnostic, and can work both with narrow and wide characters. Current version does not fully support UTF-16 or UTF-32, so use of wide characters is somewhat incapacitated. However, it should succesfully parse wchar_t strings containing UTF-16 or UTF-32 if endianness of the data matches that of the machine.

so I just use the following:

#include <rapidxml/rapidxml.hpp>
typedef rapidxml::xml_node<wchar_t> const *      xml_node_cptr;
typedef rapidxml::xml_node<wchar_t> *            xml_node_ptr;
typedef rapidxml::xml_attribute<wchar_t> const * xml_attribute_cptr;
typedef rapidxml::xml_attribute<wchar_t> *       xml_attribute_ptr;
typedef rapidxml::xml_document<wchar_t>          xml_doc;

Note that if you do this, all parameters will be wchar_t, so the call to first_node() also needs to wchar_t. i.e.

node->first_node(L"xmlnode")->value()
like image 182
dteviot Avatar answered May 15 '26 00:05

dteviot


here you need convert str to wstr. you can use for this standart std

#include <string>
#include <codecvt>
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;

std::string strSample; // convert str to wstr
std::wstring wstrValue = converter.from_bytes(strSample);

std::wstring wstrSample; // convert wstr to str
std::string strValue = converter.to_bytes(wstrSample);

hope this help

like image 44
Alexander I. Denisevich Avatar answered May 14 '26 23:05

Alexander I. Denisevich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!