在Qt中解析XML文件

时间:2022-08-04 17:00:12

Let's say I have an XML file like this:

假设我有一个这样的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<name>
    <id>1</id>
</name>

How can I parse it and get the value of id?

如何解析它并获取id的值?

std::string id = ...;

2 个解决方案

#1


19  

Something like this will work:

类似这样的东西会起作用:

xmlFile = new QFile("xmlFile.xml");
        if (!xmlFile->open(QIODevice::ReadOnly | QIODevice::Text)) {
                QMessageBox::critical(this,"Load XML File Problem",
                "Couldn't open xmlfile.xml to load settings for download",
                QMessageBox::Ok);
                return;
        }
xmlReader = new QXmlStreamReader(xmlFile);


//Parse the XML until we reach end of it
while(!xmlReader->atEnd() && !xmlReader->hasError()) {
        // Read next element
        QXmlStreamReader::TokenType token = xmlReader->readNext();
        //If token is just StartDocument - go to next
        if(token == QXmlStreamReader::StartDocument) {
                continue;
        }
        //If token is StartElement - read it
        if(token == QXmlStreamReader::StartElement) {

                if(xmlReader->name() == "name") {
                        continue;
                }

                if(xmlReader->name() == "id") {
                    qDebug() << xmlReader->readElementText();
                }
        }
}

if(xmlReader->hasError()) {
        QMessageBox::critical(this,
        "xmlFile.xml Parse Error",xmlReader->errorString(),
        QMessageBox::Ok);
        return;
}

//close reader and flush file
xmlReader->clear();
xmlFile->close();

#2


0  

I made a simplified version of @Scrivener's answer. Instead of reading from a file I just read from a QString variable and I removed the continue; blocks:

我简化了@Scrivener的答案。不是从文件中读取,而是从QString变量中读取,然后删除continue;块:

QString xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
              "<name>\n"
              " <id>1</id>\n"
              "</name>\n";

QXmlStreamReader reader(xml);
while(!reader.atEnd() && !reader.hasError()) {
    if(reader.readNext() == QXmlStreamReader::StartElement && reader.name() == "id") {
        qDebug() << reader.readElementText();
    }
}

#1


19  

Something like this will work:

类似这样的东西会起作用:

xmlFile = new QFile("xmlFile.xml");
        if (!xmlFile->open(QIODevice::ReadOnly | QIODevice::Text)) {
                QMessageBox::critical(this,"Load XML File Problem",
                "Couldn't open xmlfile.xml to load settings for download",
                QMessageBox::Ok);
                return;
        }
xmlReader = new QXmlStreamReader(xmlFile);


//Parse the XML until we reach end of it
while(!xmlReader->atEnd() && !xmlReader->hasError()) {
        // Read next element
        QXmlStreamReader::TokenType token = xmlReader->readNext();
        //If token is just StartDocument - go to next
        if(token == QXmlStreamReader::StartDocument) {
                continue;
        }
        //If token is StartElement - read it
        if(token == QXmlStreamReader::StartElement) {

                if(xmlReader->name() == "name") {
                        continue;
                }

                if(xmlReader->name() == "id") {
                    qDebug() << xmlReader->readElementText();
                }
        }
}

if(xmlReader->hasError()) {
        QMessageBox::critical(this,
        "xmlFile.xml Parse Error",xmlReader->errorString(),
        QMessageBox::Ok);
        return;
}

//close reader and flush file
xmlReader->clear();
xmlFile->close();

#2


0  

I made a simplified version of @Scrivener's answer. Instead of reading from a file I just read from a QString variable and I removed the continue; blocks:

我简化了@Scrivener的答案。不是从文件中读取,而是从QString变量中读取,然后删除continue;块:

QString xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
              "<name>\n"
              " <id>1</id>\n"
              "</name>\n";

QXmlStreamReader reader(xml);
while(!reader.atEnd() && !reader.hasError()) {
    if(reader.readNext() == QXmlStreamReader::StartElement && reader.name() == "id") {
        qDebug() << reader.readElementText();
    }
}