Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to read language name from xml as it is



  • 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!


  • Moderators

    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


  • 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!


  • Moderators

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



  • 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?


  • Lifetime Qt Champion

    @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.



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


Log in to reply