返回介绍

QDomElement Class

发布于 2019-10-04 14:59:46 字数 13198 浏览 1071 评论 0 收藏 0

The QDomElement class represents one element in the DOM tree. More...

#include <qdom.h>

Inherits QDomNode.

List of all member functions.

Public Members

  • QDomElement ()
  • QDomElement ( constQDomElement&x )
  • QDomElement & operator= ( constQDomElement&x )
  • ~QDomElement ()
  • QString attribute ( constQString&name, constQString&defValue = QString::null ) const
  • void setAttribute ( constQString&name, constQString&value )
  • void setAttribute ( constQString&name, intvalue )
  • void setAttribute ( constQString&name, uintvalue )
  • void setAttribute ( constQString&name, doublevalue )
  • void removeAttribute ( constQString&name )
  • QDomAttr attributeNode ( constQString&name )
  • QDomAttr setAttributeNode ( constQDomAttr&newAttr )
  • QDomAttr removeAttributeNode ( constQDomAttr&oldAttr )
  • virtual QDomNodeList elementsByTagName ( constQString&tagname ) const
  • bool hasAttribute ( constQString&name ) const
  • QString attributeNS ( constQStringnsURI, constQString&localName, constQString&defValue ) const
  • void setAttributeNS ( constQStringnsURI, constQString&qName, constQString&value )
  • void setAttributeNS ( constQStringnsURI, constQString&qName, intvalue )
  • void setAttributeNS ( constQStringnsURI, constQString&qName, uintvalue )
  • void setAttributeNS ( constQStringnsURI, constQString&qName, doublevalue )
  • void removeAttributeNS ( constQString&nsURI, constQString&localName )
  • QDomAttr attributeNodeNS ( constQString&nsURI, constQString&localName )
  • QDomAttr setAttributeNodeNS ( constQDomAttr&newAttr )
  • virtual QDomNodeList elementsByTagNameNS ( constQString&nsURI, constQString&localName ) const
  • bool hasAttributeNS ( constQString&nsURI, constQString&localName ) const
  • QString tagName () const
  • void setTagName ( constQString&name )
  • virtual QDomNamedNodeMap attributes () const
  • virtual QDomNode::NodeType nodeType () const
  • virtual bool isElement () const
  • QString text () const

Detailed Description

The QDomElement class represents one element in the DOM tree.

Elements have a tagName() and zero or more attributes associated with them. The tag name can be changed with setTagName().

Attributes of the element are represented by QDomAttr objects, that can be queried using the attribute() and attributeNode() functions. You can set attributes with the setAttribute() and setAttributeNode() functions. Attributes can be removed with removeAttribute(). There are namespace-aware equivalents to these functions, i.e. setAttributeNS(), setAttributeNodeNS() and removeAttributeNS().

If you want to access the text of a node use text(), e.g.

    QDomElement e = //...
    //...
    QString s = e.text()
    
The 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() or elementsByTagNameNS().

For further information about the Document Object Model see http://www.w3.org/TR/REC-DOM-Level-1/ and http://www.w3.org/TR/DOM-Level-2-Core/. For a more general introduction of the DOM implementation see the QDomDocument documentation.

See also XML.


Member Function Documentation

QDomElement::QDomElement ()

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

QDomElement::QDomElement ( constQDomElement&x )

Constructs a copy of x.

The data of the copy is shared (shallow copy): modifying one node will also change the other. If you want to make a deep copy, use cloneNode().

QDomElement::~QDomElement ()

Destroys the object and frees its resources.

QString QDomElement::attribute ( constQString&name, constQString&defValue = QString::null ) const

Returns the attribute called name. If the attribute does not exist defValue is returned.

See also setAttribute(), attributeNode(), setAttributeNode() and attributeNS().

QString QDomElement::attributeNS ( constQStringnsURI, constQString&localName, constQString&defValue ) const

Returns the attribute with the local name localName and the namespace URI nsURI. If the attribute does not exist defValue is returned.

See also setAttributeNS(), attributeNodeNS(), setAttributeNodeNS() and attribute().

QDomAttr QDomElement::attributeNode ( constQString&name )

Returns the QDomAttr object that corresponds to the attribute called name. If no such attribute exists a null object is returned.

See also setAttributeNode(), attribute(), setAttribute() and attributeNodeNS().

QDomAttr QDomElement::attributeNodeNS ( constQString&nsURI, constQString&localName )

Returns the QDomAttr object that corresponds to the attribute with the local name localName and the namespace URI nsURI. If no such attribute exists a null object is returned.

See also setAttributeNode(), attribute() and setAttribute().

QDomNamedNodeMap QDomElement::attributes () const [virtual]

Returns a QDomNamedNodeMap containing all this element's attributes.

See also attribute(), setAttribute(), attributeNode() and setAttributeNode().

Reimplemented from QDomNode.

QDomNodeList QDomElement::elementsByTagName ( constQString&tagname ) const [virtual]

Returns a QDomNodeList containing all descendant elements of this element called tagname. The order they are in the node list, is the order they are encountered in a preorder traversal of the element tree.

See also elementsByTagNameNS() and QDomDocument::elementsByTagName().

QDomNodeList QDomElement::elementsByTagNameNS ( constQString&nsURI, constQString&localName ) const [virtual]

Returns a QDomNodeList containing all the descendant elements of this element with the local name localName and the namespace URI nsURI. The order they are in the node list, is the order they are encountered in a preorder traversal of the element tree.

See also elementsByTagName() and QDomDocument::elementsByTagNameNS().

bool QDomElement::hasAttribute ( constQString&name ) const

Returns TRUE if this element has an attribute called name; otherwise returns FALSE.

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

Returns TRUE if this element has an attribute with the local name localName and the namespace URI nsURI; otherwise returns FALSE.

bool QDomElement::isElement () const [virtual]

Returns TRUE.

Reimplemented from QDomNode.

QDomNode::NodeType QDomElement::nodeType () const [virtual]

Returns ElementNode.

Reimplemented from QDomNode.

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

Assigns x to this DOM element.

The data of the copy is shared (shallow copy): modifying one node will also change the other. If you want to make a deep copy, use cloneNode().

void QDomElement::removeAttribute ( constQString&name )

Removes the attribute called name name from this element.

See also setAttribute(), attribute() and removeAttributeNS().

void QDomElement::removeAttributeNS ( constQString&nsURI, constQString&localName )

Removes the attribute with the local name localName and the namespace URI nsURI from this element.

See also setAttributeNS(), attributeNS() and removeAttribute().

QDomAttr QDomElement::removeAttributeNode ( constQDomAttr&oldAttr )

Removes the attribute oldAttr from the element and returns it.

See also attributeNode() and setAttributeNode().

void QDomElement::setAttribute ( constQString&name, constQString&value )

Adds an attribute called name name with value value. If an attribute with the same name exists, its value is replaced by value.

See also attribute(), setAttributeNode() and setAttributeNS().

void QDomElement::setAttribute ( constQString&name, intvalue )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

void QDomElement::setAttribute ( constQString&name, uintvalue )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

void QDomElement::setAttribute ( constQString&name, doublevalue )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

void QDomElement::setAttributeNS ( constQStringnsURI, constQString&qName, constQString&value )

Adds an attribute with the qualified name qName and the namespace URI nsURI with the value 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.

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

See also attributeNS(), setAttributeNodeNS() and setAttribute().

void QDomElement::setAttributeNS ( constQStringnsURI, constQString&qName, intvalue )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

void QDomElement::setAttributeNS ( constQStringnsURI, constQString&qName, uintvalue )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

void QDomElement::setAttributeNS ( constQStringnsURI, constQString&qName, doublevalue )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

QDomAttr QDomElement::setAttributeNode ( constQDomAttr&newAttr )

Adds the attribute newAttr to this element.

If the element has another attribute that has the same name as newAttr, this function replaces that attribute and returns it; otherwise the function returns a null attribute.

See also attributeNode(), setAttribute() and setAttributeNodeNS().

QDomAttr QDomElement::setAttributeNodeNS ( constQDomAttr&newAttr )

Adds the attribute newAttr to this element.

If the element has another attribute that has the same local name and namespace URI as newAttr, this function replaces that attribute and returns it; otherwise the function returns a null attribute.

See also attributeNodeNS(), setAttributeNS() and setAttributeNode().

void QDomElement::setTagName ( constQString&name )

Sets the tag name of this element to name.

See also tagName().

QString QDomElement::tagName () const

Returns the tag name of this element. For an XML element like
  <img src="myimg.png">
  
the tagname would return "img".

See also setTagName().

QString QDomElement::text () const

Returns the text contained inside this element.

Example:

  <h1>Hello <b>Qt</b> <![CDATA[<xml is cool>]]></h1>
  

The function text() of the QDomElement for the <h1> tag, will return "Hello Qt <xml is cool>".

Comments are ignored by this function. It only evaluates QDomText and QDomCDATASection objects.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文