R
R
RubySword2020-07-14 10:58:17
Qt
RubySword, 2020-07-14 10:58:17

Why doesn't XmlReader work in QT?

Hello. I am making a simple xml file parser in qt, but the code does not go further than the line if(xmlReader.isStartElement()). The debugger at the breakpoint does not move further through the code. Here is the code in full:

class Maket{
public:
    QString name;
    QString timeBegin;
    QString timeCancel;
    int statValue;
};
QFile file("maket.xml");
    QList<Maket> maketList;
    if(file.open(QIODevice::ReadOnly)){
        QXmlStreamReader xmlReader;
        xmlReader.setDevice(&file);
        xmlReader.readNext();
        while (!xmlReader.atEnd()) {
            if(xmlReader.isStartElement()){
                    Maket maket;
                    if(xmlReader.name() == "measuringpoint"){
                        foreach (const QXmlStreamAttribute& attr, xmlReader.attributes()) {
                            if(attr.name().toString() == "name"){
                                maket.name = attr.value().toString();
                            }
                        }
                    }
                    else if(xmlReader.name() == "period"){
                        foreach (const QXmlStreamAttribute& attr, xmlReader.attributes()) {
                            if(attr.name().toString() == "start"){
                                maket.timeBegin = attr.value().toString();
                            }
                        }
                    }
                    else if(xmlReader.name() == "period"){
                        foreach (const QXmlStreamAttribute& attr, xmlReader.attributes()) {
                            if(attr.name().toString() == "end"){
                                maket.timeCancel = attr.value().toString();
                            }
                        }
                    }
                    else if(xmlReader.name() == "value"){
                        foreach (const QXmlStreamAttribute& attr, xmlReader.attributes()) {
                            if(attr.name().toString() == "value"){
                                maket.statValue = attr.value().toInt();
                            }
                        }
                    }
                    maketList.push_back(maket);
                    xmlReader.readNext();
                }
            file.close();
            }
    }

Please tell me what is wrong

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Jacob E, 2020-07-14
@RubySword

Well, if the isStartElement() method returns false, then the structure of the document is not what the parser code expects.

Returns true if tokenType() equals StartElement; otherwise returns false

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question