QDomElement 類

QDomElement 類錶示 DOM (文檔對象模型) 樹中一元素。 更多...

頭: #include <QDomElement>
qmake: QT += xml
繼承: QDomNode

注意: 此類的所有函數 可重入 .

公共函數

QDomElement (const QDomElement & x )
QDomElement ()
QDomElement & operator= (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

詳細描述

元素擁有 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 (const QDomElement & x )

構造副本為 x .

拷貝數據是共享的 (淺拷貝):修改一節點也會改變另一節點。若想要做深拷貝,使用 cloneNode ().

QDomElement:: QDomElement ()

構造空元素。使用 QDomDocument::createElement () 函數以構造具有內容的元素。

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

賦值 x 到此 DOM (文檔對象模型) 元素。

拷貝數據是共享的 (淺拷貝):修改一節點也會改變另一節點。若想要做深拷貝,使用 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 )

這是重載函數。

格式始終使用 QLocale::C .

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

這是重載函數。

格式始終使用 QLocale::C .

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

這是重載函數。

格式始終使用 QLocale::C .

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

這是重載函數。

格式始終使用 QLocale::C .

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

這是重載函數。

格式始終使用 QLocale::C .

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

這是重載函數。

格式始終使用 QLocale::C .

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

添加屬性具有閤格名稱 qName 和名稱空間 URI nsURI 采用值 value 。若存在具有相同本地名稱和名稱空間 URI (統一資源標識符) 的屬性,會替換其前綴通過前綴對於 qName and its value is replaced by 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 對象。