QDomDocument¶
The
QDomDocument
class represents an XML document. More…
Synopsis¶
Functions¶
def
createAttribute
(name)def
createAttributeNS
(nsURI, qName)def
createCDATASection
(data)def
createComment
(data)def
createDocumentFragment
()def
createElement
(tagName)def
createElementNS
(nsURI, qName)def
createEntityReference
(name)def
createProcessingInstruction
(target, data)def
createTextNode
(data)def
doctype
()def
documentElement
()def
elementById
(elementId)def
elementsByTagName
(tagname)def
elementsByTagNameNS
(nsURI, localName)def
implementation
()def
importNode
(importedNode, deep)def
setContent
(dev)def
setContent
(dev, namespaceProcessing)def
setContent
(reader, namespaceProcessing[, errorMsg=None[, errorLine=None[, errorColumn=None]]])def
setContent
(source, namespaceProcessing)def
setContent
(source, reader)def
setContent
(text)def
setContent
(text)def
setContent
(text, namespaceProcessing)def
setContent
(text, namespaceProcessing)def
toByteArray
([arg__1=1])def
toString
([arg__1=1])
Detailed Description¶
The
QDomDocument
class represents the entire XML document. Conceptually, it is the root of the document tree, and provides the primary access to the document’s data.Since elements, text nodes, comments, processing instructions, etc., cannot exist outside the context of a document, the document class also contains the factory functions needed to create these objects. The node objects created have an
ownerDocument()
function which associates them with the document within whose context they were created. The DOM classes that will be used most often areQDomNode
,QDomDocument
,QDomElement
andQDomText
.The parsed XML is represented internally by a tree of objects that can be accessed using the various QDom classes. All QDom classes only reference objects in the internal tree. The internal objects in the DOM tree will get deleted once the last QDom object referencing them or the
QDomDocument
itself is deleted.Creation of elements, text nodes, etc. is done using the various factory functions provided in this class. Using the default constructors of the QDom classes will only result in empty objects that cannot be manipulated or inserted into the Document.
The
QDomDocument
class has several functions for creating document data, for example,createElement()
,createTextNode()
,createComment()
,createCDATASection()
,createProcessingInstruction()
,createAttribute()
andcreateEntityReference()
. Some of these functions have versions that support namespaces, i.e.createElementNS()
andcreateAttributeNS()
. ThecreateDocumentFragment()
function is used to hold parts of the document; this is useful for manipulating for complex documents.The entire content of the document is set with
setContent()
. This function parses the string it is passed as an XML document and creates the DOM tree that represents the document. The root element is available usingdocumentElement()
. The textual representation of the document can be obtained usingtoString()
.Note
The DOM tree might end up reserving a lot of memory if the XML document is big. For such documents, the
QXmlStreamReader
or theQXmlQuery
classes might be better solutions.It is possible to insert a node from another document into the document using
importNode()
.You can obtain a list of all the elements that have a particular tag with
elementsByTagName()
or withelementsByTagNameNS()
.The QDom classes are typically used as follows:
doc = QDomDocument("mydocument") file = QFile("mydocument.xml") if not file.open(QIODevice::ReadOnly): return if not doc.setContent(&file): file.close() return file.close() # print out the element names of all elements that are direct children # of the outermost element. docElem = doc.documentElement() n = docElem.firstChild() while not n.isNull(): e = n.toElement() # try to convert the node to an element. if not e.isNull(): print e.tagName() # the node really is an element. n = n.nextSibling() } # Here we append a new element to the end of the document elem = doc.createElement("img") elem.setAttribute("src", "myimage.png") docElem.appendChild(elem)Once
doc
andelem
go out of scope, the whole internal tree representing the XML document is deleted.To create a document using DOM use code like this:
doc = QDomDocument("MyML") root = doc.createElement("MyML") doc.appendChild(root) tag = doc.createElement("Greeting") root.appendChild(tag) t = doc.createTextNode("Hello World") tag.appendChild(t) xml = doc.toString()For further information about the Document Object Model see the Document Object Model (DOM) Level 1 and Level 2 Core Specifications.
- class PySide2.QtXml.QDomDocument¶
PySide2.QtXml.QDomDocument(x)
PySide2.QtXml.QDomDocument(doctype)
PySide2.QtXml.QDomDocument(name)
- param x:
- param doctype:
- param name:
str
Constructs an empty document.
- PySide2.QtXml.QDomDocument.createAttribute(name)¶
- Parameters:
name – str
- Return type:
Creates a new attribute called
name
that can be inserted into an element, e.g. usingsetAttributeNode()
.If
name
is not a valid XML name, the behavior of this function is governed byInvalidDataPolicy
.See also
- PySide2.QtXml.QDomDocument.createAttributeNS(nsURI, qName)¶
- Parameters:
nsURI – str
qName – str
- Return type:
Creates a new attribute with namespace support that can be inserted into an element. The name of the attribute is
qName
and the namespace URI isnsURI
. This function also setsprefix()
andlocalName()
to appropriate values (depending onqName
).If
qName
is not a valid XML name, the behavior of this function is governed byInvalidDataPolicy
.See also
- PySide2.QtXml.QDomDocument.createCDATASection(data)¶
- Parameters:
data – str
- Return type:
Creates a new CDATA section for the string
value
that can be inserted into the document, e.g. usingappendChild()
.If
value
contains characters which cannot be stored in a CDATA section, the behavior of this function is governed byInvalidDataPolicy
.See also
- PySide2.QtXml.QDomDocument.createComment(data)¶
- Parameters:
data – str
- Return type:
Creates a new comment for the string
value
that can be inserted into the document, e.g. usingappendChild()
.If
value
contains characters which cannot be stored in an XML comment, the behavior of this function is governed byInvalidDataPolicy
.See also
- PySide2.QtXml.QDomDocument.createDocumentFragment()¶
- Return type:
Creates a new document fragment, that can be used to hold parts of the document, e.g. when doing complex manipulations of the document tree.
- PySide2.QtXml.QDomDocument.createElement(tagName)¶
- Parameters:
tagName – str
- Return type:
Creates a new element called
tagName
that can be inserted into the DOM tree, e.g. usingappendChild()
.If
tagName
is not a valid XML name, the behavior of this function is governed byInvalidDataPolicy
.
- PySide2.QtXml.QDomDocument.createElementNS(nsURI, qName)¶
- Parameters:
nsURI – str
qName – str
- Return type:
Creates a new element with namespace support that can be inserted into the DOM tree. The name of the element is
qName
and the namespace URI isnsURI
. This function also setsprefix()
andlocalName()
to appropriate values (depending onqName
).If
qName
is an empty string, returns a null element regardless of whether the invalid data policy is set.See also
- PySide2.QtXml.QDomDocument.createEntityReference(name)¶
- Parameters:
name – str
- Return type:
Creates a new entity reference called
name
that can be inserted into the document, e.g. usingappendChild()
.If
name
is not a valid XML name, the behavior of this function is governed byInvalidDataPolicy
.See also
- PySide2.QtXml.QDomDocument.createProcessingInstruction(target, data)¶
- Parameters:
target – str
data – str
- Return type:
Creates a new processing instruction that can be inserted into the document, e.g. using
appendChild()
. This function sets the target for the processing instruction totarget
and the data todata
.If
target
is not a valid XML name, or data if contains characters which cannot appear in a processing instruction, the behavior of this function is governed byInvalidDataPolicy
.See also
- PySide2.QtXml.QDomDocument.createTextNode(data)¶
- Parameters:
data – str
- Return type:
Creates a text node for the string
value
that can be inserted into the document tree, e.g. usingappendChild()
.If
value
contains characters which cannot be stored as character data of an XML document (even in the form of character references), the behavior of this function is governed byInvalidDataPolicy
.See also
- PySide2.QtXml.QDomDocument.doctype()¶
- Return type:
Returns the document type of this document.
- PySide2.QtXml.QDomDocument.documentElement()¶
- Return type:
Returns the root element of the document.
- PySide2.QtXml.QDomDocument.elementById(elementId)¶
- Parameters:
elementId – str
- Return type:
Returns the element whose ID is equal to
elementId
. If no element with the ID was found, this function returns anull element
.Since the QDomClasses do not know which attributes are element IDs, this function returns always a
null element
. This may change in a future version.
- PySide2.QtXml.QDomDocument.elementsByTagName(tagname)¶
- Parameters:
tagname – str
- Return type:
Returns a
QDomNodeList
, that contains all the elements in the document with the nametagname
. The order of the node list is the order they are encountered in a preorder traversal of the element tree.See also
- PySide2.QtXml.QDomDocument.elementsByTagNameNS(nsURI, localName)¶
- Parameters:
nsURI – str
localName – str
- Return type:
Returns a
QDomNodeList
that contains all the elements in the document with the local namelocalName
and a namespace URI ofnsURI
. The order of the node list is the order they are encountered in a preorder traversal of the element tree.See also
- PySide2.QtXml.QDomDocument.implementation()¶
- Return type:
Returns a
QDomImplementation
object.
- PySide2.QtXml.QDomDocument.importNode(importedNode, deep)¶
- Parameters:
importedNode –
PySide2.QtXml.QDomNode
deep – bool
- Return type:
Imports the node
importedNode
from another document to this document.importedNode
remains in the original document; this function creates a copy that can be used within this document.This function returns the imported node that belongs to this document. The returned node has no parent. It is not possible to import
QDomDocument
andQDomDocumentType
nodes. In those cases this function returns anull node
.If
importedNode
is anull node
, a null node is returned.If
deep
is true, this function imports not only the nodeimportedNode
but its whole subtree; if it is false, only theimportedNode
is imported. The argumentdeep
has no effect onQDomAttr
andQDomEntityReference
nodes, since the descendants ofQDomAttr
nodes are always imported and those ofQDomEntityReference
nodes are never imported.The behavior of this function is slightly different depending on the node types:
Node Type
Behavior
The owner element is set to 0 and the specified flag is set to true in the generated attribute. The whole subtree of
importedNode
is always imported for attribute nodes:deep
has no effect.Document nodes cannot be imported.
If
deep
is true, this function imports the whole document fragment; otherwise it only generates an empty document fragment.Document type nodes cannot be imported.
Attributes for which
specified()
is true are also imported, other attributes are not imported. Ifdeep
is true, this function also imports the subtree ofimportedNode
; otherwise it imports only the element node (and some attributes, see above).Entity nodes can be imported, but at the moment there is no way to use them since the document type is read-only in DOM level 2.
Descendants of entity reference nodes are never imported:
deep
has no effect.Notation nodes can be imported, but at the moment there is no way to use them since the document type is read-only in DOM level 2.
The target and value of the processing instruction is copied to the new node.
The text is copied to the new node.
The text is copied to the new node.
The text is copied to the new node.
- PySide2.QtXml.QDomDocument.setContent(text, namespaceProcessing)¶
- Parameters:
text – str
namespaceProcessing – bool
- Return type:
(retval, errorMsg, errorLine, errorColumn)
- PySide2.QtXml.QDomDocument.setContent(text)
- Parameters:
text – str
- Return type:
(retval, errorMsg, errorLine, errorColumn)
- PySide2.QtXml.QDomDocument.setContent(source, reader)
- Parameters:
source –
PySide2.QtXml.QXmlInputSource
reader –
PySide2.QtXml.QXmlReader
- Return type:
(retval, errorMsg, errorLine, errorColumn)
Note
This function is deprecated.
This is an overloaded function.
This function reads the XML document from the
QXmlInputSource
source
and parses it with theQXmlReader
reader
, returning true if the content was successfully parsed; otherwise returnsfalse
.This function doesn’t change the features of the
reader
. If you want to use certain features for parsing you can use this function to set up the reader appropriately.See also
- PySide2.QtXml.QDomDocument.setContent(text, namespaceProcessing)
- Parameters:
text –
PySide2.QtCore.QByteArray
namespaceProcessing – bool
- Return type:
(retval, errorMsg, errorLine, errorColumn)
- PySide2.QtXml.QDomDocument.setContent(text)
- Parameters:
text –
PySide2.QtCore.QByteArray
- Return type:
(retval, errorMsg, errorLine, errorColumn)
- PySide2.QtXml.QDomDocument.setContent(source, namespaceProcessing)
- Parameters:
source –
PySide2.QtXml.QXmlInputSource
namespaceProcessing – bool
- Return type:
(retval, errorMsg, errorLine, errorColumn)
Note
This function is deprecated.
This is an overloaded function.
This function reads the XML document from the
QXmlInputSource
source
, returning true if the content was successfully parsed; otherwise returnsfalse
.
- PySide2.QtXml.QDomDocument.setContent(dev, namespaceProcessing)
- Parameters:
dev –
PySide2.QtCore.QIODevice
namespaceProcessing – bool
- Return type:
(retval, errorMsg, errorLine, errorColumn)
This is an overloaded function.
This function reads the XML document from the IO device
dev
, returning true if the content was successfully parsed; otherwise returnsfalse
.
- PySide2.QtXml.QDomDocument.setContent(dev)
- Parameters:
dev –
PySide2.QtCore.QIODevice
- Return type:
(retval, errorMsg, errorLine, errorColumn)
This is an overloaded function.
This function reads the XML document from the IO device
dev
, returning true if the content was successfully parsed; otherwise returnsfalse
.No namespace processing is performed.
- PySide2.QtXml.QDomDocument.setContent(reader, namespaceProcessing[, errorMsg=None[, errorLine=None[, errorColumn=None]]])
- Parameters:
reader –
PySide2.QtCore.QXmlStreamReader
namespaceProcessing – bool
errorMsg – str
errorLine – int
errorColumn – int
- Return type:
bool
This is an overloaded function.
This function reads the XML document from the
QXmlStreamReader
reader
and parses it. Returnstrue
if the content was successfully parsed; otherwise returnsfalse
.If
namespaceProcessing
istrue
, the parser recognizes namespaces in the XML file and sets the prefix name, local name and namespace URI to appropriate values. IfnamespaceProcessing
isfalse
, the parser does no namespace processing when it reads the XML file.If a parse error occurs, the error message is placed in
*
errorMsg
, the line number in*
errorLine
and the column number in*
errorColumn
(unless the associated pointer is set to 0).See also
QXmlStreamReader
- PySide2.QtXml.QDomDocument.toByteArray([arg__1=1])¶
- Parameters:
arg__1 – int
- Return type:
Converts the parsed document back to its textual representation and returns a
QByteArray
containing the data encoded as UTF-8.This function uses
indent
as the amount of space to indent subelements.See also
- PySide2.QtXml.QDomDocument.toString([arg__1=1])¶
- Parameters:
arg__1 – int
- Return type:
str
Converts the parsed document back to its textual representation.
This function uses
indent
as the amount of space to indent subelements.If
indent
is -1, no whitespace at all is added.
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.