Problems encountered in reading xml files using QXmlStreamReader in Qt

Qt series article directory

foreword

<?xml version="1.0" encoding="UTF-8"?>
<note>
  <yolov5>E:/work/python_jdk/yolov5/dist/detect/detect.exe</yolov5>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>
QString parseXml(QString xmlFile, QString nodeName)
    {
    
    
        QFile file(xmlFile);
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
    
    
            qDebug() << "Cannot open file";
            return "Cannot open file";
        }

        QXmlStreamReader xml(&file);
        QString nodeContent;

        while(!xml.atEnd() && !xml.hasError()) {
    
    
            xml.readNext();

            if(xml.isStartElement()) {
    
    
                if(xml.name() == nodeName) {
    
    
                    nodeContent = xml.readElementText();
                    qDebug() << "Content of " << nodeName << ":" << xml.readElementText();

                    qDebug() << "nodeContent" << nodeContent;
                    break;
                }
            }
        }

        if(xml.hasError()) {
    
    
            qDebug() << "XML Error:" << xml.errorString();
            return xml.errorString();
        }

        xml.clear();
        file.close();

        return nodeContent;
    }

The above is the first paragraph is the xml text, and the second paragraph is the code to read the content of the xml file

1. Why xml.readElementText() is empty

insert image description here

problem causes

This is because after the QXmlStreamReader::readElementText() function is called, the internal position will be updated to the end position of the current label. Therefore, when you call xml.readElementText() twice in a row, the second call is actually at the position, and there is nothing to read at this time, so an empty string is returned.

Your code should save the result the first time it calls readElementText()

Introduction to QXmlStreamReader class

Introduction, for XML content, usually, we only care about the parsing of XML elements. At this time, it can be realized by the convenience function readNextStartElement() in QXmlStreamReader. Brief introduction Detailed introduction Using more references Detailed introduction The method used before mainly uses readNext() to read the next tag and return the corresponding type.

QStringRef documentEncoding(); //Get the XML encoding, if not specified in the XML declaration, return NULL
QStringRef documentVersion (); //Get the XML version, if not specified in the XML declaration, return NULL
bool atEnd ();
//if Returns true if the reader has read to the end of the XML document, or if an error occurred and reading was aborted. Otherwise, it will return false.
//Read the XML content, if it reads to the end of the XML, or reads an error, it will return true, otherwise it will return false
//When returning true, you can use error() to judge whether the reading error is
Error error (); // Return error type, if there is no error, it will return QXmlStreamReader::NoError (value 0)

void raiseError ( constQString & message = QString() );
//Actively report errors, and fill in the message error information (can be obtained through errorString()), so that the next time atEnd() is called, it will directly return the true value
QString errorString( );
// Get the error message that occurred

void QXmlStreamReader::setDevice ( QIODevice *device );
//Set the parsing device of QXmlStreamReader, which is equivalent to QXmlStreamReader ( QIODevice *) constructor
TokenType readNext(); //Read a token and return the flag bit of the data, also It can be passed through
//Common marks are as follows:
QXmlStreamReader::StartDocument //Document start position, used to define XML version, encoding and other information,
QXmlStreamReader::StartElement //Element start position
QXmlStreamReader::EndElement //Element end position
// For example, Anna
// The element is the start and end position of the element.
// "Anna" information can be obtained through the readElementText() member function.

QXmlStreamAttributes attributes(); // Get the attributes of the element
// For example
// You can get the element value "of pictures" through attributes().value("term").toString()

Guess you like

Origin blog.csdn.net/aoxuestudy/article/details/131453839