V
V
Vladislav Shcherbakov2015-03-15 02:51:19
Qt
Vladislav Shcherbakov, 2015-03-15 02:51:19

How to parse xml using QXmlStreamReader in Qt?

There is such an xml file with the following structure:

<?xml version="1.0" encoding="windows-1251"?>
<FileIndex>
    <Name>lol.xml</Name>
    <Size>0</Size>
    <Path>E:/test/build-untitled-Desktop_Qt_5_4_1_MinGW_32bit-Release/lol.xml</Path>
    <Created>Сб мар 14 21:55:37 2015</Created>
    <Name>Makefile</Name>
    <Size>20399</Size>
    <Path>E:/test/build-untitled-Desktop_Qt_5_4_1_MinGW_32bit-Release/Makefile</Path>
    <Created>Чт мар 12 19:00:45 2015</Created>
</FileIndex>

There is a file name. If such a file is in the list, then you need to display the path to it.
That is, if there is a Name tag, then you need to look into it and check the text with the existing one. If the text matches, go to the nearest Path tag, pull the path into a variable and exit the loop.
I started small and just try to print the names of all the files:
while(!xmlReader.atEnd())
    {
        QXmlStreamReader::TokenType token = xmlReader.readNext();
        if(token == QXmlStreamReader::StartElement) {
            if(xmlReader.name() == "Name") {
                xmlReader.readNext();
                ui->textBrowser->setText(xmlReader.text().toString());
            }
        }
    }

But the name of only one file (the last one) will get into the text field, but all should. What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yaroslav, 2015-03-15
@vlad007700

I dare to assume that all are displayed, but very quickly and at the end there is only one, the last one, which you see. This line is ui->textBrowser->setText(xmlReader.text().toString()); overwrites the previous value and inserts the new one. Use the append() method and then everything will appear.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question