Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |
The QDomDocument class represents an XML document. More...
All the functions in this class are reentrant when Qt is built with thread support.
#include <qdom.h>
Inherits QDomNode.
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(). The createDocumentFragment() 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 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 file( "mydocument.xml" ); if ( !file.open( IO_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 << 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 is 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.
See also QDomImplementation::createDocumentType().
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().
See also createAttributeNS().
See also createAttribute().
See also QDomNode::appendChild(), QDomNode::insertBefore(), and QDomNode::insertAfter().
See also QDomNode::appendChild(), QDomNode::insertBefore(), and QDomNode::insertAfter().
See also createElementNS(), QDomNode::appendChild(), QDomNode::insertBefore(), and QDomNode::insertAfter().
See also createElement().
See also QDomNode::appendChild(), QDomNode::insertBefore(), and QDomNode::insertAfter().
See also QDomNode::appendChild(), QDomNode::insertBefore(), and QDomNode::insertAfter().
Warning: All characters within an XML document must be in the range:
#x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
This rule also applies to characters encoded as character entities and characters in CDATA sections. If you use this function to insert characters outside of this range, the document will not be well-formed.
If you want to store binary data in an XML document you must either use your own scheme to escape illegal characters, or you must store it in an external unparsed entity.
See also QDomNode::appendChild(), QDomNode::insertBefore(), and QDomNode::insertAfter().
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.
See also elementsByTagNameNS() and QDomElement::elementsByTagName().
See also elementsByTagName() and QDomElement::elementsByTagNameNS().
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 its whole subtree; if it is FALSE, only the importedNode is imported. The argument deep has no effect on QDomAttr and QDomEntityReference nodes, since the descendents 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:
Node Type | Behaviour |
---|---|
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 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 read-only in DOM level 2. |
QDomEntityReference | Descendents 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 read-only in DOM level 2. |
QDomProcessingInstruction | The target and value of the processing instruction is copied to the new node. |
QDomText | The text is copied to the new node. |
QDomCDATASection | The text is copied to the new node. |
QDomComment | The text is copied to the new node. |
See also QDomElement::setAttribute(), QDomNode::insertBefore(), QDomNode::insertAfter(), QDomNode::replaceChild(), QDomNode::removeChild(), and QDomNode::appendChild().
Reimplemented from QDomNode.
Reimplemented from QDomNode.
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().
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 it returns TRUE. If a parse error occurs and errorMsg, errorLine and errorColumn are not 0, the error message is placed in *errorMsg, the line number *errorLine and the column number in *errorColumn.
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 QString::null.
See also QDomNode::namespaceURI(), QDomNode::localName(), QDomNode::prefix(), QString::isNull(), and QString::isEmpty().
This function reads the XML document from the C string buffer.
Warning: This function does not try to detect the encoding: instead it assumes that the C string is UTF-8 encoded.
This function reads the XML document from the string text. Since text is already a Unicode string, no encoding detection is done.
This function reads the XML document from the IO device dev.
This function reads the XML document from the C string buffer.
No namespace processing is performed.
Warning: This function does not try to detect the encoding: instead it assumes that the C string is UTF-8 encoded.
This function reads the XML document from the byte array buffer.
No namespace processing is performed.
This function reads the XML document from the string text. Since text is already a Unicode string, no encoding detection is performed.
No namespace processing is performed either.
This function reads the XML document from the IO device dev.
No namespace processing is performed.
This function reads the XML document from the QXmlInputSource source and parses it with the QXmlReader reader.
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 appropriate.
See also QXmlSimpleReader.
See also toString().
This function uses indent as the amount of space to indent subelements.
See also toCString().
This function uses indent as the amount of space to indent subelements.
This file is part of the Qt toolkit. Copyright © 1995-2007 Trolltech. All Rights Reserved.
Copyright © 2007 Trolltech | Trademarks | Qt 3.3.8
|