本页内容

QXmlStreamAttributes Class

QXmlStreamAttributes 类表示一个QXmlStreamAttribute 的向量。更多内容...

头文件: #include <QXmlStreamAttributes>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
继承自: QList

注意:该类中的所有函数均为可重入函数。

公共函数

QXmlStreamAttributes()
void append(const QString &namespaceUri, const QString &name, const QString &value)
void append(const QString &qualifiedName, const QString &value)
bool hasAttribute(QAnyStringView qualifiedName) const
bool hasAttribute(QAnyStringView namespaceUri, QAnyStringView name) const
QStringView value(QAnyStringView namespaceUri, QAnyStringView name) const
QStringView value(QAnyStringView qualifiedName) const

详细说明

当读取器报告start element 时,QXmlStreamReader 会在attributes()中返回属性。该类还可以与QXmlStreamWriter 一起用作writeAttributes()的参数。

便捷函数value() 会遍历该向量,并根据给定的 namespaceUri 和属性名称返回相应的属性值。

可通过append() 添加新属性。

成员函数文档

QXmlStreamAttributes::QXmlStreamAttributes()

QXmlStreamAttributes 的构造函数。

void QXmlStreamAttributes::append(const QString &namespaceUri, const QString &name, const QString &value)

在由namespaceUri 描述的命名空间中,添加一个新属性,其name 为该命名空间,value为value 。namespaceUri 可以为空。

void QXmlStreamAttributes::append(const QString &qualifiedName, const QString &value)

添加一个新属性,其限定名qualifiedName ,值为value 。

这是一个重载函数。

bool QXmlStreamAttributes::hasAttribute(QAnyStringView qualifiedName) const

如果该QXmlStreamAttributes 对象具有一个全限定名称为qualifiedName 的属性,则返回true ;否则返回false 。

请注意,此方法不支持命名空间。 例如,如果该QXmlStreamAttributes 包含一个词法名为“xlink:href”的属性,这并不能说明 XLink 命名空间中存在名为href 的属性,因为前缀xlink 可以绑定到任何命名空间。对于需要支持命名空间的代码,请使用将命名空间 URI 和本地名称作为参数的重载方法。

bool QXmlStreamAttributes::hasAttribute(QAnyStringView namespaceUri, QAnyStringView name) const

如果该QXmlStreamAttributes 具有一个属性的命名空间URI和名称分别与namespaceUri 和name 对应,则返回true ;否则返回false 。

这是一个重载函数。

[noexcept] QStringView QXmlStreamAttributes::value(QAnyStringView namespaceUri, QAnyStringView name) const

返回由namespaceUri 描述的命名空间中name 属性的值;如果该属性未定义,则返回空字符串引用。namespaceUri 可以为空。

注意:在 Qt 6.6 之前的版本中,此函数被实现为一个重载的 set,仅接受QString 和QLatin1StringView 的组合。

[noexcept] QStringView QXmlStreamAttributes::value(QAnyStringView qualifiedName) const

返回全称qualifiedName 对应的属性值;如果该属性未定义,则返回空字符串引用。全称是指XML数据中属性的原始名称,由命名空间前缀、冒号以及属性的本地名称组成。 由于命名空间前缀并非唯一(相同的前缀可能指向不同的命名空间,不同的前缀也可能指向同一个命名空间),因此不应使用限定名称,而应使用已解析的 namespaceUri 以及属性的本地名称。

注意:在 Qt 6.6 之前的版本中,该函数被实现为一个重载的 set 函数,仅接受QString 和QLatin1StringView 。

这是一个重载函数。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.