QDomElement 类

The QDomElement 类表示 DOM (文档对象模型) 树中一元素。 更多...

头: #include <QDomElement>
qmake: QT += xml
继承: QDomNode

注意: 此类的所有函数 可重入 .

公共函数

QDomElement ()
QDomElement (const QDomElement & x )
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 , qlonglong value )
void setAttribute (const QString & name , qulonglong value )
void setAttribute (const QString & name , int value )
void setAttribute (const QString & name , uint value )
void setAttribute (const QString & name , float value )
void setAttribute (const QString & name , double value )
void setAttributeNS (const QString nsURI , const QString & qName , const QString & value )
void setAttributeNS (const QString nsURI , const QString & qName , int value )
void setAttributeNS (const QString nsURI , const QString & qName , uint 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 , double 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 & x )

详细描述

The QDomElement 类表示 DOM (文档对象模型) 树中一元素。

元素拥有 tagName () 和零个或多个关联它们的属性。标签名称可以改变采用 setTagName ().

元素属性的表示通过 QDomAttr 对象,可以查询使用 attribute () 和 attributeNode () 函数。可以设置属性采用 setAttribute () 和 setAttributeNode () 函数。可以移除属性采用 removeAttribute ()。这些函数有名称空间等价函数,即 setAttributeNS (), setAttributeNodeNS () 和 removeAttributeNS ().

若想要访问节点文本,使用 text (),如

QDomElement e = //...
//...
QString s = e.text()
					

The 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")) {
    // ...
}
					

有关文档对象模型的进一步信息,见 级别 1 and 级别 2 核心 。有关 DOM 实现的更一般介绍,见 QDomDocument 文档编制。

成员函数文档编制

QDomElement:: QDomElement ()

构造空元素。使用 QDomDocument::createElement () 函数以构造具有内容的元素。

QDomElement:: QDomElement (const QDomElement & x )

构造副本为 x .

拷贝数据是共享的 (浅拷贝):修改一节点也会改变另一节点。若想要做深拷贝,使用 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 )

返回 QDomAttr 对象相当于属性称为 name 。若不存在这种属性 null 属性 被返回。

另请参阅 setAttributeNode (), attribute (), setAttribute (),和 attributeNodeNS ().

QDomAttr QDomElement:: attributeNodeNS (const QString & nsURI , const QString & localName )

返回 QDomAttr 对象相当于属性带有本地名称 localName 和名称空间 URI nsURI 。若不存在这种属性 null 属性 被返回。

另请参阅 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

返回 true 若此元素拥有的属性称为 name ;否则返回 false .

注意: 此函数不考虑名称空间的存在。因此,将测试指定名称 (针对包括可能存在的任何名称空间前缀的完全限定属性名)。

使用 hasAttributeNS () 以明确测试具有特定名称空间和名称的属性。

bool QDomElement:: hasAttributeNS (const QString & nsURI , const QString & localName ) const

返回 true 若此元素拥有的属性具有本地名称 localName 和名称空间 URI nsURI ;否则返回 false。

QDomNode::NodeType QDomElement:: nodeType () const

返回 ElementNode .

void QDomElement:: removeAttribute (const QString & name )

移除的属性名为 name 从此元素。

另请参阅 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 , qlonglong value )

这是重载函数。

根据当前区域设置格式化数字。

void QDomElement:: setAttribute (const QString & name , qulonglong value )

这是重载函数。

根据当前区域设置格式化数字。

void QDomElement:: setAttribute (const QString & name , int value )

这是重载函数。

根据当前区域设置格式化数字。

void QDomElement:: setAttribute (const QString & name , uint value )

这是重载函数。

根据当前区域设置格式化数字。

void QDomElement:: setAttribute (const QString & name , float value )

这是重载函数。

根据当前区域设置格式化数字。

void QDomElement:: setAttribute (const QString & name , double value )

这是重载函数。

根据当前区域设置格式化数字。

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 , int value )

这是重载函数。

void QDomElement:: setAttributeNS (const QString nsURI , const QString & qName , uint 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 , double value )

这是重载函数。

QDomAttr QDomElement:: setAttributeNode (const QDomAttr & newAttr )

添加属性 newAttr 到此元素。

若拥有另一属性的元素拥有相同名称如 newAttr ,此函数替换该属性并返回它;否则函数返回 null 属性 .

另请参阅 attributeNode (), setAttribute (),和 setAttributeNodeNS ().

QDomAttr QDomElement:: setAttributeNodeNS (const QDomAttr & newAttr )

添加属性 newAttr 到此元素。

如果拥有另一属性的元素拥有相同的本地名称和名称空间 URI 如 newAttr ,此函数替换该属性并返回它;否则函数返回 null 属性 .

另请参阅 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>
					

函数 text() 的 QDomElement <h1> 标签,将返回以下文本:

Hello Qt <xml is cool>
					

此函数忽略注释。它只评估 QDomText and QDomCDATASection 对象。

QDomElement &QDomElement:: operator= (const QDomElement & x )

赋值 x 到此 DOM (文档对象模型) 元素。

拷贝数据是共享的 (浅拷贝):修改一节点也会改变另一节点。若想要做深拷贝,使用 cloneNode ().