返回介绍

QDomDocument Class

发布于 2019-10-04 14:59:45 字数 20827 浏览 1026 评论 0 收藏 0

The QDomDocument class represents an XML document. More...

#include <qdom.h>

Inherits QDomNode.

List of all member functions.

Public Members

  • QDomDocument ()
  • QDomDocument ( constQString&name )
  • QDomDocument ( constQDomDocumentType&doctype )
  • QDomDocument ( constQDomDocument&x )
  • QDomDocument & operator= ( constQDomDocument&x )
  • ~QDomDocument ()
  • QDomElement createElement ( constQString&tagName )
  • QDomDocumentFragment createDocumentFragment ()
  • QDomText createTextNode ( constQString&value )
  • QDomComment createComment ( constQString&value )
  • QDomCDATASection createCDATASection ( constQString&value )
  • QDomProcessingInstruction createProcessingInstruction ( constQString&target, constQString&data )
  • QDomAttr createAttribute ( constQString&name )
  • QDomEntityReference createEntityReference ( constQString&name )
  • QDomNodeList elementsByTagName ( constQString&tagname ) const
  • QDomNode importNode ( constQDomNode&importedNode, booldeep )
  • QDomElement createElementNS ( constQString&nsURI, constQString&qName )
  • QDomAttr createAttributeNS ( constQString&nsURI, constQString&qName )
  • QDomNodeList elementsByTagNameNS ( constQString&nsURI, constQString&localName )
  • QDomElement elementById ( constQString&elementId )
  • QDomDocumentType doctype () const
  • QDomImplementation implementation () const
  • QDomElement documentElement () const
  • bool setContent ( constQCString&buffer, boolnamespaceProcessing, QString*errorMsg = 0, int*errorLine = 0, int*errorColumn = 0 )
  • bool setContent ( constQByteArray&buffer, boolnamespaceProcessing, QString*errorMsg = 0, int*errorLine = 0, int*errorColumn = 0 )
  • bool setContent ( constQString&text, boolnamespaceProcessing, QString*errorMsg = 0, int*errorLine = 0, int*errorColumn = 0 )
  • bool setContent ( QIODevice*dev, boolnamespaceProcessing, QString*errorMsg = 0, int*errorLine = 0, int*errorColumn = 0 )
  • bool setContent ( constQCString&buffer, QString*errorMsg = 0, int*errorLine = 0, int*errorColumn = 0 )
  • bool setContent ( constQByteArray&buffer, QString*errorMsg = 0, int*errorLine = 0, int*errorColumn = 0 )
  • bool setContent ( constQString&text, QString*errorMsg = 0, int*errorLine = 0, int*errorColumn = 0 )
  • bool setContent ( QIODevice*dev, QString*errorMsg = 0, int*errorLine = 0, int*errorColumn = 0 )
  • virtual QDomNode::NodeType nodeType () const
  • virtual bool isDocument () const
  • QString toString () const
  • QCString toCString () const

Detailed Description

The QDomDocument class represents an XML document.

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 via 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(). The createDocumentFragment() function is used to hold parts of the document, e.g. 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 using documentElement(). The textual representation of the document can be obtained using toString().

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 with elementsByTagNameNS().

The QDom classes are typically used as follows:

  QDomDocument doc( "mydocument" );
  QFile f( "mydocument.xml" );
  if ( !f.open( IO_ReadOnly ) )
      return;
  if ( !doc.setContent( &f ) ) {
      f.close();
      return;
  }
  f.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 << 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 );
  

Once doc and elem go out of scope, the whole internal tree representing the XML document will get deleted.

To create a document using DOM use code like this:

    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 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

QDomDocument::QDomDocument ()

Constructs an empty document.

QDomDocument::QDomDocument ( constQString&name )

Creates a document and sets the name of the document type to name.

QDomDocument::QDomDocument ( constQDomDocumentType&doctype )

Creates a document with the document type doctype.

See also QDomImplementation::createDocumentType().

QDomDocument::QDomDocument ( constQDomDocument&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().

QDomDocument::~QDomDocument ()

Destroys the object and frees its resources.

QDomAttr QDomDocument::createAttribute ( constQString&name )

Creates a new attribute called name that can be inserted into an element, e.g. using QDomElement::setAttributeNode().

See also createAttributeNS().

QDomAttr QDomDocument::createAttributeNS ( constQString&nsURI, constQString&qName )

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 is nsURI. This function also sets QDomNode::prefix() and QDomNode::localName() to appropriate values (depending on qName).

See also createAttribute().

QDomCDATASection QDomDocument::createCDATASection ( constQString&value )

Creates a new CDATA section for the string value that can be inserted into the document, e.g. using QDomNode::appendChild().

See also QDomNode::appendChild(), QDomNode::insertBefore() and QDomNode::insertAfter().

QDomComment QDomDocument::createComment ( constQString&value )

Creates a new comment for the string value that can be inserted into the document, e.g. using QDomNode::appendChild().

See also QDomNode::appendChild(), QDomNode::insertBefore() and QDomNode::insertAfter().

QDomDocumentFragment QDomDocument::createDocumentFragment ()

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 ( constQString&tagName )

Creates a new element called tagName that can be inserted into the DOM tree, e.g. using QDomNode::appendChild().

See also createElementNS(), QDomNode::appendChild(), QDomNode::insertBefore() and QDomNode::insertAfter().

QDomElement QDomDocument::createElementNS ( constQString&nsURI, constQString&qName )

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 is nsURI. This function also sets QDomNode::prefix() and QDomNode::localName() to appropriate values (depending on qName).

See also createElement().

QDomEntityReference QDomDocument::createEntityReference ( constQString&name )

Creates a new entity reference called name that can be inserted into the document, e.g. using QDomNode::appendChild().

See also QDomNode::appendChild(), QDomNode::insertBefore() and QDomNode::insertAfter().

QDomProcessingInstruction QDomDocument::createProcessingInstruction ( constQString&target, constQString&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 and the data to data.

See also QDomNode::appendChild(), QDomNode::insertBefore() and QDomNode::insertAfter().

QDomText QDomDocument::createTextNode ( constQString&value )

Creates a text node for the string value that can be inserted into the document tree, e.g. using QDomNode::appendChild().

See also QDomNode::appendChild(), QDomNode::insertBefore() and QDomNode::insertAfter().

QDomDocumentType QDomDocument::doctype () const

Returns the document type of this document.

QDomElement QDomDocument::documentElement () const

Returns the root element of the document.

QDomElement QDomDocument::elementById ( constQString&elementId )

Returns the element whose ID is equal to elementId. If no element with the ID was found, this function returns a null element.

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

QDomNodeList QDomDocument::elementsByTagName ( constQString&tagname ) const

Returns a 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.

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

QDomNodeList QDomDocument::elementsByTagNameNS ( constQString&nsURI, constQString&localName )

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

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

QDomImplementation QDomDocument::implementation () const

Returns a QDomImplementation object.

QDomNode QDomDocument::importNode ( constQDomNode&importedNode, booldeep )

Imports the node importedNode from another document to this document. importedNode remains in the original document; this function creates a copy of it 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 and QDomDocumentType nodes. In those cases this function returns a null node.

If deep is TRUE, this function imports not only the node importedNode but the whole subtree; if it is FALSE, only the importedNode is imported. The argument 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:

  • QDomAttr - The owner element is set to 0 and the specified flag is set to TRUE on the generated attribute. The whole subtree of importedNode is always imported for attribute nodes - deep has no effect.
  • QDomDocument - Document nodes cannot be imported.
  • QDomDocumentFragment - If deep is TRUE, this function imports the whole document fragment, otherwise it only generates an empty document fragment.
  • QDomDocumentType - Document type nodes cannot be imported.
  • QDomElement - Attributes for which 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 readonly in DOM level 2.
  • QDomEntityReference - Descendants of entity reference nodes are never imported - deep has no effect.
  • QDomNotation - Notation nodes can be imported, but at the moment there is no way to use them since the document type is readonly in DOM level 2.
  • QDomProcessingInstruction - The target and value of the processing instruction is copied to the new node.
  • QDomText, QDomCDATASection and QDomComment - The text is copied to the new node.

See also QDomElement::setAttribute(), QDomNode::insertBefore(), QDomNode::insertAfter(), QDomNode::replaceChild(), QDomNode::removeChild() and QDomNode::appendChild().

bool QDomDocument::isDocument () const [virtual]

Returns TRUE.

Reimplemented from QDomNode.

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

Returns DocumentNode.

Reimplemented from QDomNode.

QDomDocument& QDomDocument::operator= ( constQDomDocument&x )

Assigns x to this DOM document.

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().

bool QDomDocument::setContent ( constQByteArray&buffer, boolnamespaceProcessing, QString*errorMsg = 0, int*errorLine = 0, int*errorColumn = 0 )

This function parses the XML document from the byte array buffer and sets it as the content of the document. It tries to detect the encoding of the document as required by the XML specification.

If 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, the function returns FALSE; otherwise TRUE. If a parse error occurs the error message is placed in *errorMsg, the line number in *errorLine and the column number in *errorColumn. These error variables will only be populated if they are non-null.

If namespaceProcessing is TRUE, the function QDomNode::prefix() returns a string for all elements and attributes. It returns an empty string if the element or attribute has no prefix.

If namespaceProcessing is FALSE, the functions QDomNode::prefix(), QDomNode::localName() and QDomNode::namespaceURI() return a null string.

See also QDomNode::namespaceURI(), QDomNode::localName(), QDomNode::prefix(), QString::isNull() and QString::isEmpty().

bool QDomDocument::setContent ( constQCString&buffer, boolnamespaceProcessing, QString*errorMsg = 0, int*errorLine = 0, int*errorColumn = 0 )

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

This function reads the XML document from the C string buffer.

Use this function with extreme care, since it does not try to detect the encoding, but rather assumes that the C string is in Utf8 encoding.

bool QDomDocument::setContent ( constQString&text, boolnamespaceProcessing, QString*errorMsg = 0, int*errorLine = 0, int*errorColumn = 0 )

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

This function reads the XML document from the string text. Since text is already a unicode string, no encoding detection is done.

bool QDomDocument::setContent ( QIODevice*dev, boolnamespaceProcessing, QString*errorMsg = 0, int*errorLine = 0, int*errorColumn = 0 )

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

This function reads the XML document from the IO device dev.

bool QDomDocument::setContent ( constQCString&buffer, QString*errorMsg = 0, int*errorLine = 0, int*errorColumn = 0 )

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

This function reads the XML document from the C string buffer.

Use this function with extreme care, since it does not try to detect the encoding, but rather assumes that the C string is in Utf8 encoding.

No namespace processing is done.

bool QDomDocument::setContent ( constQByteArray&buffer, QString*errorMsg = 0, int*errorLine = 0, int*errorColumn = 0 )

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

This function reads the XML document from the byte array buffer.

No namespace processing is done.

bool QDomDocument::setContent ( constQString&text, QString*errorMsg = 0, int*errorLine = 0, int*errorColumn = 0 )

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

This function reads the XML document from the string text. Since text is already a unicode string, no encoding detection is done.

No namespace processing is done.

bool QDomDocument::setContent ( QIODevice*dev, QString*errorMsg = 0, int*errorLine = 0, int*errorColumn = 0 )

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

This function reads the XML document from the IO device dev.

No namespace processing is done.

QCString QDomDocument::toCString () const

Converts the parsed document back to its textual representation and returns a QCString for that is encoded in UTF-8.

See also toString().

QString QDomDocument::toString () const

Converts the parsed document back to its textual representation.

See also toCString().

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

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

发布评论

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