使用 QT 进行 XML 解析

XML parsing with QT

本文关键字:解析 XML 进行 QT 使用      更新时间:2023-10-16

当我们尝试使用 QT 解析 xml 文档时,我们使用如下内容:

QString str;
QXmlStreamAttributes attrib = xml_reader.attributes();
if(attrib.hasAttribute("id"))
{
     str = attrib.value("id").toString();
}

为此,我需要知道该属性称为"id"。有没有办法在不知道其名称的情况下读取第一个属性?

提前谢谢。

QXmlStreamAttributes 类继承自 QVector<QXmlStreamAttribute> .这意味着您可以像使用 QVector 一样"循环"对象

您应该能够访问第一项:

attrib[0].name();  //containst a QStringRef to the name
attrib[0].value(); //containst a QStringRef to the value

顺便说一下,强烈建议您先检查QVector大小;)

attrib.size();     //contains the number of attributes