QDomElement Class
QDomElement 类代表 DOM 树中的一个元素。更多
头文件: | #include <QDomElement> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Xml) target_link_libraries(mytarget PRIVATE Qt6::Xml) |
qmake: | QT += xml |
继承: | QDomNode |
- 所有成员(包括继承成员)的列表
- QDomElement 属于XML 类。
注意:该类中的所有函数都是可重入的。
公共函数
QDomElement() | |
QDomElement(const QDomElement &element) | |
QString | attribute(const QString &name, const QString &defValue = QString()) const |
QString | attributeNS(const QString &nsURI, const QString &localName, const QString &defValue = QString()) const |
QDomAttr | attributeNode(const QString &name) |
QDomAttr | attributeNodeNS(const QString &nsURI, const QString &localName) |
QDomNamedNodeMap | attributes() const |
QDomNodeList | elementsByTagName(const QString &tagname) const |
QDomNodeList | elementsByTagNameNS(const QString &nsURI, const QString &localName) const |
bool | hasAttribute(const QString &name) const |
bool | hasAttributeNS(const QString &nsURI, const QString &localName) const |
QDomNode::NodeType | nodeType() const |
void | removeAttribute(const QString &name) |
void | removeAttributeNS(const QString &nsURI, const QString &localName) |
QDomAttr | removeAttributeNode(const QDomAttr &oldAttr) |
void | setAttribute(const QString &name, const QString &value) |
void | setAttribute(const QString &name, double value) |
void | setAttribute(const QString &name, float value) |
void | setAttribute(const QString &name, int value) |
void | setAttribute(const QString &name, qlonglong value) |
void | setAttribute(const QString &name, qulonglong value) |
void | setAttribute(const QString &name, uint value) |
void | setAttributeNS(const QString &nsURI, const QString &qName, const QString &value) |
void | setAttributeNS(const QString &nsURI, const QString &qName, double value) |
void | setAttributeNS(const QString &nsURI, const QString &qName, int value) |
void | setAttributeNS(const QString &nsURI, const QString &qName, qlonglong value) |
void | setAttributeNS(const QString &nsURI, const QString &qName, qulonglong value) |
void | setAttributeNS(const QString &nsURI, const QString &qName, uint value) |
QDomAttr | setAttributeNode(const QDomAttr &newAttr) |
QDomAttr | setAttributeNodeNS(const QDomAttr &newAttr) |
void | setTagName(const QString &name) |
QString | tagName() const |
QString | text() const |
QDomElement & | operator=(const QDomElement &other) |
详细描述
元素有一个tagName() 和零个或多个与之关联的属性。标签名称可通过setTagName() 进行更改。
元素属性由QDomAttr 对象表示,可使用attribute() 和attributeNode() 函数进行查询。可以使用setAttribute() 和setAttributeNode() 函数设置属性。可以使用removeAttribute() 删除属性。这些函数有名称空间感知的对应函数,即setAttributeNS(),setAttributeNodeNS() 和removeAttributeNS().
如果要访问节点的文本,请使用text() ,例如
QDomElement e = //... //... QString s = e.text()
text() 函数以递归方式查找文本(因为并非所有元素都包含文本)。如果要查找一个节点所有子节点中的所有文本,可遍历子节点查找QDomText 节点,例如
QString text; QDomElement element = doc.documentElement(); for(QDomNode n = element.firstChild(); !n.isNull(); n = n.nextSibling()) { QDomText t = n.toText(); if (!t.isNull()) text += t.data(); }
请注意,我们会尝试将每个节点转换为文本节点,并在节点上直接使用text() 而不是firstChild().toText().data() 或 n.toText().data() ,因为节点可能不是文本元素。
使用elementsByTagName() 或elementsByTagNameNS() 可以获得具有指定标记名的元素的所有子元素列表。
要浏览 dom 文档中的元素,请使用firstChildElement()、lastChildElement()、nextSiblingElement() 和previousSiblingElement() 。例如,要遍历名为 "database "的根元素中名为 "entry "的所有子元素,可以使用:
QDomDocument doc = // ... QDomElement root = doc.firstChildElement("database"); QDomElement elt = root.firstChildElement("entry"); for (; !elt.isNull(); elt = elt.nextSiblingElement("entry")) { // ... }
有关文档对象模型的更多信息,请参见一级和二级核心。有关 DOM 实现的一般介绍,请参阅QDomDocument 文档。
成员函数文档
QDomElement::QDomElement()
构造一个空元素。使用QDomDocument::createElement() 函数可构建有内容的元素。
QDomElement::QDomElement(const QDomElement &element)
构造element 的副本。
副本的数据是共享的(浅层副本):修改一个节点也会改变另一个节点。如果要进行深度复制,请使用cloneNode() 。
QString QDomElement::attribute(const QString &name, const QString &defValue = QString()) const
返回名为name 的属性。如果属性不存在,则返回defValue 。
另请参阅 setAttribute()、attributeNode()、setAttributeNode() 和attributeNS()。
QString QDomElement::attributeNS(const QString &nsURI, const QString &localName, const QString &defValue = QString()) const
返回具有本地名称localName 和命名空间 URInsURI 的属性。如果属性不存在,则返回defValue 。
另请参阅 setAttributeNS()、attributeNodeNS()、setAttributeNodeNS() 和attribute()。
QDomAttr QDomElement::attributeNode(const QString &name)
返回与名为name 的属性相对应的QDomAttr 对象。如果不存在该属性,则返回null attribute 。
另请参阅 setAttributeNode()、attribute()、setAttribute() 和attributeNodeNS()。
QDomAttr QDomElement::attributeNodeNS(const QString &nsURI, const QString &localName)
返回与具有本地名称localName 和名称空间 URInsURI 的属性相对应的QDomAttr 对象。如果不存在这样的属性,则返回null attribute 。
另请参阅 setAttributeNodeNS()、setAttributeNode()、attribute() 和setAttribute()。
QDomNamedNodeMap QDomElement::attributes() const
返回包含此元素所有属性的QDomNamedNodeMap 。
另请参阅 attribute()、setAttribute()、attributeNode() 和setAttributeNode() 。
QDomNodeList QDomElement::elementsByTagName(const QString &tagname) const
返回QDomNodeList ,其中包含在以该元素为根的元素子树的预排序遍历过程中遇到的名为tagname 的该元素的所有后代。返回列表中元素的顺序是它们在预序遍历中遇到的顺序。
另请参见 elementsByTagNameNS() 和QDomDocument::elementsByTagName()。
QDomNodeList QDomElement::elementsByTagNameNS(const QString &nsURI, const QString &localName) const
返回一个QDomNodeList ,其中包含在以该元素为根的元素子树的预序遍历过程中遇到的该元素的所有子元素(本地名称为localName ,命名空间 URI 为nsURI )。返回列表中元素的顺序与预序遍历时遇到的顺序一致。
另请参阅 elementsByTagName() 和QDomDocument::elementsByTagNameNS()。
bool QDomElement::hasAttribute(const QString &name) const
如果该元素有名为name 的属性,则返回true
;否则返回false
。
注意:此函数不考虑命名空间的存在。因此,指定的名称将根据包含任何可能存在的命名空间前缀的全称属性名称进行测试。
使用hasAttributeNS() 可明确测试具有特定命名空间和名称的属性。
bool QDomElement::hasAttributeNS(const QString &nsURI, const QString &localName) const
如果该元素的属性具有本地名称localName 和命名空间 URInsURI ,则返回true
;否则返回 false。
QDomNode::NodeType QDomElement::nodeType() const
返回ElementNode
。
void QDomElement::removeAttribute(const QString &name)
删除该元素中名为 namename 的属性。
另请参阅 setAttribute()、attribute() 和removeAttributeNS()。
void QDomElement::removeAttributeNS(const QString &nsURI, const QString &localName)
从该元素中删除本地名称为localName 、命名空间 URI 为nsURI 的属性。
另请参阅 setAttributeNS()、attributeNS() 和removeAttribute()。
QDomAttr QDomElement::removeAttributeNode(const QDomAttr &oldAttr)
从元素中删除属性oldAttr 并返回。
另请参阅 attributeNode() 和setAttributeNode()。
void QDomElement::setAttribute(const QString &name, const QString &value)
添加名为name 的属性,其值为value 。如果存在同名属性,其值将由value 代替。
另请参阅 attribute()、setAttributeNode() 和setAttributeNS()。
void QDomElement::setAttribute(const QString &name, double value)
这是一个重载函数。
格式化总是使用QLocale::C 。
void QDomElement::setAttribute(const QString &name, float value)
这是一个重载函数。
格式化总是使用QLocale::C 。
void QDomElement::setAttribute(const QString &name, int value)
这是一个重载函数。
格式化总是使用QLocale::C 。
void QDomElement::setAttribute(const QString &name, qlonglong value)
这是一个重载函数。
格式化总是使用QLocale::C 。
void QDomElement::setAttribute(const QString &name, qulonglong value)
这是一个重载函数。
格式化总是使用QLocale::C 。
void QDomElement::setAttribute(const QString &name, uint value)
这是一个重载函数。
格式化总是使用QLocale::C 。
void QDomElement::setAttributeNS(const QString &nsURI, const QString &qName, const QString &value)
添加限定名称为qName 、名称空间 URI 为nsURI 、值为value 的属性。如果存在具有相同本地名称和名称空间 URI 的属性,则其前缀会被qName 的前缀替换,其值会被value 替换。
虽然qName 是限定名称,但本地名称用于决定是否替换现有属性的值。
另请参阅 attributeNS()、setAttributeNodeNS() 和setAttribute()。
void QDomElement::setAttributeNS(const QString &nsURI, const QString &qName, double value)
这是一个重载函数。
void QDomElement::setAttributeNS(const QString &nsURI, const QString &qName, int value)
这是一个重载函数。
void QDomElement::setAttributeNS(const QString &nsURI, const QString &qName, qlonglong value)
这是一个重载函数。
void QDomElement::setAttributeNS(const QString &nsURI, const QString &qName, qulonglong value)
这是一个重载函数。
void QDomElement::setAttributeNS(const QString &nsURI, const QString &qName, uint value)
这是一个重载函数。
QDomAttr QDomElement::setAttributeNode(const QDomAttr &newAttr)
为该元素添加属性newAttr 。
如果该元素有另一个与newAttr 同名的属性,则该函数将替换该属性并返回它;否则,该函数将返回null attribute 。
另请参阅 attributeNode()、setAttribute() 和setAttributeNodeNS()。
QDomAttr QDomElement::setAttributeNodeNS(const QDomAttr &newAttr)
为该元素添加属性newAttr 。
如果该元素有另一个属性的本地名称和命名空间 URI 与newAttr 相同,则该函数将替换该属性并返回它;否则,该函数将返回null attribute 。
另请参阅 attributeNodeNS()、setAttributeNS() 和setAttributeNode()。
void QDomElement::setTagName(const QString &name)
将此元素的标记名设置为name 。
另请参阅 tagName() 。
QString QDomElement::tagName() const
返回此元素的标记名。对于像这样的 XML 元素
<img src="myimg.png">
标签名将返回 "img"。
另请参见 setTagName().
QString QDomElement::text() const
返回元素的文本或空字符串。
例如
<h1>Hello <b>Qt</b> <![CDATA[<xml is cool>]]></h1>
QDomElement 的<h1>
标签的 text() 函数将返回以下文本:
Hello Qt <xml is cool>
该函数忽略注释。它只评估QDomText 和QDomCDATASection 对象。
QDomElement &QDomElement::operator=(const QDomElement &other)
为该 DOM 元素指定other 。
副本的数据是共享的(浅层副本):修改一个节点也会改变另一个节点。如果要进行深度复制,请使用cloneNode()。
© 2025 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.