QDomElement 类

QDomElement class represents one element in the DOM tree. 更多...

头: #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 )

详细描述

QDomElement class represents one element in the DOM tree.

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

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

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

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

text () function operates recursively to find the text (since not all elements contain text). If you want to find all the text in all of a node's children, iterate over the children looking for QDomText nodes, e.g.

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();
}
					

Note that we attempt to convert each node to a text node and use text () rather than using firstChild (). toText ().data() or n. toText ().data() directly on the node, because the node may not be a text element.

You can get a list of all the decendents of an element which have a specified tag name with elementsByTagName () 或 elementsByTagNameNS ().

To browse the elements of a dom document use firstChildElement (), lastChildElement (), nextSiblingElement () 和 previousSiblingElement (). For example, to iterate over all child elements called "entry" in a root element called "database", you can use:

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 ()

Constructs an empty element. Use the QDomDocument::createElement () function to construct elements with content.

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 and namespace 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 )

这是重载函数。

The number is formatted according to the current locale.

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

这是重载函数。

The number is formatted according to the current locale.

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

这是重载函数。

The number is formatted according to the current locale.

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

这是重载函数。

The number is formatted according to the current locale.

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

这是重载函数。

The number is formatted according to the current locale.

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

这是重载函数。

The number is formatted according to the current locale.

void QDomElement:: setAttributeNS (const QString nsURI , const QString & qName , const QString & value )

Adds an attribute with the qualified name qName 和名称空间 URI nsURI 采用值 value . If an attribute with the same local name and namespace URI exists, its prefix is replaced by the prefix of qName and its value is repaced by value .

尽管 qName is the qualified name, the local name is used to decide if an existing attribute's value should be replaced.

另请参阅 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 to this DOM element.

拷贝数据是共享的 (浅拷贝):修改一个节点也将改变另一节点。若想要制作深度副本,使用 cloneNode ().