Qt 4.8
Public Functions | Private Functions | Friends | List of all members
QDomDocument Class Reference

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

#include <qdom.h>

Inheritance diagram for QDomDocument:
QDomNode

Public Functions

QDomAttr createAttribute (const QString &name)
 Creates a new attribute called name that can be inserted into an element, e.g. More...
 
QDomAttr createAttributeNS (const QString &nsURI, const QString &qName)
 Creates a new attribute with namespace support that can be inserted into an element. More...
 
QDomCDATASection createCDATASection (const QString &data)
 Creates a new CDATA section for the string value that can be inserted into the document, e.g. More...
 
QDomComment createComment (const QString &data)
 Creates a new comment for the string value that can be inserted into the document, e.g. More...
 
QDomDocumentFragment createDocumentFragment ()
 Creates a new document fragment, that can be used to hold parts of the document, e.g. More...
 
QDomElement createElement (const QString &tagName)
 Creates a new element called tagName that can be inserted into the DOM tree, e.g. More...
 
QDomElement createElementNS (const QString &nsURI, const QString &qName)
 Creates a new element with namespace support that can be inserted into the DOM tree. More...
 
QDomEntityReference createEntityReference (const QString &name)
 Creates a new entity reference called name that can be inserted into the document, e.g. More...
 
QDomProcessingInstruction createProcessingInstruction (const QString &target, const QString &data)
 Creates a new processing instruction that can be inserted into the document, e.g. More...
 
QDomText createTextNode (const QString &data)
 Creates a text node for the string value that can be inserted into the document tree, e.g. More...
 
QDomDocumentType doctype () const
 Returns the document type of this document. More...
 
QDomElement documentElement () const
 Returns the root element of the document. More...
 
QDomElement elementById (const QString &elementId)
 Returns the element whose ID is equal to elementId. More...
 
QDomNodeList elementsByTagName (const QString &tagname) const
 Returns a QDomNodeList, that contains all the elements in the document with the name tagname. More...
 
QDomNodeList 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. More...
 
QDomImplementation implementation () const
 Returns a QDomImplementation object. More...
 
QDomNode importNode (const QDomNode &importedNode, bool deep)
 Imports the node importedNode from another document to this document. More...
 
QDomNode::NodeType nodeType () const
 Returns DocumentNode. More...
 
QDomDocumentoperator= (const QDomDocument &)
 Assigns x to this DOM document. More...
 
 QDomDocument ()
 Constructs an empty document. More...
 
 QDomDocument (const QString &name)
 Creates a document and sets the name of the document type to name. More...
 
 QDomDocument (const QDomDocumentType &doctype)
 Creates a document with the document type doctype. More...
 
 QDomDocument (const QDomDocument &x)
 Constructs a copy of x. More...
 
bool setContent (const QByteArray &text, 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)
 This function reads the XML document from the string text, returning true if the content was successfully parsed; otherwise returns false. More...
 
bool setContent (QIODevice *dev, bool namespaceProcessing, QString *errorMsg=0, int *errorLine=0, int *errorColumn=0)
 This function reads the XML document from the IO device dev, returning true if the content was successfully parsed; otherwise returns false. More...
 
bool setContent (QXmlInputSource *source, bool namespaceProcessing, QString *errorMsg=0, int *errorLine=0, int *errorColumn=0)
 This function reads the XML document from the QXmlInputSource source, returning true if the content was successfully parsed; otherwise returns false. More...
 
bool setContent (const QByteArray &text, QString *errorMsg=0, int *errorLine=0, int *errorColumn=0)
 This function reads the XML document from the byte array buffer, returning true if the content was successfully parsed; otherwise returns false. More...
 
bool setContent (const QString &text, QString *errorMsg=0, int *errorLine=0, int *errorColumn=0)
 This function reads the XML document from the string text, returning true if the content was successfully parsed; otherwise returns false. More...
 
bool setContent (QIODevice *dev, QString *errorMsg=0, int *errorLine=0, int *errorColumn=0)
 This function reads the XML document from the IO device dev, returning true if the content was successfully parsed; otherwise returns false. More...
 
bool setContent (QXmlInputSource *source, QXmlReader *reader, QString *errorMsg=0, int *errorLine=0, int *errorColumn=0)
 This function reads the XML document from the QXmlInputSource source and parses it with the QXmlReader reader, returning true if the content was successfully parsed; otherwise returns false. More...
 
QByteArray toByteArray (int=1) const
 Converts the parsed document back to its textual representation and returns a QByteArray containing the data encoded as UTF-8. More...
 
QString toString (int=1) const
 Converts the parsed document back to its textual representation. More...
 
 ~QDomDocument ()
 Destroys the object and frees its resources. More...
 
- Public Functions inherited from QDomNode
QDomNode appendChild (const QDomNode &newChild)
 Appends newChild as the node's last child. More...
 
QDomNamedNodeMap attributes () const
 Returns a named node map of all attributes. More...
 
QDomNodeList childNodes () const
 Returns a list of all direct child nodes. More...
 
void clear ()
 Converts the node into a null node; if it was not a null node before, its type and contents are deleted. More...
 
QDomNode cloneNode (bool deep=true) const
 Creates a deep (not shallow) copy of the QDomNode. More...
 
int columnNumber () const
 For nodes created by QDomDocument::setContent(), this function returns the column number in the XML document where the node was parsed. More...
 
QDomNode firstChild () const
 Returns the first child of the node. More...
 
QDomElement firstChildElement (const QString &tagName=QString()) const
 Returns the first child element with tag name tagName if tagName is non-empty; otherwise returns the first child element. More...
 
bool hasAttributes () const
 Returns true if the node has attributes; otherwise returns false. More...
 
bool hasChildNodes () const
 Returns true if the node has one or more children; otherwise returns false. More...
 
QDomNode insertAfter (const QDomNode &newChild, const QDomNode &refChild)
 Inserts the node newChild after the child node refChild. More...
 
QDomNode insertBefore (const QDomNode &newChild, const QDomNode &refChild)
 Inserts the node newChild before the child node refChild. More...
 
bool isAttr () const
 Returns true if the node is an attribute; otherwise returns false. More...
 
bool isCDATASection () const
 Returns true if the node is a CDATA section; otherwise returns false. More...
 
bool isCharacterData () const
 Returns true if the node is a character data node; otherwise returns false. More...
 
bool isComment () const
 Returns true if the node is a comment; otherwise returns false. More...
 
bool isDocument () const
 Returns true if the node is a document; otherwise returns false. More...
 
bool isDocumentFragment () const
 Returns true if the node is a document fragment; otherwise returns false. More...
 
bool isDocumentType () const
 Returns true if the node is a document type; otherwise returns false. More...
 
bool isElement () const
 Returns true if the node is an element; otherwise returns false. More...
 
bool isEntity () const
 Returns true if the node is an entity; otherwise returns false. More...
 
bool isEntityReference () const
 Returns true if the node is an entity reference; otherwise returns false. More...
 
bool isNotation () const
 Returns true if the node is a notation; otherwise returns false. More...
 
bool isNull () const
 Returns true if this node is null (i.e. More...
 
bool isProcessingInstruction () const
 Returns true if the node is a processing instruction; otherwise returns false. More...
 
bool isSupported (const QString &feature, const QString &version) const
 Returns true if the DOM implementation implements the feature feature and this feature is supported by this node in the version version; otherwise returns false. More...
 
bool isText () const
 Returns true if the node is a text node; otherwise returns false. More...
 
QDomNode lastChild () const
 Returns the last child of the node. More...
 
QDomElement lastChildElement (const QString &tagName=QString()) const
 Returns the last child element with tag name tagName if tagName is non-empty; otherwise returns the last child element. More...
 
int lineNumber () const
 For nodes created by QDomDocument::setContent(), this function returns the line number in the XML document where the node was parsed. More...
 
QString localName () const
 If the node uses namespaces, this function returns the local name of the node; otherwise it returns an empty string. More...
 
QDomNode namedItem (const QString &name) const
 Returns the first direct child node for which nodeName() equals name. More...
 
QString namespaceURI () const
 Returns the namespace URI of this node or an empty string if the node has no namespace URI. More...
 
QDomNode nextSibling () const
 Returns the next sibling in the document tree. More...
 
QDomElement nextSiblingElement (const QString &taName=QString()) const
 Returns the next sibling element with tag name tagName if tagName is non-empty; otherwise returns any next sibling element. More...
 
QString nodeName () const
 Returns the name of the node. More...
 
NodeType nodeType () const
 Returns the type of the node. More...
 
QString nodeValue () const
 Returns the value of the node. More...
 
void normalize ()
 Calling normalize() on an element converts all its children into a standard form. More...
 
bool operator!= (const QDomNode &) const
 Returns true if n and this DOM node are not equal; otherwise returns false. More...
 
QDomNodeoperator= (const QDomNode &)
 Assigns a copy of n to this DOM node. More...
 
bool operator== (const QDomNode &) const
 Returns true if n and this DOM node are equal; otherwise returns false. More...
 
QDomDocument ownerDocument () const
 Returns the document to which this node belongs. More...
 
QDomNode parentNode () const
 Returns the parent node. More...
 
QString prefix () const
 Returns the namespace prefix of the node or an empty string if the node has no namespace prefix. More...
 
QDomNode previousSibling () const
 Returns the previous sibling in the document tree. More...
 
QDomElement previousSiblingElement (const QString &tagName=QString()) const
 Returns the previous sibilng element with tag name tagName if tagName is non-empty; otherwise returns any previous sibling element. More...
 
 QDomNode ()
 Constructs a null node. More...
 
 QDomNode (const QDomNode &)
 Constructs a copy of n. More...
 
QDomNode removeChild (const QDomNode &oldChild)
 Removes oldChild from the list of children. More...
 
QDomNode replaceChild (const QDomNode &newChild, const QDomNode &oldChild)
 Replaces oldChild with newChild. More...
 
void save (QTextStream &, int) const
 Writes the XML representation of the node and all its children to the stream str. More...
 
void save (QTextStream &, int, EncodingPolicy) const
 If encodingPolicy is QDomNode::EncodingFromDocument, this function behaves as save(QTextStream &str, int indent). More...
 
void setNodeValue (const QString &)
 Sets the node's value to v. More...
 
void setPrefix (const QString &pre)
 If the node has a namespace prefix, this function changes the namespace prefix of the node to pre. More...
 
QDomAttr toAttr () const
 Converts a QDomNode into a QDomAttr. More...
 
QDomCDATASection toCDATASection () const
 Converts a QDomNode into a QDomCDATASection. More...
 
QDomCharacterData toCharacterData () const
 Converts a QDomNode into a QDomCharacterData. More...
 
QDomComment toComment () const
 Converts a QDomNode into a QDomComment. More...
 
QDomDocument toDocument () const
 Converts a QDomNode into a QDomDocument. More...
 
QDomDocumentFragment toDocumentFragment () const
 Converts a QDomNode into a QDomDocumentFragment. More...
 
QDomDocumentType toDocumentType () const
 Converts a QDomNode into a QDomDocumentType. More...
 
QDomElement toElement () const
 Converts a QDomNode into a QDomElement. More...
 
QDomEntity toEntity () const
 Converts a QDomNode into a QDomEntity. More...
 
QDomEntityReference toEntityReference () const
 Converts a QDomNode into a QDomEntityReference. More...
 
QDomNotation toNotation () const
 Converts a QDomNode into a QDomNotation. More...
 
QDomProcessingInstruction toProcessingInstruction () const
 Converts a QDomNode into a QDomProcessingInstruction. More...
 
QDomText toText () const
 Converts a QDomNode into a QDomText. More...
 
 ~QDomNode ()
 Destroys the object and frees its resources. More...
 

Private Functions

 QDomDocument (QDomDocumentPrivate *)
 

Friends

class QDomNode
 

Additional Inherited Members

- Public Types inherited from QDomNode
enum  EncodingPolicy { EncodingFromDocument = 1, EncodingFromTextStream = 2 }
 This enum specifies how QDomNode::save() determines what encoding to use when serializing. More...
 
enum  NodeType {
  ElementNode = 1, AttributeNode = 2, TextNode = 3, CDATASectionNode = 4,
  EntityReferenceNode = 5, EntityNode = 6, ProcessingInstructionNode = 7, CommentNode = 8,
  DocumentNode = 9, DocumentTypeNode = 10, DocumentFragmentNode = 11, NotationNode = 12,
  BaseNode = 21, CharacterDataNode = 22
}
 This enum defines the type of the node: More...
 
- Protected Functions inherited from QDomNode
 QDomNode (QDomNodePrivate *)
 Constructs a new node for the data n. More...
 
- Protected Variables inherited from QDomNode
QDomNodePrivateimpl
 

Detailed Description

The QDomDocument class represents an XML document.

Note
This class or function is reentrant.
Attention
Module: QtXml

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

Note
The DOM tree might end up reserving a lot of memory if the XML document is big. For big XML documents, the QXmlStreamReader or the QXmlQuery classes might be better solutions.

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(QIODevice::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 << qPrintable(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 the Document Object Model (DOM) Level 1 and Level 2 Core Specifications.

See also
{DOM Bookmarks Example}, {Simple DOM Model Example}

Definition at line 308 of file qdom.h.

Constructors and Destructors

◆ QDomDocument() [1/5]

QDomDocument::QDomDocument ( )

Constructs an empty document.

Definition at line 6751 of file qdom.cpp.

Referenced by QDomNode::toDocument().

6752 {
6753  impl = 0;
6754 }
QDomNodePrivate * impl
Definition: qdom.h:243

◆ QDomDocument() [2/5]

QDomDocument::QDomDocument ( const QString name)
explicit

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

Definition at line 6760 of file qdom.cpp.

6761 {
6762  // We take over ownership
6763  impl = new QDomDocumentPrivate(name);
6764 }
QDomNodePrivate * impl
Definition: qdom.h:243

◆ QDomDocument() [3/5]

QDomDocument::QDomDocument ( const QDomDocumentType doctype)
explicit

Creates a document with the document type doctype.

See also
QDomImplementation::createDocumentType()

Definition at line 6771 of file qdom.cpp.

6772 {
6774 }
QDomNodePrivate * impl
Definition: qdom.h:243

◆ QDomDocument() [4/5]

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

Definition at line 6783 of file qdom.cpp.

6784  : QDomNode(x)
6785 {
6786 }
QDomNode()
Constructs a null node.
Definition: qdom.cpp:2057

◆ ~QDomDocument()

QDomDocument::~QDomDocument ( )

Destroys the object and frees its resources.

Definition at line 6808 of file qdom.cpp.

6809 {
6810 }

◆ QDomDocument() [5/5]

QDomDocument::QDomDocument ( QDomDocumentPrivate x)
private

Definition at line 6788 of file qdom.cpp.

6789  : QDomNode(x)
6790 {
6791 }
QDomNode()
Constructs a null node.
Definition: qdom.cpp:2057

Functions

◆ createAttribute()

QDomAttr QDomDocument::createAttribute ( const QString name)

Creates a new attribute called name that can be inserted into an element, e.g.

using QDomElement::setAttributeNode().

If name is not a valid XML name, the behavior of this function is governed by QDomImplementation::InvalidDataPolicy.

See also
createAttributeNS()

Definition at line 7180 of file qdom.cpp.

7181 {
7182  if (!impl)
7183  impl = new QDomDocumentPrivate();
7184  return QDomAttr(IMPL->createAttribute(name));
7185 }
QDomNodePrivate * impl
Definition: qdom.h:243
#define IMPL
Definition: qdom.cpp:6666
The QDomAttr class represents one attribute of a QDomElement.
Definition: qdom.h:449

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

If qName is not a valid XML name, the behavior of this function is governed by QDomImplementation::InvalidDataPolicy.

See also
createAttribute()

Definition at line 7320 of file qdom.cpp.

7321 {
7322  if (!impl)
7323  impl = new QDomDocumentPrivate();
7324  return QDomAttr(IMPL->createAttributeNS(nsURI, qName));
7325 }
QDomNodePrivate * impl
Definition: qdom.h:243
#define IMPL
Definition: qdom.cpp:6666
The QDomAttr class represents one attribute of a QDomElement.
Definition: qdom.h:449

◆ createCDATASection()

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

If value contains characters which cannot be stored in a CDATA section, the behavior of this function is governed by QDomImplementation::InvalidDataPolicy.

See also
QDomNode::appendChild() QDomNode::insertBefore() QDomNode::insertAfter()

Definition at line 7143 of file qdom.cpp.

7144 {
7145  if (!impl)
7146  impl = new QDomDocumentPrivate();
7147  return QDomCDATASection(IMPL->createCDATASection(value));
7148 }
QDomNodePrivate * impl
Definition: qdom.h:243
#define IMPL
Definition: qdom.cpp:6666
The QDomCDATASection class represents an XML CDATA section.
Definition: qdom.h:572

◆ createComment()

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

If value contains characters which cannot be stored in an XML comment, the behavior of this function is governed by QDomImplementation::InvalidDataPolicy.

See also
QDomNode::appendChild() QDomNode::insertBefore() QDomNode::insertAfter()

Definition at line 7126 of file qdom.cpp.

7127 {
7128  if (!impl)
7129  impl = new QDomDocumentPrivate();
7130  return QDomComment(IMPL->createComment(value));
7131 }
QDomNodePrivate * impl
Definition: qdom.h:243
#define IMPL
Definition: qdom.cpp:6666
The QDomComment class represents an XML comment.
Definition: qdom.h:555

◆ createDocumentFragment()

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.

Definition at line 7093 of file qdom.cpp.

7094 {
7095  if (!impl)
7096  impl = new QDomDocumentPrivate();
7097  return QDomDocumentFragment(IMPL->createDocumentFragment());
7098 }
The QDomDocumentFragment class is a tree of QDomNodes which is not usually a complete QDomDocument...
Definition: qdom.h:399
QDomNodePrivate * impl
Definition: qdom.h:243
#define IMPL
Definition: qdom.cpp:6666

◆ createElement()

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

If tagName is not a valid XML name, the behavior of this function is governed by QDomImplementation::InvalidDataPolicy.

See also
createElementNS() QDomNode::appendChild() QDomNode::insertBefore() QDomNode::insertAfter()

Definition at line 7081 of file qdom.cpp.

7082 {
7083  if (!impl)
7084  impl = new QDomDocumentPrivate();
7085  return QDomElement(IMPL->createElement(tagName));
7086 }
QDomNodePrivate * impl
Definition: qdom.h:243
#define IMPL
Definition: qdom.cpp:6666
The QDomElement class represents one element in the DOM tree.
Definition: qdom.h:476

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

If qName is an empty string, returns a null element regardless of whether the invalid data policy is set.

See also
createElement()

Definition at line 7301 of file qdom.cpp.

Referenced by QDomImplementation::createDocument().

7302 {
7303  if (!impl)
7304  impl = new QDomDocumentPrivate();
7305  return QDomElement(IMPL->createElementNS(nsURI, qName));
7306 }
QDomNodePrivate * impl
Definition: qdom.h:243
#define IMPL
Definition: qdom.cpp:6666
The QDomElement class represents one element in the DOM tree.
Definition: qdom.h:476

◆ createEntityReference()

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

If name is not a valid XML name, the behavior of this function is governed by QDomImplementation::InvalidDataPolicy.

See also
QDomNode::appendChild() QDomNode::insertBefore() QDomNode::insertAfter()

Definition at line 7196 of file qdom.cpp.

7197 {
7198  if (!impl)
7199  impl = new QDomDocumentPrivate();
7200  return QDomEntityReference(IMPL->createEntityReference(name));
7201 }
QDomNodePrivate * impl
Definition: qdom.h:243
#define IMPL
Definition: qdom.cpp:6666
The QDomEntityReference class represents an XML entity reference.
Definition: qdom.h:631

◆ createProcessingInstruction()

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.

If target is not a valid XML name, or data if contains characters which cannot appear in a processing instruction, the behavior of this function is governed by QDomImplementation::InvalidDataPolicy.

See also
QDomNode::appendChild() QDomNode::insertBefore() QDomNode::insertAfter()

Definition at line 7162 of file qdom.cpp.

7164 {
7165  if (!impl)
7166  impl = new QDomDocumentPrivate();
7167  return QDomProcessingInstruction(IMPL->createProcessingInstruction(target, data));
7168 }
The QDomProcessingInstruction class represents an XML processing instruction.
Definition: qdom.h:648
QDomNodePrivate * impl
Definition: qdom.h:243
#define IMPL
Definition: qdom.cpp:6666

◆ createTextNode()

QDomText QDomDocument::createTextNode ( const QString value)

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

using QDomNode::appendChild().

If value contains characters which cannot be stored as character data of an XML document (even in the form of character references), the behavior of this function is governed by QDomImplementation::InvalidDataPolicy.

See also
QDomNode::appendChild() QDomNode::insertBefore() QDomNode::insertAfter()

Definition at line 7110 of file qdom.cpp.

7111 {
7112  if (!impl)
7113  impl = new QDomDocumentPrivate();
7114  return QDomText(IMPL->createTextNode(value));
7115 }
The QDomText class represents text data in the parsed XML document.
Definition: qdom.h:534
QDomNodePrivate * impl
Definition: qdom.h:243
#define IMPL
Definition: qdom.cpp:6666

◆ doctype()

QDomDocumentType QDomDocument::doctype ( ) const

Returns the document type of this document.

Definition at line 7044 of file qdom.cpp.

7045 {
7046  if (!impl)
7047  return QDomDocumentType();
7048  return QDomDocumentType(IMPL->doctype());
7049 }
friend class QDomDocumentType
Definition: qdom.h:248
QDomNodePrivate * impl
Definition: qdom.h:243
#define IMPL
Definition: qdom.cpp:6666

◆ documentElement()

QDomElement QDomDocument::documentElement ( ) const

Returns the root element of the document.

Definition at line 7064 of file qdom.cpp.

7065 {
7066  if (!impl)
7067  return QDomElement();
7068  return QDomElement(IMPL->documentElement());
7069 }
QDomNodePrivate * impl
Definition: qdom.h:243
#define IMPL
Definition: qdom.cpp:6666
The QDomElement class represents one element in the DOM tree.
Definition: qdom.h:476

◆ elementById()

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 QDomClasses do not know which attributes are element IDs, this function returns always a null element. This may change in a future version.

Definition at line 7349 of file qdom.cpp.

7350 {
7351  qWarning("elementById() is not implemented and will always return a null node.");
7352  return QDomElement();
7353 }
Q_CORE_EXPORT void qWarning(const char *,...)
The QDomElement class represents one element in the DOM tree.
Definition: qdom.h:476

◆ elementsByTagName()

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.

See also
elementsByTagNameNS() QDomElement::elementsByTagName()

Definition at line 7211 of file qdom.cpp.

7212 {
7213  return QDomNodeList(new QDomNodeListPrivate(impl, tagname));
7214 }
friend class QDomNodeList
Definition: qdom.h:249
QDomNodePrivate * impl
Definition: qdom.h:243

◆ elementsByTagNameNS()

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.

The order of the node list is the order they are encountered in a preorder traversal of the element tree.

See also
elementsByTagName() QDomElement::elementsByTagNameNS()

Definition at line 7335 of file qdom.cpp.

7336 {
7337  return QDomNodeList(new QDomNodeListPrivate(impl, nsURI, localName));
7338 }
friend class QDomNodeList
Definition: qdom.h:249
QDomNodePrivate * impl
Definition: qdom.h:243

◆ implementation()

QDomImplementation QDomDocument::implementation ( ) const

Returns a QDomImplementation object.

Definition at line 7054 of file qdom.cpp.

7055 {
7056  if (!impl)
7057  return QDomImplementation();
7058  return QDomImplementation(IMPL->implementation());
7059 }
QDomNodePrivate * impl
Definition: qdom.h:243
#define IMPL
Definition: qdom.cpp:6666
The QDomImplementation class provides information about the features of the DOM implementation.
Definition: qdom.h:99

◆ importNode()

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

Node Type Behavior
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 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 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() QDomNode::appendChild()

Definition at line 7282 of file qdom.cpp.

7283 {
7284  if (!impl)
7285  impl = new QDomDocumentPrivate();
7286  return QDomNode(IMPL->importNode(importedNode.impl, deep));
7287 }
QDomNode()
Constructs a null node.
Definition: qdom.cpp:2057
QDomNodePrivate * impl
Definition: qdom.h:243
#define IMPL
Definition: qdom.cpp:6666

◆ nodeType()

QDomNode::NodeType QDomDocument::nodeType ( ) const
inline

Returns DocumentNode.

Definition at line 340 of file qdom.h.

340 { return DocumentNode; }

◆ operator=()

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

Definition at line 6800 of file qdom.cpp.

6801 {
6802  return (QDomDocument&) QDomNode::operator=(x);
6803 }
QDomNode & operator=(const QDomNode &)
Assigns a copy of n to this DOM node.
Definition: qdom.cpp:2096
The QDomDocument class represents an XML document.
Definition: qdom.h:308

◆ setContent() [1/8]

bool QDomDocument::setContent ( const QByteArray data,
bool  namespaceProcessing,
QString errorMsg = 0,
int *  errorLine = 0,
int *  errorColumn = 0 
)
Note
This class or function is not reentrant.

This function parses the XML document from the byte array data 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, this function returns false and the error message is placed in *{errorMsg}, the line number in *{errorLine} and the column number in *{errorColumn} (unless the associated pointer is set to 0); otherwise this function returns true. The various error messages are described in the QXmlParseException class documentation. Note that, if you want to display these error messages to your application's users, they will be displayed in English unless they are explicitly translated.

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.

Text nodes consisting only of whitespace are stripped and won't appear in the QDomDocument. If this behavior is not desired, one can use the setContent() overload that allows a QXmlReader to be supplied.

If namespaceProcessing is false, the functions QDomNode::prefix(), QDomNode::localName() and QDomNode::namespaceURI() return an empty string.

Entity references are handled as follows:

  • References to internal general entities and character entities occurring in the content are included. The result is a QDomText node with the references replaced by their corresponding entity values.
  • References to parameter entities occurring in the internal subset are included. The result is a QDomDocumentType node which contains entity and notation declarations with the references replaced by their corresponding entity values.
  • Any general parsed entity reference which is not defined in the internal subset and which occurs in the content is represented as a QDomEntityReference node.
  • Any parsed entity reference which is not defined in the internal subset and which occurs outside of the content is replaced with an empty string.
  • Any unparsed entity reference is replaced with an empty string.
See also
QDomNode::namespaceURI() QDomNode::localName() QDomNode::prefix() QString::isNull() QString::isEmpty()

Definition at line 6887 of file qdom.cpp.

Referenced by QDBusXmlParser::QDBusXmlParser(), and setContent().

6888 {
6889  if (!impl)
6890  impl = new QDomDocumentPrivate();
6891  QBuffer buf;
6892  buf.setData(data);
6893  QXmlInputSource source(&buf);
6894  return IMPL->setContent(&source, namespaceProcessing, errorMsg, errorLine, errorColumn);
6895 }
void setData(const QByteArray &data)
Sets the contents of the internal buffer to be data.
Definition: qbuffer.cpp:315
The QBuffer class provides a QIODevice interface for a QByteArray.
Definition: qbuffer.h:57
QDomNodePrivate * impl
Definition: qdom.h:243
#define IMPL
Definition: qdom.cpp:6666
The QXmlInputSource class provides the input data for the QXmlReader subclasses.
Definition: qxml.h:158

◆ setContent() [2/8]

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

This function reads the XML document from the string text, returning true if the content was successfully parsed; otherwise returns false.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Since text is already a Unicode string, no encoding detection is done.

Definition at line 6823 of file qdom.cpp.

6824 {
6825  if (!impl)
6826  impl = new QDomDocumentPrivate();
6827  QXmlInputSource source;
6828  source.setData(text);
6829  return IMPL->setContent(&source, namespaceProcessing, errorMsg, errorLine, errorColumn);
6830 }
virtual void setData(const QString &dat)
Sets the data of the input source to dat.
Definition: qxml.cpp:1509
QDomNodePrivate * impl
Definition: qdom.h:243
#define IMPL
Definition: qdom.cpp:6666
The QXmlInputSource class provides the input data for the QXmlReader subclasses.
Definition: qxml.h:158

◆ setContent() [3/8]

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

This function reads the XML document from the IO device dev, returning true if the content was successfully parsed; otherwise returns false.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 6906 of file qdom.cpp.

6907 {
6908  if (!impl)
6909  impl = new QDomDocumentPrivate();
6910  QXmlInputSource source(dev);
6911  return IMPL->setContent(&source, namespaceProcessing, errorMsg, errorLine, errorColumn);
6912 }
QDomNodePrivate * impl
Definition: qdom.h:243
#define IMPL
Definition: qdom.cpp:6666
The QXmlInputSource class provides the input data for the QXmlReader subclasses.
Definition: qxml.h:158

◆ setContent() [4/8]

bool QDomDocument::setContent ( QXmlInputSource source,
bool  namespaceProcessing,
QString errorMsg = 0,
int *  errorLine = 0,
int *  errorColumn = 0 
)

This function reads the XML document from the QXmlInputSource source, returning true if the content was successfully parsed; otherwise returns false.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Since
4.5

Definition at line 6925 of file qdom.cpp.

6926 {
6927  if (!impl)
6928  impl = new QDomDocumentPrivate();
6929  QXmlSimpleReader reader;
6930  initializeReader(reader, namespaceProcessing);
6931  return IMPL->setContent(source, &reader, errorMsg, errorLine, errorColumn);
6932 }
The QXmlSimpleReader class provides an implementation of a simple XML parser.
Definition: qxml.h:241
QDomNodePrivate * impl
Definition: qdom.h:243
#define IMPL
Definition: qdom.cpp:6666
static void initializeReader(QXmlSimpleReader &reader, bool namespaceProcessing)
Definition: qdom.cpp:6360

◆ setContent() [5/8]

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

This function reads the XML document from the byte array buffer, returning true if the content was successfully parsed; otherwise returns false.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

No namespace processing is performed.

Definition at line 6964 of file qdom.cpp.

6965 {
6966  return setContent(buffer, false, errorMsg, errorLine, errorColumn);
6967 }
bool setContent(const QByteArray &text, bool namespaceProcessing, QString *errorMsg=0, int *errorLine=0, int *errorColumn=0)
Definition: qdom.cpp:6887

◆ setContent() [6/8]

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

This function reads the XML document from the string text, returning true if the content was successfully parsed; otherwise returns false.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Since text is already a Unicode string, no encoding detection is performed.

No namespace processing is performed either.

Definition at line 6947 of file qdom.cpp.

6948 {
6949  return setContent(text, false, errorMsg, errorLine, errorColumn);
6950 }
bool setContent(const QByteArray &text, bool namespaceProcessing, QString *errorMsg=0, int *errorLine=0, int *errorColumn=0)
Definition: qdom.cpp:6887

◆ setContent() [7/8]

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

This function reads the XML document from the IO device dev, returning true if the content was successfully parsed; otherwise returns false.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

No namespace processing is performed.

Definition at line 6980 of file qdom.cpp.

6981 {
6982  return setContent(dev, false, errorMsg, errorLine, errorColumn);
6983 }
bool setContent(const QByteArray &text, bool namespaceProcessing, QString *errorMsg=0, int *errorLine=0, int *errorColumn=0)
Definition: qdom.cpp:6887

◆ setContent() [8/8]

bool QDomDocument::setContent ( QXmlInputSource source,
QXmlReader reader,
QString errorMsg = 0,
int *  errorLine = 0,
int *  errorColumn = 0 
)

This function reads the XML document from the QXmlInputSource source and parses it with the QXmlReader reader, returning true if the content was successfully parsed; otherwise returns false.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

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

See also
QXmlSimpleReader

Definition at line 7001 of file qdom.cpp.

7002 {
7003  if (!impl)
7004  impl = new QDomDocumentPrivate();
7005  return IMPL->setContent(source, reader, errorMsg, errorLine, errorColumn);
7006 }
QDomNodePrivate * impl
Definition: qdom.h:243
#define IMPL
Definition: qdom.cpp:6666

◆ toByteArray()

QByteArray QDomDocument::toByteArray ( int  indent = 1) const

Converts the parsed document back to its textual representation and returns a QByteArray containing the data encoded as UTF-8.

This function uses indent as the amount of space to indent subelements.

See also
toString()

Definition at line 7033 of file qdom.cpp.

7034 {
7035  // ### if there is an encoding specified in the xml declaration, this
7036  // encoding declaration should be changed to utf8
7037  return toString(indent).toUtf8();
7038 }
QByteArray toUtf8() const Q_REQUIRED_RESULT
Returns a UTF-8 representation of the string as a QByteArray.
Definition: qstring.cpp:4074
QString toString(int=1) const
Converts the parsed document back to its textual representation.
Definition: qdom.cpp:7016

◆ toString()

QString QDomDocument::toString ( int  indent = 1) const

Converts the parsed document back to its textual representation.

This function uses indent as the amount of space to indent subelements.

If indent is -1, no whitespace at all is added.

Definition at line 7016 of file qdom.cpp.

Referenced by toByteArray().

7017 {
7018  QString str;
7020  save(s, indent);
7021  return str;
7022 }
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QTextStream class provides a convenient interface for reading and writing text.
Definition: qtextstream.h:73
void save(QTextStream &, int) const
Writes the XML representation of the node and all its children to the stream str. ...
Definition: qdom.cpp:2728

Friends and Related Functions

◆ QDomNode

friend class QDomNode
friend

Definition at line 359 of file qdom.h.


The documentation for this class was generated from the following files: