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 read language name from xml as it is
Forum Updated to NodeBB v4.3 + New Features

How to read language name from xml as it is

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 468 Views 2 Watching
  • 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.
  • S Offline
    S Offline
    Srujan18
    wrote on last edited by Srujan18
    #1

    I am trying to parse an XML file for the first time and I got a requirement like I need to read different country codes and their respective languages from the XML file as it is. And need to display them in UI. Now for my task to complete, there is a hurdle like "reading different country language name as is"!! I am able to read it but not as is. Can anyone help me to get rid of this hurdle?

    For your information, the sample XML file looks like the following...

    e66f5001-e013-443b-97d5-3e2f8cfc3af3-image.png

    Sorry if I make any querying mistakes. Thank you in advance!

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      What exactly do you have problem with? What do you mean by "I am able to read it but not as is."?

      Please show:

      • relevant parts of your code
      • what output you get
      • what output you expect to get instead

      (Z(:^

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Srujan18
        wrote on last edited by
        #3

        Okay. Please find the below code.

        void parseXMLFile()
        {
            QStatusBar *statusBar = new QStatusBar(this);
            QFile *xmlFile = new QFile("xmlfilename.xml");
            if (nullptr != xmlFile) {
                if (!xmlFile->open(QIODevice::ReadOnly | QIODevice::Text))
                {
                    statusBar->showMessage("Unable to open xml file!");
                }
                else
                {
                    QXmlStreamReader xmlStreamReader(xmlFile);
                    while(!xmlStreamReader.hasError() && !xmlStreamReader.atEnd())
                    {
                        // Read next element
                        QXmlStreamReader::TokenType tokenType = xmlStreamReader.readNext();
        
                        //If token type is just start document then go for next
                        if ( tokenType == QXmlStreamReader::StartDocument)
                            continue;
                        // If token type is start element then read it
                        if ( tokenType == QXmlStreamReader::StartElement )
                        {
                            if (xmlStreamReader.name() == "countries")
                                continue;
                            if (xmlStreamReader.name() == "country")
                            {
                                parseCountry(xmlStreamReader);
                            }
                        }
                    }
                }
            }
        }
        
        void parseCountry(QXmlStreamReader &xml)
        {
            QMap<QString, QString> countryCodeNameMap;
            QXmlStreamAttributes attributes = xml.attributes();
        
             if (attributes.hasAttribute("code") && attributes.hasAttribute("iso"))
             {
                    qDebug()<< "code: " << attributes.value("code").toString();
                    qDebug()<< "iso: " << attributes.value("iso").toString();
        
                    xml.readNext();
                    qDebug() <<"xml text: "<<xml.text().toString();
        
                    countryCodeNameMap[attributes.value("code").toString()] = xml.text().toString();
            }
        
            QMapIterator<QString, QString> mapIter(countryCodeNameMap);
            while(mapIter.hasNext()) {
                mapIter.next();
                m_countryMap.insert(mapIter.key(),mapIter.value());
            }
        }
        

        Output:
        code: "JP"
        iso: "392"
        xml name: "country"
        xml text: "???"

        Expected output:
        code: "JP"
        iso: "392"
        xml name: "country"
        xml text: "日本語"

        I hope I have answered your question by providing the necessary information!

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Is the XML file marked as using UTF-8 encoding?

          (Z(:^

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Srujan18
            wrote on last edited by
            #5

            No, I guess. I am using a simple .xml file to parse. In code also I am not parsing using any encoding technique. Did I answer your question? If not can you please explain what you are expecting?

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Srujan18 said in How to read language name from xml as it is:

              I am using a simple .xml file to parse.

              This does not matter as long as there is a correct header: https://www.educba.com/xml-encoding/

              Where do you develop? On Windows? If so then qDebug() will not output anything useful as long as your locale is not japanese. Use a QLabel or QMessageBox instead.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              3
              • S Offline
                S Offline
                Srujan18
                wrote on last edited by
                #7

                I am working on the Windows platform. It worked when I pass to the UI element. Thank you!

                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