You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tqt3/doc/man/man3/tqdomdocument.3qt

449 lines
23 KiB

'\" t
.TH QDomDocument 3qt "2 February 2007" "Trolltech AS" \" -*- nroff -*-
.\" Copyright 1992-2007 Trolltech ASA. All rights reserved. See the
.\" license file included in the distribution for a complete license
.\" statement.
.\"
.ad l
.nh
.SH NAME
QDomDocument \- Represents an XML document
.SH SYNOPSIS
All the functions in this class are reentrant when TQt is built with thread support.</p>
.PP
\fC#include <ntqdom.h>\fR
.PP
Inherits QDomNode.
.PP
.SS "Public Members"
.in +1c
.ti -1c
.BI "\fBQDomDocument\fR ()"
.br
.ti -1c
.BI "explicit \fBQDomDocument\fR ( const QString & name )"
.br
.ti -1c
.BI "explicit \fBQDomDocument\fR ( const QDomDocumentType & doctype )"
.br
.ti -1c
.BI "\fBQDomDocument\fR ( const QDomDocument & x )"
.br
.ti -1c
.BI "QDomDocument & \fBoperator=\fR ( const QDomDocument & x )"
.br
.ti -1c
.BI "\fB~QDomDocument\fR ()"
.br
.ti -1c
.BI "QDomElement \fBcreateElement\fR ( const QString & tagName )"
.br
.ti -1c
.BI "QDomDocumentFragment \fBcreateDocumentFragment\fR ()"
.br
.ti -1c
.BI "QDomText \fBcreateTextNode\fR ( const QString & value )"
.br
.ti -1c
.BI "QDomComment \fBcreateComment\fR ( const QString & value )"
.br
.ti -1c
.BI "QDomCDATASection \fBcreateCDATASection\fR ( const QString & value )"
.br
.ti -1c
.BI "QDomProcessingInstruction \fBcreateProcessingInstruction\fR ( const QString & target, const QString & data )"
.br
.ti -1c
.BI "QDomAttr \fBcreateAttribute\fR ( const QString & name )"
.br
.ti -1c
.BI "QDomEntityReference \fBcreateEntityReference\fR ( const QString & name )"
.br
.ti -1c
.BI "QDomNodeList \fBelementsByTagName\fR ( const QString & tagname ) const"
.br
.ti -1c
.BI "QDomNode \fBimportNode\fR ( const QDomNode & importedNode, bool deep )"
.br
.ti -1c
.BI "QDomElement \fBcreateElementNS\fR ( const QString & nsURI, const QString & qName )"
.br
.ti -1c
.BI "QDomAttr \fBcreateAttributeNS\fR ( const QString & nsURI, const QString & qName )"
.br
.ti -1c
.BI "QDomNodeList \fBelementsByTagNameNS\fR ( const QString & nsURI, const QString & localName )"
.br
.ti -1c
.BI "QDomElement \fBelementById\fR ( const QString & elementId )"
.br
.ti -1c
.BI "QDomDocumentType \fBdoctype\fR () const"
.br
.ti -1c
.BI "QDomImplementation \fBimplementation\fR () const"
.br
.ti -1c
.BI "QDomElement \fBdocumentElement\fR () const"
.br
.ti -1c
.BI "bool \fBsetContent\fR ( const QCString & buffer, bool namespaceProcessing, QString * errorMsg = 0, int * errorLine = 0, int * errorColumn = 0 )"
.br
.ti -1c
.BI "bool \fBsetContent\fR ( const QByteArray & buffer, bool namespaceProcessing, QString * errorMsg = 0, int * errorLine = 0, int * errorColumn = 0 )"
.br
.ti -1c
.BI "bool \fBsetContent\fR ( const QString & text, bool namespaceProcessing, QString * errorMsg = 0, int * errorLine = 0, int * errorColumn = 0 )"
.br
.ti -1c
.BI "bool \fBsetContent\fR ( QIODevice * dev, bool namespaceProcessing, QString * errorMsg = 0, int * errorLine = 0, int * errorColumn = 0 )"
.br
.ti -1c
.BI "bool \fBsetContent\fR ( const QCString & buffer, QString * errorMsg = 0, int * errorLine = 0, int * errorColumn = 0 )"
.br
.ti -1c
.BI "bool \fBsetContent\fR ( const QByteArray & buffer, QString * errorMsg = 0, int * errorLine = 0, int * errorColumn = 0 )"
.br
.ti -1c
.BI "bool \fBsetContent\fR ( const QString & text, QString * errorMsg = 0, int * errorLine = 0, int * errorColumn = 0 )"
.br
.ti -1c
.BI "bool \fBsetContent\fR ( QIODevice * dev, QString * errorMsg = 0, int * errorLine = 0, int * errorColumn = 0 )"
.br
.ti -1c
.BI "bool \fBsetContent\fR ( QXmlInputSource * source, QXmlReader * reader, QString * errorMsg = 0, int * errorLine = 0, int * errorColumn = 0 )"
.br
.ti -1c
.BI "virtual QDomNode::NodeType \fBnodeType\fR () const"
.br
.ti -1c
.BI "virtual bool \fBisDocument\fR () const"
.br
.ti -1c
.BI "QString \fBtoString\fR () const"
.br
.ti -1c
.BI "QString \fBtoString\fR ( int indent ) const"
.br
.ti -1c
.BI "QCString \fBtoCString\fR () const"
.br
.ti -1c
.BI "QCString \fBtoCString\fR ( int indent ) const"
.br
.in -1c
.SH DESCRIPTION
The QDomDocument class represents an XML document.
.PP
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.
.PP
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.
.PP
The parsed XML is represented internally by a tree of objects that can be accessed using the various QDom classes. All QDom classes only \fIreference\fR 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.
.PP
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.
.PP
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.
.PP
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().
.PP
It is possible to insert a node from another document into the document using importNode().
.PP
You can obtain a list of all the elements that have a particular tag with elementsByTagName() or with elementsByTagNameNS().
.PP
The QDom classes are typically used as follows:
.PP
.nf
.br
QDomDocument doc( "mydocument" );
.br
QFile file( "mydocument.xml" );
.br
if ( !file.open( IO_ReadOnly ) )
.br
return;
.br
if ( !doc.setContent( &file ) ) {
.br
file.close();
.br
return;
.br
}
.br
file.close();
.br
.br
// print out the element names of all elements that are direct children
.br
// of the outermost element.
.br
QDomElement docElem = doc.documentElement();
.br
.br
QDomNode n = docElem.firstChild();
.br
while( !n.isNull() ) {
.br
QDomElement e = n.toElement(); // try to convert the node to an element.
.br
if( !e.isNull() ) {
.br
cout << e.tagName() << endl; // the node really is an element.
.br
}
.br
n = n.nextSibling();
.br
}
.br
.br
// Here we append a new element to the end of the document
.br
QDomElement elem = doc.createElement( "img" );
.br
elem.setAttribute( "src", "myimage.png" );
.br
docElem.appendChild( elem );
.br
.fi
.PP
Once \fCdoc\fR and \fCelem\fR go out of scope, the whole internal tree representing the XML document is deleted.
.PP
To create a document using DOM use code like this:
.PP
.nf
.br
QDomDocument doc( "MyML" );
.br
QDomElement root = doc.createElement( "MyML" );
.br
doc.appendChild( root );
.br
.br
QDomElement tag = doc.createElement( "Greeting" );
.br
root.appendChild( tag );
.br
.br
QDomText t = doc.createTextNode( "Hello World" );
.br
tag.appendChild( t );
.br
.br
QString xml = doc.toString();
.br
.fi
.PP
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.
.PP
See also XML.
.SH MEMBER FUNCTION DOCUMENTATION
.SH "QDomDocument::QDomDocument ()"
Constructs an empty document.
.SH "explicit QDomDocument::QDomDocument ( const QString & name )"
Creates a document and sets the name of the document type to \fIname\fR.
.SH "explicit QDomDocument::QDomDocument ( const QDomDocumentType & doctype )"
Creates a document with the document type \fIdoctype\fR.
.PP
See also QDomImplementation::createDocumentType().
.SH "QDomDocument::QDomDocument ( const QDomDocument & x )"
Constructs a copy of \fIx\fR.
.PP
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().
.SH "QDomDocument::~QDomDocument ()"
Destroys the object and frees its resources.
.SH "QDomAttr QDomDocument::createAttribute ( const QString & name )"
Creates a new attribute called \fIname\fR that can be inserted into an element, e.g. using QDomElement::setAttributeNode().
.PP
See also createAttributeNS().
.SH "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 \fIqName\fR and the namespace URI is \fInsURI\fR. This function also sets QDomNode::prefix() and QDomNode::localName() to appropriate values (depending on \fIqName\fR).
.PP
See also createAttribute().
.SH "QDomCDATASection QDomDocument::createCDATASection ( const QString & value )"
Creates a new CDATA section for the string \fIvalue\fR that can be inserted into the document, e.g. using QDomNode::appendChild().
.PP
See also QDomNode::appendChild(), QDomNode::insertBefore(), and QDomNode::insertAfter().
.SH "QDomComment QDomDocument::createComment ( const QString & value )"
Creates a new comment for the string \fIvalue\fR that can be inserted into the document, e.g. using QDomNode::appendChild().
.PP
See also QDomNode::appendChild(), QDomNode::insertBefore(), and QDomNode::insertAfter().
.SH "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.
.SH "QDomElement QDomDocument::createElement ( const QString & tagName )"
Creates a new element called \fItagName\fR that can be inserted into the DOM tree, e.g. using QDomNode::appendChild().
.PP
See also createElementNS(), QDomNode::appendChild(), QDomNode::insertBefore(), and QDomNode::insertAfter().
.SH "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 \fIqName\fR and the namespace URI is \fInsURI\fR. This function also sets QDomNode::prefix() and QDomNode::localName() to appropriate values (depending on \fIqName\fR).
.PP
See also createElement().
.SH "QDomEntityReference QDomDocument::createEntityReference ( const QString & name )"
Creates a new entity reference called \fIname\fR that can be inserted into the document, e.g. using QDomNode::appendChild().
.PP
See also QDomNode::appendChild(), QDomNode::insertBefore(), and QDomNode::insertAfter().
.SH "QDomProcessingInstruction QDomDocument::createProcessingInstruction ( const QString & target, const 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 \fItarget\fR and the data to \fIdata\fR.
.PP
See also QDomNode::appendChild(), QDomNode::insertBefore(), and QDomNode::insertAfter().
.SH "QDomText QDomDocument::createTextNode ( const QString & value )"
Creates a text node for the string \fIvalue\fR that can be inserted into the document tree, e.g. using QDomNode::appendChild().
.PP
\fBWarning:\fR All characters within an XML document must be in the range:
.PP
#x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
.PP
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.
.PP
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.
.PP
See also QDomNode::appendChild(), QDomNode::insertBefore(), and QDomNode::insertAfter().
.SH "QDomDocumentType QDomDocument::doctype () const"
Returns the document type of this document.
.SH "QDomElement QDomDocument::documentElement () const"
Returns the root element of the document.
.SH "QDomElement QDomDocument::elementById ( const QString & elementId )"
Returns the element whose ID is equal to \fIelementId\fR. If no element with the ID was found, this function returns a null element.
.PP
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.
.SH "QDomNodeList QDomDocument::elementsByTagName ( const QString & tagname ) const"
Returns a QDomNodeList, that contains all the elements in the document with the name \fItagname\fR. The order of the node list is the order they are encountered in a preorder traversal of the element tree.
.PP
See also elementsByTagNameNS() and QDomElement::elementsByTagName().
.SH "QDomNodeList QDomDocument::elementsByTagNameNS ( const QString & nsURI, const QString & localName )"
Returns a QDomNodeList that contains all the elements in the document with the local name \fIlocalName\fR and a namespace URI of \fInsURI\fR. The order of the node list is the order they are encountered in a preorder traversal of the element tree.
.PP
See also elementsByTagName() and QDomElement::elementsByTagNameNS().
.SH "QDomImplementation QDomDocument::implementation () const"
Returns a QDomImplementation object.
.SH "QDomNode QDomDocument::importNode ( const QDomNode & importedNode, bool deep )"
Imports the node \fIimportedNode\fR from another document to this document. \fIimportedNode\fR remains in the original document; this function creates a copy that can be used within this document.
.PP
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.
.PP
If \fIdeep\fR is TRUE, this function imports not only the node \fIimportedNode\fR but its whole subtree; if it is FALSE, only the \fIimportedNode\fR is imported. The argument \fIdeep\fR 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.
.PP
The behavior of this function is slightly different depending on the node types: <center>.nf
.TS
l - l. 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 \fIimportedNode\fR is always imported for attribute nodes: \fIdeep\fR has no effect. QDomDocument Document nodes cannot be imported. QDomDocumentFragment If \fIdeep\fR 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 \fIdeep\fR is TRUE, this function also imports the subtree of \fIimportedNode\fR; 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: \fIdeep\fR 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
.TE
.fi
</center>
.PP
See also QDomElement::setAttribute(), QDomNode::insertBefore(), QDomNode::insertAfter(), QDomNode::replaceChild(), QDomNode::removeChild(), and QDomNode::appendChild().
.SH "bool QDomDocument::isDocument () const\fC [virtual]\fR"
Returns TRUE.
.PP
Reimplemented from QDomNode.
.SH "QDomNode::NodeType QDomDocument::nodeType () const\fC [virtual]\fR"
Returns DocumentNode.
.PP
Reimplemented from QDomNode.
.SH "QDomDocument & QDomDocument::operator= ( const QDomDocument & x )"
Assigns \fIx\fR to this DOM document.
.PP
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().
.SH "bool QDomDocument::setContent ( const QByteArray & buffer, bool namespaceProcessing, QString * errorMsg = 0, int * errorLine = 0, int * errorColumn = 0 )"
This function parses the XML document from the byte array \fIbuffer\fR and sets it as the content of the document. It tries to detect the encoding of the document as required by the XML specification.
.PP
If \fInamespaceProcessing\fR is TRUE, the parser recognizes namespaces in the XML file and sets the prefix name, local name and namespace URI to appropriate values. If \fInamespaceProcessing\fR is FALSE, the parser does no namespace processing when it reads the XML file.
.PP
If a parse error occurs, the function returns FALSE; otherwise it returns TRUE. If a parse error occurs and \fIerrorMsg\fR, \fIerrorLine\fR and \fIerrorColumn\fR are not 0, the error message is placed in \fI*errorMsg\fR, the line number \fI*errorLine\fR and the column number in \fI*errorColumn\fR.
.PP
If \fInamespaceProcessing\fR 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.
.PP
If \fInamespaceProcessing\fR is FALSE, the functions QDomNode::prefix(), QDomNode::localName() and QDomNode::namespaceURI() return QString::null.
.PP
See also QDomNode::namespaceURI(), QDomNode::localName(), QDomNode::prefix(), QString::isNull(), and QString::isEmpty().
.SH "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.
.PP
This function reads the XML document from the C string \fIbuffer\fR.
.PP
\fBWarning:\fR This function does not try to detect the encoding: instead it assumes that the C string is UTF-8 encoded.
.SH "bool QDomDocument::setContent ( const QString & text, 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.
.PP
This function reads the XML document from the string \fItext\fR. Since \fItext\fR is already a Unicode string, no encoding detection is done.
.SH "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.
.PP
This function reads the XML document from the IO device \fIdev\fR.
.SH "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.
.PP
This function reads the XML document from the C string \fIbuffer\fR.
.PP
No namespace processing is performed.
.PP
\fBWarning:\fR This function does not try to detect the encoding: instead it assumes that the C string is UTF-8 encoded.
.SH "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.
.PP
This function reads the XML document from the byte array \fIbuffer\fR.
.PP
No namespace processing is performed.
.SH "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.
.PP
This function reads the XML document from the string \fItext\fR. Since \fItext\fR is already a Unicode string, no encoding detection is performed.
.PP
No namespace processing is performed either.
.SH "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.
.PP
This function reads the XML document from the IO device \fIdev\fR.
.PP
No namespace processing is performed.
.SH "bool QDomDocument::setContent ( QXmlInputSource * source, QXmlReader * reader, 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.
.PP
This function reads the XML document from the QXmlInputSource \fIsource\fR and parses it with the QXmlReader \fIreader\fR.
.PP
This function doesn't change the features of the \fIreader\fR. If you want to use certain features for parsing you can use this function to set up the reader appropriate.
.PP
See also QXmlSimpleReader.
.SH "QCString QDomDocument::toCString () const"
Converts the parsed document back to its textual representation and returns a QCString for that is encoded in UTF-8.
.PP
See also toString().
.SH "QCString QDomDocument::toCString ( int indent ) const"
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
This function uses \fIindent\fR as the amount of space to indent subelements.
.SH "QString QDomDocument::toString () const"
Converts the parsed document back to its textual representation.
.PP
See also toCString().
.SH "QString QDomDocument::toString ( int indent ) const"
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
This function uses \fIindent\fR as the amount of space to indent
subelements.
.SH "SEE ALSO"
.BR http://doc.trolltech.com/qdomdocument.html
.BR http://www.trolltech.com/faq/tech.html
.SH COPYRIGHT
Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. See the
license file included in the distribution for a complete license
statement.
.SH AUTHOR
Generated automatically from the source code.
.SH BUGS
If you find a bug in Qt, please report it as described in
.BR http://doc.trolltech.com/bughowto.html .
Good bug reports help us to help you. Thank you.
.P
The definitive TQt documentation is provided in HTML format; it is
located at $QTDIR/doc/html and can be read using TQt Assistant or with
a web browser. This man page is provided as a convenience for those
users who prefer man pages, although this format is not officially
supported by Trolltech.
.P
If you find errors in this manual page, please report them to
.BR qt-bugs@trolltech.com .
Please include the name of the manual page (tqdomdocument.3qt) and the Qt
version (3.3.8).