在 Qt 中解析 XML 文件

发布于 2024-09-06 10:13:05 字数 236 浏览 3 评论 0原文

假设我有一个如下所示的 XML 文件:

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

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

std::string id = ...;

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

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

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

std::string id = ...;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凤舞天涯 2024-09-13 10:13:05

像这样的事情会起作用:

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();

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();
妄想挽回 2024-09-13 10:13:05

我制作了@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();
    }
}

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:

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文