我有 XML 格式为
<Value active="false">8.0</Value>
我正在使用 QXmlStreamReader解析 XML。我能够读取标签“值”和实际值“8.0”,但无法读取 XML 的 active="false">
部分。
const QString label = xmlReader.name().toString();
xmlReader.readNext();
const QString text = xmlReader.text().toString();
在 label
中给出“Value”,在 text
中给出“8.0”。
我将代码修改为
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();
关于c++ - 使用 QXmlStreamReader 读取 XML 标签内的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30318277/