使用QXmlStreamReader读取XML标签内的值

Read value inside XML tag using QXmlStreamReader

本文关键字:标签 XML QXmlStreamReader 读取 使用      更新时间:2023-10-16

我有XML格式

<Value active="false">8.0</Value>

我使用QXmlStreamReader来解析XML。我能够读取标签&;value &;和实际值"8.0",但我无法读取XML的active="false">部分。

    const QString label = xmlReader.name().toString();
    xmlReader.readNext();
    const QString text = xmlReader.text().toString();

给出了"Value"在label和"8.0";in text .

我将代码修改为

const QString label = xmlReader.name().toString();
const QString labelIn = xmlReader.readElementText();
xmlReader.readNext();
const QString text = xmlReader.text().toString();

但是labelIn返回一个空字符串。我错过什么了吗?

我正在使用Qt 5.3

节点的active="false"部分称为属性。要获得它的值,您需要使用QXmlStreamReader类的专用API:

QXmlStreamAttributes attribs = xmlReader->attributes();
QString attr = attribs.value("active").toString();