PySide6.QtXml.QDomNode¶
- class QDomNode¶
The
QDomNode
class is the base class for all the nodes in a DOM tree. More…Inherited by:
QDomProcessingInstruction
,QDomNotation
,QDomEntityReference
,QDomEntity
,QDomElement
,QDomDocumentType
,QDomDocumentFragment
,QDomDocument
,QDomCharacterData
,QDomText
,QDomComment
,QDomCDATASection
,QDomAttr
Synopsis¶
Methods¶
def
__init__()
def
appendChild()
def
attributes()
def
childNodes()
def
clear()
def
cloneNode()
def
columnNumber()
def
firstChild()
def
hasAttributes()
def
hasChildNodes()
def
insertAfter()
def
insertBefore()
def
isAttr()
def
isCDATASection()
def
isComment()
def
isDocument()
def
isDocumentType()
def
isElement()
def
isEntity()
def
isNotation()
def
isNull()
def
isSupported()
def
isText()
def
lastChild()
def
lineNumber()
def
localName()
def
namedItem()
def
namespaceURI()
def
nextSibling()
def
nodeName()
def
nodeType()
def
nodeValue()
def
normalize()
def
__ne__()
def
__eq__()
def
ownerDocument()
def
parentNode()
def
prefix()
def
removeChild()
def
replaceChild()
def
save()
def
setNodeValue()
def
setPrefix()
def
toAttr()
def
toCDATASection()
def
toComment()
def
toDocument()
def
toDocumentType()
def
toElement()
def
toEntity()
def
toNotation()
def
toText()
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
Detailed Description¶
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Many functions in the DOM return a
QDomNode
.You can find out the type of a node using
isAttr()
,isCDATASection()
,isDocumentFragment()
,isDocument()
,isDocumentType()
,isElement()
,isEntityReference()
,isText()
,isEntity()
,isNotation()
,isProcessingInstruction()
,isCharacterData()
andisComment()
.A
QDomNode
can be converted into one of its subclasses usingtoAttr()
,toCDATASection()
,toDocumentFragment()
,toDocument()
,toDocumentType()
,toElement()
,toEntityReference()
,toText()
,toEntity()
,toNotation()
,toProcessingInstruction()
,toCharacterData()
ortoComment()
. You can convert a node to a null node withclear()
.Copies of the
QDomNode
class share their data using explicit sharing. This means that modifying one node will change all copies. This is especially useful in combination with functions which return aQDomNode
, e.g.firstChild()
. You can make an independent (deep) copy of the node withcloneNode()
.A
QDomNode
can be null, much likeNone
. Creating a copy of a null node results in another null node. It is not possible to modify a null node, but it is possible to assign another, possibly non-null node to it. In this case, the copy of the null node will remain null. You can check if aQDomNode
is null by callingisNull()
. The empty constructor of aQDomNode
(or any of the derived classes) creates a null node.Nodes are inserted with
insertBefore()
,insertAfter()
orappendChild()
. You can replace one node with another usingreplaceChild()
and remove a node withremoveChild()
.To traverse nodes use
firstChild()
to get a node’s first child (if any), andnextSibling()
to traverse.QDomNode
also provideslastChild()
,previousSibling()
andparentNode()
. To find the first child node with a particular node name usenamedItem()
.To find out if a node has children use
hasChildNodes()
and to get a list of all of a node’s children usechildNodes()
.The node’s name and value (the meaning of which varies depending on its type) is returned by
nodeName()
andnodeValue()
respectively. The node’s type is returned bynodeType()
. The node’s value can be set withsetNodeValue()
.The document to which the node belongs is returned by
ownerDocument()
.Adjacent
QDomText
nodes can be merged into a single node withnormalize()
.QDomElement
nodes have attributes which can be retrieved withattributes()
.QDomElement
andQDomAttr
nodes can have namespaces which can be retrieved withnamespaceURI()
. Their local name is retrieved withlocalName()
, and their prefix withprefix()
. The prefix can be set withsetPrefix()
.You can write the XML representation of the node to a text stream with
save()
.The following example looks for the first element in an XML document and prints the names of all the elements that are its direct children.
d = QDomDocument() someXML = QString() d.setContent(someXML) n = d.firstChild() while not n.isNull(): if n.isElement(): e = n.toElement() print("Element name: ", qPrintable(e.tagName()), '\n') break n = n.nextSibling()
For further information about the Document Object Model see Level 1 and Level 2 Core . For a more general introduction of the DOM implementation see the
QDomDocument
documentation.- class NodeType¶
This enum defines the type of the node:
Constant
Description
QDomNode.ElementNode
QDomNode.AttributeNode
QDomNode.TextNode
QDomNode.CDATASectionNode
QDomNode.EntityReferenceNode
QDomNode.EntityNode
QDomNode.ProcessingInstructionNode
QDomNode.CommentNode
QDomNode.DocumentNode
QDomNode.DocumentTypeNode
QDomNode.DocumentFragmentNode
QDomNode.NotationNode
QDomNode.BaseNode
QDomNode.CharacterDataNode
- class EncodingPolicy¶
This enum specifies how
save()
determines what encoding to use when serializing.Constant
Description
QDomNode.EncodingFromDocument
The encoding is fetched from the document.
QDomNode.EncodingFromTextStream
The encoding is fetched from the QTextStream.
See also
- __init__()¶
Constructs a
null
node.- __init__(node)
- Parameters:
node –
QDomNode
Constructs a copy of
node
.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()
.Appends
newChild
as the node’s last child.If
newChild
is the child of another node, it is reparented to this node. IfnewChild
is a child of this node, then its position in the list of children is changed.If
newChild
is aQDomDocumentFragment
, then the children of the fragment are removed from the fragment and appended.If
newChild
is aQDomElement
and this node is aQDomDocument
that already has an element node as a child,newChild
is not added as a child and a null node is returned.Returns a new reference to
newChild
on success or anull node
on failure.Calling this function on a null node(created, for example, with the default constructor) does nothing and returns a
null node
.The DOM specification disallow inserting attribute nodes, but for historical reasons, QDom accepts them anyway.
- attributes()¶
- Return type:
Returns a named node map of all attributes. Attributes are only provided for
QDomElement
s.Changing the attributes in the map will also change the attributes of this
QDomNode
.- childNodes()¶
- Return type:
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Returns a list of all direct child nodes.
Most often you will call this function on a
QDomElement
object.For example, if the XML document looks like this:
<body> <h1>Heading</h1> <p>Hello <b>you</b></p> </body>
Then the list of child nodes for the “body”-element will contain the node created by the <h1> tag and the node created by the <p> tag.
The nodes in the list are not copied; so changing the nodes in the list will also change the children of this node.
See also
- clear()¶
Converts the node into a null node; if it was not a null node before, its type and contents are deleted.
See also
Creates a deep (not shallow) copy of the
QDomNode
.If
deep
is true, then the cloning is done recursively which means that all the node’s children are deep copied too. Ifdeep
is false only the node itself is copied and the copy will have no child nodes.- columnNumber()¶
- Return type:
int
For nodes created by
setContent()
, this function returns the column number in the XML document where the node was parsed. Otherwise, -1 is returned.See also
Returns the first child of the node. If there is no child node, a
null node
is returned. Changing the returned node will also change the node in the document tree.See also
- firstChildElement([tagName=""[, namespaceURI=""]])¶
- Parameters:
tagName – str
namespaceURI – str
- Return type:
Returns the first child element with tag name
tagName
and namespace URInamespaceURI
. IftagName
is empty, returns the first child element withnamespaceURI
, and ifnamespaceURI
is empty, returns the first child element withtagName
. If the both parameters are empty, returns the first child element. Returns a null element if no such child exists.- hasAttributes()¶
- Return type:
bool
Returns
true
if the node has attributes; otherwise returnsfalse
.See also
- hasChildNodes()¶
- Return type:
bool
Returns
true
if the node has one or more children; otherwise returnsfalse
.- insertAfter(newChild, refChild)¶
Inserts the node
newChild
after the child noderefChild
.refChild
must be a direct child of this node. IfrefChild
isnull
thennewChild
is appended as this node’s last child.If
newChild
is the child of another node, it is reparented to this node. IfnewChild
is a child of this node, then its position in the list of children is changed.If
newChild
is aQDomDocumentFragment
, then the children of the fragment are removed from the fragment and inserted afterrefChild
.Returns a new reference to
newChild
on success or anull node
on failure.The DOM specification disallow inserting attribute nodes, but due to historical reasons QDom accept them nevertheless.
- insertBefore(newChild, refChild)¶
Inserts the node
newChild
before the child noderefChild
.refChild
must be a direct child of this node. IfrefChild
isnull
thennewChild
is inserted as the node’s first child.If
newChild
is the child of another node, it is reparented to this node. IfnewChild
is a child of this node, then its position in the list of children is changed.If
newChild
is aQDomDocumentFragment
, then the children of the fragment are removed from the fragment and inserted beforerefChild
.Returns a new reference to
newChild
on success or anull node
on failure.The DOM specification disallow inserting attribute nodes, but due to historical reasons QDom accept them nevertheless.
- isAttr()¶
- Return type:
bool
Returns
true
if the node is an attribute; otherwise returnsfalse
.If this function returns
true
, it does not imply that this object is a QDomAttribute; you can get the QDomAttribute with toAttribute().See also
- isCDATASection()¶
- Return type:
bool
Returns
true
if the node is a CDATA section; otherwise returns false.If this function returns
true
, it does not imply that this object is aQDomCDATASection
; you can get theQDomCDATASection
withtoCDATASection()
.See also
- isCharacterData()¶
- Return type:
bool
Returns
true
if the node is a character data node; otherwise returnsfalse
.If this function returns
true
, it does not imply that this object is aQDomCharacterData
; you can get theQDomCharacterData
withtoCharacterData()
.See also
- isComment()¶
- Return type:
bool
Returns
true
if the node is a comment; otherwise returnsfalse
.If this function returns
true
, it does not imply that this object is aQDomComment
; you can get theQDomComment
withtoComment()
.See also
- isDocument()¶
- Return type:
bool
Returns
true
if the node is a document; otherwise returnsfalse
.If this function returns
true
, it does not imply that this object is aQDomDocument
; you can get theQDomDocument
withtoDocument()
.See also
- isDocumentFragment()¶
- Return type:
bool
Returns
true
if the node is a document fragment; otherwise returns false.If this function returns
true
, it does not imply that this object is aQDomDocumentFragment
; you can get theQDomDocumentFragment
withtoDocumentFragment()
.See also
- isDocumentType()¶
- Return type:
bool
Returns
true
if the node is a document type; otherwise returns false.If this function returns
true
, it does not imply that this object is aQDomDocumentType
; you can get theQDomDocumentType
withtoDocumentType()
.See also
- isElement()¶
- Return type:
bool
Returns
true
if the node is an element; otherwise returnsfalse
.If this function returns
true
, it does not imply that this object is aQDomElement
; you can get theQDomElement
withtoElement()
.See also
- isEntity()¶
- Return type:
bool
Returns
true
if the node is an entity; otherwise returnsfalse
.If this function returns
true
, it does not imply that this object is aQDomEntity
; you can get theQDomEntity
withtoEntity()
.See also
- isEntityReference()¶
- Return type:
bool
Returns
true
if the node is an entity reference; otherwise returns false.If this function returns
true
, it does not imply that this object is aQDomEntityReference
; you can get theQDomEntityReference
withtoEntityReference()
.See also
- isNotation()¶
- Return type:
bool
Returns
true
if the node is a notation; otherwise returnsfalse
.If this function returns
true
, it does not imply that this object is aQDomNotation
; you can get theQDomNotation
withtoNotation()
.See also
- isNull()¶
- Return type:
bool
Returns
true
if this node is null (i.e. if it has no type or contents); otherwise returnsfalse
.- isProcessingInstruction()¶
- Return type:
bool
Returns
true
if the node is a processing instruction; otherwise returnsfalse
.If this function returns
true
, it does not imply that this object is aQDomProcessingInstruction
; you can get the QProcessingInstruction withtoProcessingInstruction()
.See also
- isSupported(feature, version)¶
- Parameters:
feature – str
version – str
- Return type:
bool
Returns
true
if the DOM implementation implements the featurefeature
and this feature is supported by this node in the versionversion
; otherwise returnsfalse
.See also
- isText()¶
- Return type:
bool
Returns
true
if the node is a text node; otherwise returnsfalse
.If this function returns
true
, it does not imply that this object is aQDomText
; you can get theQDomText
withtoText()
.See also
Returns the last child of the node. If there is no child node, a
null node
is returned. Changing the returned node will also change the node in the document tree.See also
- lastChildElement([tagName=""[, namespaceURI=""]])¶
- Parameters:
tagName – str
namespaceURI – str
- Return type:
Returns the last child element with tag name
tagName
and namespace URInamespaceURI
. IftagName
is empty, returns the last child element withnamespaceURI
, and ifnamespaceURI
is empty, returns the last child element withtagName
. If the both parameters are empty, returns the last child element. Returns a null element if no such child exists.- lineNumber()¶
- Return type:
int
For nodes created by
setContent()
, this function returns the line number in the XML document where the node was parsed. Otherwise, -1 is returned.See also
- localName()¶
- Return type:
str
If the node uses namespaces, this function returns the local name of the node; otherwise it returns an empty string.
Only nodes of type
ElementNode
orAttributeNode
can have namespaces. A namespace must have been specified at creation time; it is not possible to add a namespace afterwards.Returns the first direct child node for which
nodeName()
equalsname
.If no such direct child exists, a
null node
is returned.See also
- namespaceURI()¶
- Return type:
str
Returns the namespace URI of this node or an empty string if the node has no namespace URI.
Only nodes of type
ElementNode
orAttributeNode
can have namespaces. A namespace URI must be specified at creation time and cannot be changed later.Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Returns the next sibling in the document tree. Changing the returned node will also change the node in the document tree.
If you have XML like this:
<h1>Heading</h1> <p>The text...</p> <h2>Next heading</h2>
and this
QDomNode
represents the <p> tag, nextSibling() will return the node representing the <h2> tag.See also
- nextSiblingElement([taName=""[, namespaceURI=""]])¶
- Parameters:
taName – str
namespaceURI – str
- Return type:
Returns the next sibling element with tag name
tagName
and namespace URInamespaceURI
. IftagName
is empty, returns the next sibling element withnamespaceURI
, and ifnamespaceURI
is empty, returns the next sibling child element withtagName
. If the both parameters are empty, returns the next sibling element. Returns a null element if no such sibling exists.- nodeName()¶
- Return type:
str
Returns the name of the node.
The meaning of the name depends on the subclass:
Name
Meaning
The name of the attribute
The string “#cdata-section”
The string “#comment”
The string “#document”
The string “#document-fragment”
The name of the document type
The tag name
The name of the entity
The name of the referenced entity
The name of the notation
The target of the processing instruction
The string “#text”
Note
This function does not take the presence of namespaces into account when processing the names of element and attribute nodes. As a result, the returned name can contain any namespace prefix that may be present. To obtain the node name of an element or attribute, use
localName()
; to obtain the namespace prefix, usenamespaceURI()
.See also
Returns the type of the node.
- nodeValue()¶
- Return type:
str
Returns the value of the node.
The meaning of the value depends on the subclass:
Name
Meaning
The attribute value
The content of the CDATA section
The comment
The data of the processing instruction
The text
All the other subclasses do not have a node value and will return an empty string.
See also
- normalize()¶
Calling normalize() on an element converts all its children into a standard form. This means that adjacent
QDomText
objects will be merged into a single text object (QDomCDATASection
nodes are not merged).Returns
true
ifother
and this DOM node are not equal; otherwise returnsfalse
.Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Returns
true
ifother
and this DOM node are equal; otherwise returnsfalse
.Any instance of
QDomNode
acts as a reference to an underlying data structure inQDomDocument
. The test for equality checks if the two references point to the same underlying node. For example:document = QDomDocument() element1 = document.documentElement() element2 = element1
The two nodes (
QDomElement
is aQDomNode
subclass) both refer to the document’s root element, andelement1 == element2
will return true. On the other hand:element3 = document.createElement("MyElement") element4 = document.createElement("MyElement")
Even though both nodes are empty elements carrying the same name,
element3 == element4
will return false because they refer to two different nodes in the underlying data structure.- ownerDocument()¶
- Return type:
Returns the document to which this node belongs.
Returns the parent node. If this node has no parent, a null node is returned (i.e. a node for which
isNull()
returnstrue
).- prefix()¶
- Return type:
str
Returns the namespace prefix of the node or an empty string if the node has no namespace prefix.
Only nodes of type
ElementNode
orAttributeNode
can have namespaces. A namespace prefix must be specified at creation time. If a node was created with a namespace prefix, you can change it later withsetPrefix()
.If you create an element or attribute with
createElement()
orcreateAttribute()
, the prefix will be an empty string. If you usecreateElementNS()
orcreateAttributeNS()
instead, the prefix will not be an empty string; but it might be an empty string if the name does not have a prefix.Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Returns the previous sibling in the document tree. Changing the returned node will also change the node in the document tree.
For example, if you have XML like this:
<h1>Heading</h1> <p>The text...</p> <h2>Next heading</h2>
and this
QDomNode
represents the <p> tag, previousSibling() will return the node representing the <h1> tag.See also
- previousSiblingElement([tagName=""[, namespaceURI=""]])¶
- Parameters:
tagName – str
namespaceURI – str
- Return type:
Returns the previous sibling element with tag name
tagName
and namespace URInamespaceURI
. IftagName
is empty, returns the previous sibling element withnamespaceURI
, and ifnamespaceURI
is empty, returns the previous sibling element withtagName
. If the both parameters are empty, returns the previous sibling element. Returns a null element if no such sibling exists.Removes
oldChild
from the list of children.oldChild
must be a direct child of this node.Returns a new reference to
oldChild
on success or anull node
on failure.- replaceChild(newChild, oldChild)¶
Replaces
oldChild
withnewChild
.oldChild
must be a direct child of this node.If
newChild
is the child of another node, it is reparented to this node. IfnewChild
is a child of this node, then its position in the list of children is changed.If
newChild
is aQDomDocumentFragment
, thenoldChild
is replaced by all of the children of the fragment.Returns a new reference to
oldChild
on success or anull node
on failure.- save(arg__1, arg__2[, arg__3=QDomNode.EncodingFromDocument])¶
- Parameters:
arg__1 –
QTextStream
arg__2 – int
arg__3 –
EncodingPolicy
Writes the XML representation of the node and all its children to the stream
stream
. This function usesindent
as the amount of space to indent the node.If the document contains invalid XML characters or characters that cannot be encoded in the given encoding, the result and behavior is undefined.
If
encodingPolicy
isEncodingFromDocument
and this node is a document node, the encoding of text streamstream
's encoding is set by treating a processing instruction by name “xml” as an XML declaration, if one exists, and otherwise defaults to UTF-8. XML declarations are not processing instructions, but this behavior exists for historical reasons. If this node is not a document node, the text stream’s encoding is used.If
encodingPolicy
isEncodingFromTextStream
and this node is a document node, this function behaves as save(QTextStream &str, int indent) with the exception that the encoding specified in the text streamstream
is used.If the document contains invalid XML characters or characters that cannot be encoded in the given encoding, the result and behavior is undefined.
- setNodeValue(value)¶
- Parameters:
value – str
Sets the node’s value to
value
.See also
- setPrefix(pre)¶
- Parameters:
pre – str
If the node has a namespace prefix, this function changes the namespace prefix of the node to
pre
. Otherwise this function does nothing.Only nodes of type
ElementNode
orAttributeNode
can have namespaces. A namespace prefix must have be specified at creation time; it is not possible to add a namespace prefix afterwards.Converts a
QDomNode
into aQDomAttr
. If the node is not an attribute, the returned object will benull
.See also
- toCDATASection()¶
- Return type:
Converts a
QDomNode
into aQDomCDATASection
. If the node is not a CDATA section, the returned object will benull
.See also
- toCharacterData()¶
- Return type:
Converts a
QDomNode
into aQDomCharacterData
. If the node is not a character data node the returned object will benull
.See also
- toComment()¶
- Return type:
Converts a
QDomNode
into aQDomComment
. If the node is not a comment the returned object will benull
.See also
- toDocument()¶
- Return type:
Converts a
QDomNode
into aQDomDocument
. If the node is not a document the returned object will benull
.See also
- toDocumentFragment()¶
- Return type:
Converts a
QDomNode
into aQDomDocumentFragment
. If the node is not a document fragment the returned object will benull
.See also
- toDocumentType()¶
- Return type:
Converts a
QDomNode
into aQDomDocumentType
. If the node is not a document type the returned object will benull
.See also
- toElement()¶
- Return type:
Converts a
QDomNode
into aQDomElement
. If the node is not an element the returned object will benull
.See also
- toEntity()¶
- Return type:
Converts a
QDomNode
into aQDomEntity
. If the node is not an entity the returned object will benull
.See also
- toEntityReference()¶
- Return type:
Converts a
QDomNode
into aQDomEntityReference
. If the node is not an entity reference, the returned object will benull
.See also
- toNotation()¶
- Return type:
Converts a
QDomNode
into aQDomNotation
. If the node is not a notation the returned object will benull
.See also
- toProcessingInstruction()¶
- Return type:
Converts a
QDomNode
into aQDomProcessingInstruction
. If the node is not a processing instruction the returned object will benull
.See also
Converts a
QDomNode
into aQDomText
. If the node is not a text, the returned object will benull
.See also