QDomDocument(3qt)

πŸ“¦ version 3.3.8b, in fedora 43, fedora 44

QDomDocument πŸ’– (3qt)

okay bestie, gather round, because today we're talking about QDomDocument from the Qt3 manual pages (section 3qt, iykyk) β€” the class that Represents an XML document. yes. the whole document. she's the main character of your XML tree and she knows it. πŸ’…


✨ NAME ✨

QDomDocument β€” Represents an XML document 🌈


πŸ¦‹ SYNOPSIS πŸ¦‹

All the functions in this class are reentrant when Qt is built with thread support.</p>

not me pointing out that little stray </p> that's just... sitting there in the original. iconic behavior honestly. leaving it exactly where I found it. πŸŽ€
#include <qdom.h>

Inherits QDomNode.

πŸ’œ Public Members πŸ’œ

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

🌸 DESCRIPTION 🌸

The QDomDocument class represents an XML document. πŸŽ€

So like, QDomDocument is the entire XML document. Conceptually she's the root of the document tree, and she's your primary access point to everything the document's got going on. main character energy fr. πŸ‘‘

And here's the thing β€” elements, text nodes, comments, processing instructions, all of that? They literally cannot exist outside the context of a document. So the document class also has all the factory functions you need to make those lil objects. Every node object you create has an ownerDocument() function that ties it back to the document it was born in. The DOM classes you're gonna be using the most are QDomNode, QDomDocument, QDomElement and QDomText. ✨

The parsed XML lives internally as a tree of objects you can reach through the various QDom classes. And heads up β€” all QDom classes only reference objects in that internal tree. The internal objects in the DOM tree get deleted once the last QDom object referencing them and the QDomDocument itself are deleted. so like, nobody's leaving until everybody's left, basically. πŸ’€

Creating elements, text nodes, etc. is done with the various factory functions on this class. and ngl, if you just use the default constructors of the QDom classes, you get empty objects that you can't manipulate and can't insert into the Document. not slay. 🚫

QDomDocument has a bunch of functions for creating document data β€” for example, createElement(), createTextNode(), createComment(), createCDATASection(), createProcessingInstruction(), createAttribute() and createEntityReference(). Some of these have namespace-supporting versions too, aka createElementNS() and createAttributeNS(). And createDocumentFragment() is the one that holds parts of the document β€” super useful when you're doing complex document manipulation. 🧚

The entire content of the document gets set with setContent(). That function parses whatever string you hand it as an XML document and builds the DOM tree representing it. Root element? That's documentElement(). Textual representation of the document? That's toString(). 🫢

You can also insert a node from another document into this one using importNode().

And if you want a list of every element with a particular tag, you've got elementsByTagName() or elementsByTagNameNS(). ⭐

The QDom classes are typically used like this:

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. poof. gone. 🌈

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 info about the Document Object Model, peep http://www.w3.org/TR/REC-DOM-Level-1/ and http://www.w3.org/TR/DOM-Level-2-Core/. And for a more general intro to the DOM implementation, see the QDomDocument documentation. πŸ’–

See also XML.


πŸ‘‘ MEMBER FUNCTION DOCUMENTATION πŸ‘‘

πŸ’… QDomDocument::QDomDocument ()

Constructs an empty document. blank slate bestie, she's got nothing in her yet. ✨

πŸ’… explicit QDomDocument::QDomDocument ( const QString & name )

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

πŸ’… explicit 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 (shallow copy): modifying one node will also change the other. If you want to make a deep copy, use cloneNode().

so like, they're holding hands. change one, you change both. 🫢 if that's not the vibe, cloneNode() it.

πŸ’… QDomDocument::~QDomDocument ()

Destroys the object and frees its resources. πŸ’€

πŸ’… QDomAttr QDomDocument::createAttribute ( const QString & 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 ( 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, e.g. using QDomNode::appendChild().

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

πŸ’… QDomComment QDomDocument::createComment ( const QString & 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 ( const QString & 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 ( 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 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 ( 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 target and the data to data.

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

QDomText QDomDocument::createTextNode ( const QString & value )

okay so bestie, this one makes a text node out of the string value so you can yeet it into the document tree β€” like with QDomNode::appendChild(). πŸ’…

⚠️ Warning: every single character in an XML document has to live in this range: text #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]

That rule is giving for real, and it also applies to characters encoded as character entities and characters in CDATA sections. If you use this function to insert characters outside that range, the document will not be well-formed. 😭

If you wanna store binary data in an XML document, you gotta either use your own scheme to escape illegal characters, or store it in an external unparsed entity. No cap.

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


QDomDocumentType QDomDocument::doctype () const

Returns the document type of this document. Simple as. πŸ‘‘


QDomElement QDomDocument::documentElement () const

Returns the root element of the document. The main character of the tree, periodt. 🌳✨


QDomElement QDomDocument::elementById ( const QString & elementId )

Returns the element whose ID equals elementId. If no element with that ID is found, you get a null element. πŸ™ˆ

But lowkey? Since the QDomClasses do not know which attributes are element IDs, this function always returns a null element. This may change in a future version. So rn it's giving "not yet, bestie". πŸ’€


QDomNodeList QDomDocument::elementsByTagName ( const QString & 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. So it's a whole squad, in tree order. 🌈

See also: elementsByTagNameNS() and QDomElement::elementsByTagName(). πŸ’–


QDomNodeList QDomDocument::elementsByTagNameNS ( const QString & nsURI, const QString & localName )

Returns a QDomNodeList that contains all the elements in the document with the local name localName and a namespace URI of nsURI. Again, 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. That's it, that's the tweet. πŸ’…


QDomNode QDomDocument::importNode ( const QDomNode & importedNode, bool deep )

Imports the node importedNode from another document into this document. The importedNode stays in the original document β€” this function creates a copy that can be used within this document. So it's not moving out, it's making a clone. πŸ‘―

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. So sorry bestie, those two are not invited. 🚫

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. So for those two, the vibes are fixed. πŸ”’

The behavior of this function is slightly different depending on the node types: <center>.nf </center>.

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


bool QDomDocument::isDocument () const [virtual]

Returns TRUE. Yes queen, it is a document. πŸ‘‘

Reimplemented from QDomNode. πŸ’–


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

Returns DocumentNode. That's the type, periodt. πŸ“„

Reimplemented from QDomNode. ✨


QDomDocument & QDomDocument::operator= ( const QDomDocument & 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(). So be careful β€” it's giving "same brain cell" energy. 🧠


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 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. So it's reading the vibes to figure out the encoding. πŸ”

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 in *errorLine and the column number in *errorColumn. So you get the tea on what went wrong. 🍡

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. So yeah, null vibes. πŸ’€

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. πŸ“–

⚠️ Warning: This function does not try to detect the encoding: instead it assumes that the C string is UTF-8 encoded. So no guessing games here. πŸ”’

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. πŸ’…

This function reads the XML document from the string text. Since text is already a Unicode string, no encoding detection is done. So it's already in the language, no translation needed. 🌍


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. So plug in your device and go. πŸ”Œ


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 performed. 🚫

⚠️ Warning: This function does not try to detect the encoding: instead it assumes that the C string is UTF-8 encoded. Again, no detection. πŸ”’

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 performed. 🚫


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. Since text is already a Unicode string, no encoding detection is performed. 🌈

No namespace processing is performed either. 🚫


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 performed. 🚫


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. πŸ’…

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. So you do you, boo. πŸ’

See also: QXmlSimpleReader. ✨


QCString QDomDocument::toCString () const

Converts the parsed document back to its textual representation and returns a QCString for that is encoded in UTF-8. So it's giving "back to text, but make it UTF-8". πŸ”„

See also: toString(). πŸ’–


QCString QDomDocument::toCString ( int indent ) const

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

This function uses indent as the amount of space to indent subelements. So you can make it pretty. πŸ’…


QString QDomDocument::toString () const

Converts the parsed document back to its textual representation. πŸ“

See also: toCString(). 🫢


QString QDomDocument::toString ( int indent ) const

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

This function uses indent as the amount of space to indent subelements. Pretty printing, slay. ✨


🌟 SEE ALSO 🌟

Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. See the license file included in the distribution for a complete license statement.

✨ AUTHOR ✨

Generated automatically from the source code. So it's a bot, but make it fashion. πŸ€–πŸ’…

πŸ› BUGS πŸ›

If you find a bug in Qt, please report it as described in http://doc.trolltech.com/bughowto.html. Good bug reports help us to help you. Thank you. πŸ™

The definitive Qt documentation is provided in HTML format; it is located at $QTDIR/doc/html and can be read using Qt 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. So like, it's a vibe, not the official source. πŸ“š

If you find errors in this manual page, please report them to qt-bugs@trolltech.com. Please include the name of the manual page (qdomdocument.3qt) and the Qt version (3.3.8). πŸ›


This page was brought to you by Trolltech AS, dated 2 February 2007, for QDomDocument(3qt). Stay slay, bestie. πŸ’–βœ¨

πŸ“¦ where this page comes from