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 the xml file using QXmlStreamReader

How to read the xml file using QXmlStreamReader

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 28.7k Views 1 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.
  • M Offline
    M Offline
    mythili
    wrote on last edited by
    #1

    Hi I am having xml file. I want to read all the Id's from <Qu Id = > and the file names inside the tag 'file'.Please help me.

    My xml file is
    @<Response>
    <Qu Id="1">
    <An_Pas>
    <file>Q1P1.png</file>
    <file>Q1P2.png</file>
    </An_Pas>
    <Ro_pas>
    <file>Q1R1.png</file>
    </Ro_pas>
    </Qu>
    <Qu Id="2">
    <An_Pas>
    <file>Q2P1.png</file>
    </An_Pas>
    <Ro_pas>
    <file>Q2R1.png</file>
    <file>Q2R2.png</file>
    </Ro_pas>
    </Qu>
    </Response>@

    1 Reply Last reply
    0
    • francescmmF Offline
      francescmmF Offline
      francescmm
      wrote on last edited by
      #2

      Here are some examples:

      "QtForum Parsing XML document":http://www.qtforum.org/article/28453/qxmlstreamreader-parsing-a-simple-xml-document.html

      "Parsing XML in Qt":http://www.developer.nokia.com/Community/Wiki/QXmlStreamReader_to_parse_XML_in_Qt

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mythili
        wrote on last edited by
        #3

        please help me with the code

        1 Reply Last reply
        0
        • francescmmF Offline
          francescmmF Offline
          francescmm
          wrote on last edited by
          #4

          The second link has the code your are looking for. You only have to copy and change some lines for your tags.

          I can help you in doubts or problems but you have to try it yourself.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mythili
            wrote on last edited by
            #5

            I have tried. But I am unable to read. Please check this and make changes if any. Thank u very much
            @
            void MainWindow::parseXML()
            {

            QFile* file = new QFile&#40;":/new/prefix1/recov.xml"&#41;;
            if (!file->open(QIODevice::ReadOnly | QIODevice::Text)) {
                QMessageBox::critical(this,
                                      "QXSRExample::parseXML",
                                      "Couldn't open example.xml",
                                      QMessageBox::Ok);
                return;
            }
            /* QXmlStreamReader takes any QIODevice. */
            QXmlStreamReader xml(file);
            QList< QMap<QString,QString> > persons;
            /* We'll parse the XML until we reach end of it.*/
            while(!xml.atEnd() &&  !xml.hasError()) {
                /* Read next element.*/
                QXmlStreamReader::TokenType token = xml.readNext();
                /* If token is just StartDocument, we'll go to next.*/
                if(token == QXmlStreamReader::StartDocument) {
                    continue;
                }
                /* If token is StartElement, we'll see if we can read it.*/
                if(token == QXmlStreamReader::StartElement) {
                    /* If it's named persons, we'll go to the next.*/
                    if(xml.name() == "Response") {
                        continue;
                    }
                    /* If it's named person, we'll dig the information from there.*/
                    if(xml.name() == "Qu") {
                        persons.append(this->parsePerson(xml));
                    }
                }
            }
            /* Error handling. */
            if(xml.hasError()) {
                QMessageBox::critical(this,
                                      "QXSRExample::parseXML",
                                      xml.errorString(),
                                      QMessageBox::Ok);
            }
            /* Removes any device() or data from the reader
             * and resets its internal state to the initial state. */
            xml.clear();
            this->addPersonsToUI(persons);
            

            }

            QMap<QString, QString> MainWindow::parsePerson(QXmlStreamReader& xml)
            {

            QMap<QString, QString> person;
            /* Let's check that we're really getting a person. */
            if(xml.tokenType() != QXmlStreamReader::StartElement &&
                    xml.name() == "Qu") {
                return person;
            }
            /* Let's get the attributes for person */
            QXmlStreamAttributes attributes = xml.attributes();
            /* Let's check that person has id attribute. */
            if(attributes.hasAttribute("Id"))
            {
                /* We'll add it to the map. */
                person["Id"] = attributes.value("Id").toString();
                qDebug()<<"id:"<<person["Id"]<<endl;
            }
            
            /* Next element... */
            xml.readNext();
            
                if(xml.name() == "An_Pas")
                {
            
                    if(xml.name() == "file")
                    {
                        QString elementName = xml.text().toString();
                        qDebug()<<"add:"<<elementName<<endl;
                    }
                    xml.readNext();
            
                }
            
            return person;
            

            }

            @

            1 Reply Last reply
            0
            • B Offline
              B Offline
              bjanuario
              wrote on last edited by
              #6

              What method you want to use? JSON or DOM ?

              1 Reply Last reply
              0
              • W Offline
                W Offline
                walshmagger
                Banned
                wrote on last edited by
                #7
                This post is deleted!
                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