Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to write/read properly UTF-8 strings to/from XML files?
QtWS25 Last Chance

How to write/read properly UTF-8 strings to/from XML files?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 3.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xtingray
    wrote on last edited by xtingray
    #1

    Hi,
    I am working on a multi-platform application that handles an XML file containing data created by users in different languages. My requirement is to support any kind of language/keyboard character , so I need to find the best way to write and read my XML file without losing the content, even if my application is used from different operating systems (Windows, Linux, Mac).

    Is this actually possible?

    I was reading some articles from here and there, but I couldn't find anything really useful:
    https://wiki.qt.io/Strings_and_encodings_in_Qt

    For now, I am writing strings like this:

    QString value = "Text";
    root.setAttribute("variable", value);
    

    And reading them like this:

    QString variable = root.attribute("variable", "");
    

    Obviously, it's not working as I need it. What should be the best way to do this?
    Thanks!


    Qt Developer

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You'll probably be interested in QTextCodec and the related Codec Example.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • X Offline
        X Offline
        xtingray
        wrote on last edited by
        #3

        Thanks! That's exactly what I was looking for.

        Now, I have another question: can I use a specific codec to support several languages? is there a "universal" codec or something like that? (i.e. UTF-8)

        What are the best practices in that sense?

        P.S.: I need that the encoding action is transparent for the users.


        Qt Developer

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          IIRC there's no absolute universal codec that covers everything but if your application should only read and write from files it has generated then you can choose the codec you will use. AFAIK, UTF-8 should have you covered for most cases.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • X Offline
            X Offline
            xtingray
            wrote on last edited by xtingray
            #5

            Just the last question:
            Now I know how to handle the locale encoding/decoding part but my XML has this structure:

            <document>
            <user name="Ali Baba" age="38"/>
            <profession title="carpenter" experience="3"/>
            ...
            ...
            </document>
            

            The attributes "name" and "title" are part of the variables that I need to encode/decode (I don't need to encode the whole XML file), so I was looking for examples about how to store QByteArray variables as XML attributes with no luck.

            Is it possible to save/load QByteArray values as parameters of an XML file?


            Qt Developer

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Don't try that, encode the complete file. That's really a bad idea to put text in several different encodings in a file. How would you know when and what to decode ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • X Offline
                X Offline
                xtingray
                wrote on last edited by
                #7

                After trying several approaches, finally I found a very simple way to solve my problem. All I had to do was to add this line at the beginning of my main.cpp file:

                QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
                

                After that change, all the strings used in my project were stored using the codec "UTF-8". I created several files and I could edit them from Windows, Linux and Mac without losing special characters. Problem solved.

                Thank you so much for your advice!


                Qt Developer

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved