Classes - Annotated - Tree - Functions - Home - Structure

QDomDocument Class Reference
[XML module]

The QDomDocument class is the representation of an XML document. More...

#include <qdom.h>

Inherits QDomNode.

List of all member functions.

Public Members


Detailed Description

The QDomDocument class is the representation of 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 parsed XML is represented internally by a tree of objects that can be accessed using the various QDom classes. All QDom classes do 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 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 can not be manipulated or inserted into the Document.

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 a direct child
  // 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() ) { // the node was really an element.
          cout << e.tagName() << endl;
      }
      n = n.nextSibling();
  }

  // lets 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 scode, the whole internal tree representing the XML document will get deleted.

For further information about the Document Objct Model see http://www.w3.org/TR/REC-DOM-Level-1/.


Member Function Documentation

QDomDocument::QDomDocument ()

Constructs an empty document.

QDomDocument::QDomDocument ( const QString & name )

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

QDomDocument::QDomDocument ( const QDomDocumentType & doctype )

Creates a document with the document type doctype.

See also QDomImplementation::createDocumentType().

QDomDocument::QDomDocument ( const QDomDocument & x )

Constructs a copy of x.

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

QDomDocument::~QDomDocument ()

Destructor.

QDomAttr QDomDocument::createAttribute ( const QString & name )

Creates a new attribute with the name name that can be inserted into an element.

See also createAttributeNS().

QDomAttr QDomDocument::createAttributeNS ( const QString & nsURI, const QString & 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 ( const QString & value )

Creates a new CDATA section for the string value that can be inserted into the document.

QDomComment QDomDocument::createComment ( const QString & value )

Creates a new comment for the string value that can be inserted into the Document.

QDomDocumentFragment QDomDocument::createDocumentFragment ()

Creates a new document fragment, that can be used to hold parts of the document, when doing complex manipulations of the document tree.

QDomElement QDomDocument::createElement ( const QString & tagName )

Creates a new element with the name tagName that can be inserted into the DOM tree.

See also createElementNS().

QDomElement QDomDocument::createElementNS ( const QString & nsURI, const QString & 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 ( const QString & name )

Creates a new entity reference with the name name.

QDomProcessingInstruction QDomDocument::createProcessingInstruction ( const QString & target, const QString & data )

Creates a new processing instruction that can be inserted into the document. This function sets the target for the processing insruction to target and the data to data.

QDomText QDomDocument::createTextNode ( const QString & value )

Creates a text node for the string value that can be inserted into the document tree.

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 ( const QString & 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 ( const QString & tagname ) const

Returns a QDomNodeList, that contains all 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 ( const QString & nsURI, const QString & localName )

Returns a QDomNodeList, that contains all elements in the document with the local name localName and the namespace URI 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 ( const QDomNode & importedNode, bool deep )

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 TRUE, only importedNode is imported. The argument deep has no effect for 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 for the possible node types:

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= ( const QDomDocument & x )

Assigns x to this DOM document.

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

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

This function parses the string text and sets it as the content of the document. 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, this function returns TRUE, otherwise FALSE. If errorMsg, errorLine resp. errorColumn is non-null, then in the case of a parse error, the error message, the line number where the error occurred resp. the column number where the error occurred is stored in the corresponding variable.

If namespaceProcessing is TRUE, the function QDomNode::prefix() returns not null for all elements and attributes (it returns an empty, but not null 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 ( const QCString & buffer, bool namespaceProcessing, 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.

bool QDomDocument::setContent ( const QByteArray & buffer, bool namespaceProcessing, 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.

bool QDomDocument::setContent ( QIODevice * dev, bool namespaceProcessing, 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 ( const QCString & 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.

No namespace processing is done.

bool QDomDocument::setContent ( const QByteArray & 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 ( const QString & 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.

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


Search the documentation, FAQ, qt-interest archive and more (uses www.trolltech.com):


This file is part of the Qt toolkit, copyright © 1995-2000 Trolltech, all rights reserved.


Copyright © 2000 TrolltechTrademarks
Qt version main-beta1