QDomDocument Class Reference

[ QtXml module]

QDomDocument 类表示 XML 文档。 更多...

继承 QDomNode .

方法


详细描述

QDomDocument 类表示 XML 文档。

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 are QDomNode , QDomDocument, QDomElement and QDomText .

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 and the QDomDocument itself are 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 () and createEntityReference (). Some of these functions have versions that support namespaces, i.e. createElementNS () and createAttributeNS ()。 createDocumentFragment () function is used to hold parts of the document; this is useful for manipulating for complex documents.

整个文档内容的设置采用 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 using documentElement ()。 textual representation of the document can be obtained using toString ().

注意: The DOM tree might end up reserving a lot of memory if the XML document is big. For big XML documents, the QXmlStreamReader QXmlQuery 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 () 或 with elementsByTagNameNS ().

QDom 类的使用通常如下所示:

 QDomDocument doc("mydocument");
 QFile file("mydocument.xml");
 if (!file.open(QIODevice.ReadOnly))
     return;
 if (!doc.setContent(&file)) {
     file.close();
     return;
 }
 file.close();
 // print out the element names of all elements that are direct children
 // of the outermost element.
 QDomElement docElem = doc.documentElement();
 QDomNode n = docElem.firstChild();
 while(!n.isNull()) {
     QDomElement e = n.toElement(); // try to convert the node to an element.
     if(!e.isNull()) {
         cout << qPrintable(e.tagName()) << endl; // the node really is an element.
     }
     n = n.nextSibling();
 }
 // Here we append a new element to the end of the document
 QDomElement elem = doc.createElement("img");
 elem.setAttribute("src", "myimage.png");
 docElem.appendChild(elem);
			

一旦 doc and elem go out of scope, the whole internal tree representing the XML document is deleted.

要使用 DOM 创建文档,所用代码像这样:

 QDomDocument doc("MyML");
 QDomElement root = doc.createElement("MyML");
 doc.appendChild(root);
 QDomElement tag = doc.createElement("Greeting");
 root.appendChild(tag);
 QDomText t = doc.createTextNode("Hello World");
 tag.appendChild(t);
 QString xml = doc.toString();
			

For further information about the Document Object Model see the DOM (文档对象模型) 级别 1 and 级别 2 核心 规范。


方法文档编制

QDomDocument.__init__ ( self )

构造空文档。

QDomDocument.__init__ ( self , QString  name )

创建文档并将文档类型名称设为 name .

QDomDocument.__init__ ( self , QDomDocumentType   doctype )

创建文档采用文档类型 doctype .

另请参阅 QDomImplementation.createDocumentType ().

QDomDocument.__init__ ( self , QDomDocument   x )

构造副本为 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 ().

QDomAttr QDomDocument.createAttribute ( self , QString  name )

创建新属性称为 name that can be inserted into an element, e.g. using QDomElement.setAttributeNode ().

name is not a valid XML name, the behavior of this function is governed by QDomImplementation.InvalidDataPolicy .

另请参阅 createAttributeNS ().

QDomAttr QDomDocument.createAttributeNS ( self , QString  nsURI , QString  qName )

Creates a new attribute with namespace support that can be inserted into an element. The name of the attribute is qName 和名称空间 URI 为 nsURI 。此特征还设置 QDomNode.prefix () 和 QDomNode.localName () to appropriate values (depending on qName ).

qName is not a valid XML name, the behavior of this function is governed by QDomImplementation.InvalidDataPolicy .

另请参阅 createAttribute ().

QDomCDATASection QDomDocument.createCDATASection ( self , QString  data )

创建新 CDATA 区间为字符串 value that can be inserted into the document, e.g. using QDomNode.appendChild ().

value contains characters which cannot be stored in a CDATA section, the behavior of this function is governed by QDomImplementation.InvalidDataPolicy .

另请参阅 QDomNode.appendChild (), QDomNode.insertBefore (),和 QDomNode.insertAfter ().

QDomComment QDomDocument.createComment ( self , QString  data )

创建的新注释为字符串 value that can be inserted into the document, e.g. using QDomNode.appendChild ().

value contains characters which cannot be stored in an XML comment, the behavior of this function is governed by QDomImplementation.InvalidDataPolicy .

另请参阅 QDomNode.appendChild (), QDomNode.insertBefore (),和 QDomNode.insertAfter ().

QDomDocumentFragment QDomDocument.createDocumentFragment ( self )

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.

QDomElement QDomDocument.createElement ( self , QString  tagName )

创建的新元素称为 tagName that can be inserted into the DOM tree, e.g. using QDomNode.appendChild ().

tagName is not a valid XML name, the behavior of this function is governed by QDomImplementation.InvalidDataPolicy .

QDomNode.insertAfter ()

另请参阅 createElementNS (), QDomNode.appendChild (),和 QDomNode.insertBefore ().

QDomElement QDomDocument.createElementNS ( self , QString  nsURI , QString  qName )

Creates a new element with namespace support that can be inserted into the DOM tree. The name of the element is qName 和名称空间 URI 为 nsURI 。此特征还设置 QDomNode.prefix () 和 QDomNode.localName () to appropriate values (depending on qName ).

qName is an empty string, returns a null element regardless of whether the invalid data policy is set.

另请参阅 createElement ().

QDomEntityReference QDomDocument.createEntityReference ( self , QString  name )

创建新实体引用称为 name that can be inserted into the document, e.g. using QDomNode.appendChild ().

name is not a valid XML name, the behavior of this function is governed by QDomImplementation.InvalidDataPolicy .

另请参阅 QDomNode.appendChild (), QDomNode.insertBefore (),和 QDomNode.insertAfter ().

QDomProcessingInstruction QDomDocument.createProcessingInstruction ( self , QString  target , QString  data )

Creates a new processing instruction that can be inserted into the document, e.g. using QDomNode.appendChild (). This function sets the target for the processing instruction to target 和数据到 data .

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 by QDomImplementation.InvalidDataPolicy .

另请参阅 QDomNode.appendChild (), QDomNode.insertBefore (),和 QDomNode.insertAfter ().

QDomText QDomDocument.createTextNode ( self , QString  data )

创建文本节点为字符串 value that can be inserted into the document tree, e.g. using QDomNode.appendChild ().

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 by QDomImplementation.InvalidDataPolicy .

另请参阅 QDomNode.appendChild (), QDomNode.insertBefore (),和 QDomNode.insertAfter ().

QDomDocumentType QDomDocument.doctype ( self )

返回此文档的文档类型。

QDomElement QDomDocument.documentElement ( self )

返回文档的根元素。

QDomElement QDomDocument.elementById ( self , QString  elementId )

返回的元素 ID 等于 elementId . If no element with the ID was found, this function returns a null 元素 .

Since the QDomClasses do not know which attributes are element IDs, this function returns always a null 元素 . This may change in a future version.

QDomNodeList QDomDocument.elementsByTagName ( self , QString  tagname )

返回 QDomNodeList , that contains all the elements in the document with the name tagname . The order of the node list is the order they are encountered in a preorder traversal of the element tree.

另请参阅 elementsByTagNameNS () and QDomElement.elementsByTagName ().

QDomNodeList QDomDocument.elementsByTagNameNS ( self , QString  nsURI , QString  localName )

返回 QDomNodeList that contains all the elements in the document with the local name localName 和名称空间 URI 为 nsURI . The order of the node list is the order they are encountered in a preorder traversal of the element tree.

另请参阅 elementsByTagName () 和 QDomElement.elementsByTagNameNS ().

QDomImplementation QDomDocument.implementation ( self )

返回 QDomImplementation 对象。

QDomNode QDomDocument.importNode ( self , QDomNode   importedNode , bool  deep )

导入节点 importedNode from another document to this document. importedNode remains in the original document; this function creates a copy that can be used within this 文档。

This function returns the imported node that belongs to this document. The returned node has no parent. It is not possible to import QDomDocument and QDomDocumentType nodes. In those cases this function returns a null node .

deep 为 True,此函数不仅导入节点 importedNode 及其整个子树;若它为 false,仅 importedNode 被导入。自变量 deep has no effect on QDomAttr and QDomEntityReference nodes, since the descendants of QDomAttr nodes are always imported and those of QDomEntityReference nodes are never imported.

The behavior of this function is slightly different depending on the node types:

节点类型 Behavior
QDomAttr The owner element is set to 0 and the specified flag is set to true in the generated attribute. The whole subtree of importedNode 始终为属性节点导入: deep 不起作用。
QDomDocument 无法导入文档节点。
QDomDocumentFragment deep is true, this function imports the whole document fragment; otherwise it only generates an empty document 片段。
QDomDocumentType 无法导入文档类型节点。
QDomElement 属性为 QDomAttr.specified () is true are also imported, other attributes are not imported. If deep is true, this function also imports the subtree of importedNode ; otherwise it imports only the element node (and some attributes, see above).
QDomEntity 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.
QDomEntityReference 实体引用节点的后代从不被导入: deep 不起作用。
QDomNotation 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.
QDomProcessingInstruction The target and value of the processing instruction is copied to the new node.
QDomText 文本被拷贝到新节点。
QDomCDATASection 文本被拷贝到新节点。
QDomComment 文本被拷贝到新节点。

QDomNode.insertAfter () QDomNode.replaceChild () QDomNode.removeChild () QDomNode.appendChild ()

另请参阅 QDomElement.setAttribute () 和 QDomNode.insertBefore ().

QDomNode.NodeType QDomDocument.nodeType ( self )

返回 DocumentNode .

(bool, QString  errorMsg , int  errorLine , int  errorColumn ) QDomDocument.setContent ( self , QByteArray   data , bool  namespaceProcessing )

此函数剖析 XML 文档从字节数组 data and sets it as the content of the document. It tries to detect the encoding of the document as required by the XML specification.

namespaceProcessing is true, the parser recognizes namespaces in the XML file and sets the prefix name, local name and namespace URI to appropriate values. If namespaceProcessing is false, the parser does no namespace processing when it reads the XML file.

If a parse error occurs, this function returns false and the error message is placed in * errorMsg , the line number in * errorLine 和列号在 * errorColumn (unless the associated pointer is set to 0); otherwise this function returns true. The various error messages are described in the QXmlParseException class documentation. Note that, if you want to display these error messages to your application's users, they will be displayed in English unless they are explicitly translated.

namespaceProcessing 为 true,函数 QDomNode.prefix () returns a string for all elements and attributes. It returns an empty string if the element or attribute has no prefix.

Text nodes consisting only of whitespace are stripped and won't appear in the QDomDocument . If this behavior is not desired, one can use the setContent() overload that allows a QXmlReader to be 供给。

namespaceProcessing 为 false,函数 QDomNode.prefix (), QDomNode.localName () 和 QDomNode.namespaceURI () return an empty string.

实体引用处理如下:

QDomNode.prefix () QString.isNull () QString.isEmpty ()

警告: 此函数不 可重入 .

另请参阅 QDomNode.namespaceURI () 和 QDomNode.localName ().

(bool, QString  errorMsg , int  errorLine , int  errorColumn ) QDomDocument.setContent ( self , QString  text , bool  namespaceProcessing )

这是重载函数。

此函数读取 XML 文档从字符串 text , returning true if the content was successfully parsed; otherwise returns false. Since text is already a Unicode string, no encoding detection is done.

(bool, QString  errorMsg , int  errorLine , int  errorColumn ) QDomDocument.setContent ( self , QIODevice   dev , bool  namespaceProcessing )

这是重载函数。

此函数读取 XML 文档从 IO 设备 dev , returning true if the content was successfully parsed; otherwise returns false.

(bool, QString  errorMsg , int  errorLine , int  errorColumn ) QDomDocument.setContent ( self , QXmlInputSource   source , bool  namespaceProcessing )

这是重载函数。

此函数读取 XML 文档从 QXmlInputSource source , returning true if the content was successfully parsed; otherwise returns false.

该函数在 Qt 4.5 引入。

(bool, QString  errorMsg , int  errorLine , int  errorColumn ) QDomDocument.setContent ( self , QByteArray   buffer )

这是重载函数。

此函数读取 XML 文档从字符串 text , returning true if the content was successfully parsed; otherwise returns false. Since text is already a Unicode string, no encoding detection is performed.

也不履行名称空间处理。

(bool, QString  errorMsg , int  errorLine , int  errorColumn ) QDomDocument.setContent ( self , QString  text )

这是重载函数。

此函数读取 XML 文档从字节数组 buffer , returning true if the content was successfully parsed; otherwise returns false.

不履行名称空间处理。

(bool, QString  errorMsg , int  errorLine , int  errorColumn ) QDomDocument.setContent ( self , QIODevice   dev )

这是重载函数。

此函数读取 XML 文档从 IO 设备 dev , returning true if the content was successfully parsed; otherwise returns false.

不履行名称空间处理。

(bool, QString  errorMsg , int  errorLine , int  errorColumn ) QDomDocument.setContent ( self , QXmlInputSource   source , QXmlReader   reader )

这是重载函数。

此函数读取 XML 文档从 QXmlInputSource source and parses it with the QXmlReader reader , returning true if the content was successfully parsed; otherwise returns false.

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.

另请参阅 QXmlSimpleReader .

QByteArray QDomDocument.toByteArray ( self , int  indent  = 1)

Converts the parsed document back to its textual representation 并返回 QByteArray containing the data encoded as UTF-8.

此函数使用 indent as the amount of space to indent subelements.

另请参阅 toString ().

QString QDomDocument.toString ( self , int  indent  = 1)

Converts the parsed document back to its textual representation.

此函数使用 indent as the amount of space to indent subelements.

indent 为 -1,根本不添加空格。