Menu

Unicode (std::wstring) with TinyXML

Erdem
2010-04-27
2025-03-05
  • Erdem

    Erdem - 2010-04-27

    Hi,

    I'm converting my to support other languages so I am moving my strings from std::string to std::wstring.

    Previously I was able to do the following:

                    entry3.InsertEndChild( TiXmlText( (*i)->getDescription( lang ) ) );

    getDescription() used to return a std::string and it worked nicely.

    Now it is returning a std::wstring and it doesn't compile anymore:

    ..\..\ext\TinyXML\tinyxml.h(488) : error C2440: 'return' : cannot convert from 'const wchar_t *' to 'const char *'

    Obviously this is expected. But it says on the TinyXML page that UTF-8 is supported, how can I use std::wstring with Tiny XML ?

    Thanks,

    MDE

     
  • James Lawrence

    James Lawrence - 2025-03-05

    TinyXML (both TinyXML and TinyXML-2) primarily deals with narrow character strings (std::string). However, if you want to work with Unicode strings (std::wstring), you need to convert between std::wstring and std::string (typically using UTF-8 encoding).

    Steps to handle Unicode (std::wstring) with TinyXML:
    Convert std::wstring to std::string (UTF-8) before inserting into TinyXML.
    Convert std::string (UTF-8) back to std::wstring when reading from TinyXML.
    Implementation:
    Dependencies:
    Use <codecvt> (deprecated in C++17, removed in C++20) or std::wstring_convert
    Use TinyXML (or TinyXML-2)
    Example with TinyXML (Legacy):
    cpp
    Copy
    Edit</codecvt>

    include <iostream></iostream>

    include <string></string>

    include <locale></locale>

    include <codecvt></codecvt>

    include "tinyxml.h"

    // Helper function to convert std::wstring to UTF-8 std::string
    std::string WStringToUtf8(const std::wstring& wstr) {
    std::wstring_convert<std::codecvt_utf8\<wchar_t>> converter;
    return converter.to_bytes(wstr);
    }</std::codecvt_utf8\<wchar_t>

    // Helper function to convert UTF-8 std::string to std::wstring
    std::wstring Utf8ToWString(const std::string& str) {
    std::wstring_convert<std::codecvt_utf8\<wchar_t>> converter;
    return converter.from_bytes(str);
    }</std::codecvt_utf8\<wchar_t>

    int main() {
    TiXmlDocument doc;

    // Create XML declaration
    TiXmlDeclaration* decl = new TiXmlDeclaration("1.0", "utf-8", "");
    doc.LinkEndChild(decl);
    
    // Create root element
    TiXmlElement* root = new TiXmlElement("Root");
    doc.LinkEndChild(root);
    
    // Unicode string (wstring)
    std::wstring unicodeText = L"Привет, мир! 🌍"; // "Hello, world!" in Russian + Emoji
    
    // Convert to UTF-8 and insert into XML
    TiXmlElement* messageElement = new TiXmlElement("Message");
    messageElement->LinkEndChild(new TiXmlText(WStringToUtf8(unicodeText).c_str()));
    root->LinkEndChild(messageElement);
    
    // Save to file or print to console
    doc.SaveFile("unicode_example.xml");
    
    // Read back the value
    TiXmlElement* retrievedElement = root->FirstChildElement("Message");
    if (retrievedElement) {
        std::wstring retrievedWString = Utf8ToWString(retrievedElement->GetText());
        std::wcout << L"Retrieved Text: " << retrievedWString << std::endl;
    }
    
    return 0;
    

    }
    Explanation:
    Converts std::wstring to UTF-8 std::string before inserting into TinyXML.
    Saves XML in UTF-8 format.
    Reads XML data and converts UTF-8 std::string back to std::wstring for proper Unicode handling.
    Notes:
    std::wstring_convert is deprecated in C++17. For modern C++, consider std::codecvt_utf8 with std::wstring_convert alternatives like ICU or std::wstring-compatible libraries.
    TinyXML-2 works similarly, but you would use tinyxml2::XMLElement and tinyxml2::XMLText.
    Would you like an updated example for TinyXML-2? �

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.