How to read the xml file using QXmlStreamReader



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





  • please help me with the code



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



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

    }

    @



  • What method you want to use? JSON or DOM ?


  • Banned

    This post is deleted!

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.