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)的更多信息,请参阅第 1 级和第 2 级核心规范。如需了解 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 、命名空间 URI 为nsURI 的属性。如果该属性不存在,则返回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 、命名空间 URI 为nsURI 的属性相对应的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 和命名空间URInsURI 的该元素的后代。返回列表中元素的顺序即为前序遍历过程中遇到它们的顺序。
另请参阅 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 、命名空间 URI 为nsURI 的属性,则返回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">该 tagname 函数将返回“img”。
另请参阅 setTagName()。
QString QDomElement::text() const
返回该元素的文本或一个空字符串。
示例:
<h1>Hello <b>Qt</b> <![CDATA[<xml is cool>]]></h1><h1> 标签的QDomElement 对象的 text() 函数将返回以下文本:
Hello Qt <xml is cool>该函数会忽略注释。它仅对QDomText 和QDomCDATASection 对象进行求值。
QDomElement &QDomElement::operator=(const QDomElement &other)
将other 分配给此DOM元素。
复制的数据是共享的(浅拷贝):修改一个节点也会更改另一个节点。若要进行深拷贝,请使用 `cloneNode()`。
© 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.