1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtXml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <qplatformdefs.h>
44 #include "private/qxmlutils_p.h"
51 #include <qiodevice.h>
54 #include <qtextcodec.h>
55 #include <qtextstream.h>
59 #include <qshareddata.h>
66 ### old todo comments -- I don't know if they still apply...
68 If the document dies, remove all pointers to it from children
69 which can not be deleted at this time.
71 If a node dies and has direct children which can not be deleted,
72 then remove the pointer to the parent.
74 createElement and friends create double reference counts.
79 Remove emtpy emthods in the *Private classes
81 Make a lot of the (mostly empty) methods in the public classes inline.
82 Specially constructors assignment operators and comparison operators are candidates.
84 The virtual isXxx functions in *Private can probably be replaced by inline methods checking the nodeType().
91 1) If an intern object returns a pointer to another intern object
92 then the reference count of the returned object is not increased.
93 2) If an extern object is created and gets a pointer to some intern
94 object, then the extern object increases the intern objects reference count.
95 3) If an extern object is deleted, then it decreases the reference count
96 on its associated intern object and deletes it if nobody else hold references
102 Helper to split a qualified name in the prefix and local name.
104 static void qt_split_namespace(QString& prefix, QString& name, const QString& qName, bool hasURI)
106 int i = qName.indexOf(QLatin1Char(':'));
109 prefix = QLatin1String("");
114 prefix = qName.left(i);
115 name = qName.mid(i + 1);
119 /**************************************************************
121 * Private class declerations
123 **************************************************************/
125 class QDomImplementationPrivate
128 inline QDomImplementationPrivate() {}
130 QDomImplementationPrivate* clone();
132 static QDomImplementation::InvalidDataPolicy invalidDataPolicy;
135 class QDomNodePrivate
138 QDomNodePrivate(QDomDocumentPrivate*, QDomNodePrivate* parent = 0);
139 QDomNodePrivate(QDomNodePrivate* n, bool deep);
140 virtual ~QDomNodePrivate();
142 QString nodeName() const { return name; }
143 QString nodeValue() const { return value; }
144 virtual void setNodeValue(const QString& v) { value = v; }
146 QDomDocumentPrivate* ownerDocument();
147 void setOwnerDocument(QDomDocumentPrivate* doc);
149 virtual QDomNodePrivate* insertBefore(QDomNodePrivate* newChild, QDomNodePrivate* refChild);
150 virtual QDomNodePrivate* insertAfter(QDomNodePrivate* newChild, QDomNodePrivate* refChild);
151 virtual QDomNodePrivate* replaceChild(QDomNodePrivate* newChild, QDomNodePrivate* oldChild);
152 virtual QDomNodePrivate* removeChild(QDomNodePrivate* oldChild);
153 virtual QDomNodePrivate* appendChild(QDomNodePrivate* newChild);
155 QDomNodePrivate* namedItem(const QString& name);
157 virtual QDomNodePrivate* cloneNode(bool deep = true);
158 virtual void normalize();
159 virtual void clear();
161 inline QDomNodePrivate* parent() const { return hasParent ? ownerNode : 0; }
162 inline void setParent(QDomNodePrivate *p) { ownerNode = p; hasParent = true; }
165 ownerNode = hasParent ? (QDomNodePrivate*)ownerDocument() : 0;
170 virtual bool isAttr() const { return false; }
171 virtual bool isCDATASection() const { return false; }
172 virtual bool isDocumentFragment() const { return false; }
173 virtual bool isDocument() const { return false; }
174 virtual bool isDocumentType() const { return false; }
175 virtual bool isElement() const { return false; }
176 virtual bool isEntityReference() const { return false; }
177 virtual bool isText() const { return false; }
178 virtual bool isEntity() const { return false; }
179 virtual bool isNotation() const { return false; }
180 virtual bool isProcessingInstruction() const { return false; }
181 virtual bool isCharacterData() const { return false; }
182 virtual bool isComment() const { return false; }
184 virtual QDomNode::NodeType nodeType() const { return QDomNode::BaseNode; }
186 virtual void save(QTextStream&, int, int) const;
188 void setLocation(int lineNumber, int columnNumber);
192 QDomNodePrivate* prev;
193 QDomNodePrivate* next;
194 QDomNodePrivate* ownerNode; // either the node's parent or the node's owner document
195 QDomNodePrivate* first;
196 QDomNodePrivate* last;
198 QString name; // this is the local name if prefix != null
200 QString prefix; // set this only for ElementNode and AttributeNode
201 QString namespaceURI; // set this only for ElementNode and AttributeNode
202 bool createdWithDom1Interface : 1;
209 class QDomNodeListPrivate
212 QDomNodeListPrivate(QDomNodePrivate*);
213 QDomNodeListPrivate(QDomNodePrivate*, const QString& );
214 QDomNodeListPrivate(QDomNodePrivate*, const QString&, const QString& );
215 ~QDomNodeListPrivate();
217 bool operator== (const QDomNodeListPrivate&) const;
218 bool operator!= (const QDomNodeListPrivate&) const;
221 QDomNodePrivate* item(int index);
226 This list contains the children of this node.
228 QDomNodePrivate* node_impl;
231 QList<QDomNodePrivate*> list;
235 class QDomNamedNodeMapPrivate
238 QDomNamedNodeMapPrivate(QDomNodePrivate*);
239 ~QDomNamedNodeMapPrivate();
241 QDomNodePrivate* namedItem(const QString& name) const;
242 QDomNodePrivate* namedItemNS(const QString& nsURI, const QString& localName) const;
243 QDomNodePrivate* setNamedItem(QDomNodePrivate* arg);
244 QDomNodePrivate* setNamedItemNS(QDomNodePrivate* arg);
245 QDomNodePrivate* removeNamedItem(const QString& name);
246 QDomNodePrivate* item(int index) const;
248 bool contains(const QString& name) const;
249 bool containsNS(const QString& nsURI, const QString & localName) const;
252 * Remove all children from the map.
255 bool isReadOnly() { return readonly; }
256 void setReadOnly(bool r) { readonly = r; }
257 bool isAppendToParent() { return appendToParent; }
259 * If true, then the node will redirect insert/remove calls
260 * to its parent by calling QDomNodePrivate::appendChild or removeChild.
261 * In addition the map wont increase or decrease the reference count
262 * of the nodes it contains.
264 * By default this value is false and the map will handle reference counting
267 void setAppendToParent(bool b) { appendToParent = b; }
270 * Creates a copy of the map. It is a deep copy
271 * that means that all children are cloned.
273 QDomNamedNodeMapPrivate* clone(QDomNodePrivate* parent);
277 QHash<QString, QDomNodePrivate *> map;
278 QDomNodePrivate* parent;
283 class QDomDocumentTypePrivate : public QDomNodePrivate
286 QDomDocumentTypePrivate(QDomDocumentPrivate*, QDomNodePrivate* parent = 0);
287 QDomDocumentTypePrivate(QDomDocumentTypePrivate* n, bool deep);
288 ~QDomDocumentTypePrivate();
291 // Reimplemented from QDomNodePrivate
292 QDomNodePrivate* cloneNode(bool deep = true);
293 QDomNodePrivate* insertBefore(QDomNodePrivate* newChild, QDomNodePrivate* refChild);
294 QDomNodePrivate* insertAfter(QDomNodePrivate* newChild, QDomNodePrivate* refChild);
295 QDomNodePrivate* replaceChild(QDomNodePrivate* newChild, QDomNodePrivate* oldChild);
296 QDomNodePrivate* removeChild(QDomNodePrivate* oldChild);
297 QDomNodePrivate* appendChild(QDomNodePrivate* newChild);
299 virtual bool isDocumentType() const { return true; }
300 QDomNode::NodeType nodeType() const { return QDomNode::DocumentTypeNode; }
302 void save(QTextStream& s, int, int) const;
305 QDomNamedNodeMapPrivate* entities;
306 QDomNamedNodeMapPrivate* notations;
309 QString internalSubset;
312 class QDomDocumentFragmentPrivate : public QDomNodePrivate
315 QDomDocumentFragmentPrivate(QDomDocumentPrivate*, QDomNodePrivate* parent = 0);
316 QDomDocumentFragmentPrivate(QDomNodePrivate* n, bool deep);
318 // Reimplemented from QDomNodePrivate
319 virtual QDomNodePrivate* cloneNode(bool deep = true);
320 virtual bool isDocumentFragment() const { return true; }
321 QDomNode::NodeType nodeType() const { return QDomNode::DocumentFragmentNode; }
324 class QDomCharacterDataPrivate : public QDomNodePrivate
327 QDomCharacterDataPrivate(QDomDocumentPrivate*, QDomNodePrivate* parent, const QString& data);
328 QDomCharacterDataPrivate(QDomCharacterDataPrivate* n, bool deep);
330 uint dataLength() const;
331 QString substringData(unsigned long offset, unsigned long count) const;
332 void appendData(const QString& arg);
333 void insertData(unsigned long offset, const QString& arg);
334 void deleteData(unsigned long offset, unsigned long count);
335 void replaceData(unsigned long offset, unsigned long count, const QString& arg);
337 // Reimplemented from QDomNodePrivate
338 virtual bool isCharacterData() const { return true; }
339 QDomNode::NodeType nodeType() const { return QDomNode::CharacterDataNode; }
340 QDomNodePrivate* cloneNode(bool deep = true);
343 class QDomTextPrivate : public QDomCharacterDataPrivate
346 QDomTextPrivate(QDomDocumentPrivate*, QDomNodePrivate* parent, const QString& val);
347 QDomTextPrivate(QDomTextPrivate* n, bool deep);
349 QDomTextPrivate* splitText(int offset);
351 // Reimplemented from QDomNodePrivate
352 QDomNodePrivate* cloneNode(bool deep = true);
353 virtual bool isText() const { return true; }
354 QDomNode::NodeType nodeType() const { return QDomNode::TextNode; }
355 virtual void save(QTextStream& s, int, int) const;
358 class QDomAttrPrivate : public QDomNodePrivate
361 QDomAttrPrivate(QDomDocumentPrivate*, QDomNodePrivate*, const QString& name);
362 QDomAttrPrivate(QDomDocumentPrivate*, QDomNodePrivate*, const QString& nsURI, const QString& qName);
363 QDomAttrPrivate(QDomAttrPrivate* n, bool deep);
365 bool specified() const;
367 // Reimplemented from QDomNodePrivate
368 void setNodeValue(const QString& v);
369 QDomNodePrivate* cloneNode(bool deep = true);
370 virtual bool isAttr() const { return true; }
371 QDomNode::NodeType nodeType() const { return QDomNode::AttributeNode; }
372 virtual void save(QTextStream& s, int, int) const;
378 class QDomElementPrivate : public QDomNodePrivate
381 QDomElementPrivate(QDomDocumentPrivate*, QDomNodePrivate* parent, const QString& name);
382 QDomElementPrivate(QDomDocumentPrivate*, QDomNodePrivate* parent, const QString& nsURI, const QString& qName);
383 QDomElementPrivate(QDomElementPrivate* n, bool deep);
384 ~QDomElementPrivate();
386 QString attribute(const QString& name, const QString& defValue) const;
387 QString attributeNS(const QString& nsURI, const QString& localName, const QString& defValue) const;
388 void setAttribute(const QString& name, const QString& value);
389 void setAttributeNS(const QString& nsURI, const QString& qName, const QString& newValue);
390 void removeAttribute(const QString& name);
391 QDomAttrPrivate* attributeNode(const QString& name);
392 QDomAttrPrivate* attributeNodeNS(const QString& nsURI, const QString& localName);
393 QDomAttrPrivate* setAttributeNode(QDomAttrPrivate* newAttr);
394 QDomAttrPrivate* setAttributeNodeNS(QDomAttrPrivate* newAttr);
395 QDomAttrPrivate* removeAttributeNode(QDomAttrPrivate* oldAttr);
396 bool hasAttribute(const QString& name);
397 bool hasAttributeNS(const QString& nsURI, const QString& localName);
401 // Reimplemented from QDomNodePrivate
402 QDomNamedNodeMapPrivate* attributes() { return m_attr; }
403 bool hasAttributes() { return (m_attr->length() > 0); }
404 virtual bool isElement() const { return true; }
405 QDomNode::NodeType nodeType() const { return QDomNode::ElementNode; }
406 QDomNodePrivate* cloneNode(bool deep = true);
407 virtual void save(QTextStream& s, int, int) const;
410 QDomNamedNodeMapPrivate* m_attr;
414 class QDomCommentPrivate : public QDomCharacterDataPrivate
417 QDomCommentPrivate(QDomDocumentPrivate*, QDomNodePrivate* parent, const QString& val);
418 QDomCommentPrivate(QDomCommentPrivate* n, bool deep);
420 // Reimplemented from QDomNodePrivate
421 QDomNodePrivate* cloneNode(bool deep = true);
422 virtual bool isComment() const { return true; }
423 QDomNode::NodeType nodeType() const { return QDomNode::CommentNode; }
424 virtual void save(QTextStream& s, int, int) const;
427 class QDomCDATASectionPrivate : public QDomTextPrivate
430 QDomCDATASectionPrivate(QDomDocumentPrivate*, QDomNodePrivate* parent, const QString& val);
431 QDomCDATASectionPrivate(QDomCDATASectionPrivate* n, bool deep);
433 // Reimplemented from QDomNodePrivate
434 QDomNodePrivate* cloneNode(bool deep = true);
435 virtual bool isCDATASection() const { return true; }
436 QDomNode::NodeType nodeType() const { return QDomNode::CDATASectionNode; }
437 virtual void save(QTextStream& s, int, int) const;
440 class QDomNotationPrivate : public QDomNodePrivate
443 QDomNotationPrivate(QDomDocumentPrivate*, QDomNodePrivate* parent, const QString& name,
444 const QString& pub, const QString& sys);
445 QDomNotationPrivate(QDomNotationPrivate* n, bool deep);
447 // Reimplemented from QDomNodePrivate
448 QDomNodePrivate* cloneNode(bool deep = true);
449 virtual bool isNotation() const { return true; }
450 QDomNode::NodeType nodeType() const { return QDomNode::NotationNode; }
451 virtual void save(QTextStream& s, int, int) const;
458 class QDomEntityPrivate : public QDomNodePrivate
461 QDomEntityPrivate(QDomDocumentPrivate*, QDomNodePrivate* parent, const QString& name,
462 const QString& pub, const QString& sys, const QString& notation);
463 QDomEntityPrivate(QDomEntityPrivate* n, bool deep);
465 // Reimplemented from QDomNodePrivate
466 QDomNodePrivate* cloneNode(bool deep = true);
467 virtual bool isEntity() const { return true; }
468 QDomNode::NodeType nodeType() const { return QDomNode::EntityNode; }
469 virtual void save(QTextStream& s, int, int) const;
474 QString m_notationName;
477 class QDomEntityReferencePrivate : public QDomNodePrivate
480 QDomEntityReferencePrivate(QDomDocumentPrivate*, QDomNodePrivate* parent, const QString& name);
481 QDomEntityReferencePrivate(QDomNodePrivate* n, bool deep);
483 // Reimplemented from QDomNodePrivate
484 QDomNodePrivate* cloneNode(bool deep = true);
485 bool isEntityReference() const { return true; }
486 QDomNode::NodeType nodeType() const { return QDomNode::EntityReferenceNode; }
487 virtual void save(QTextStream& s, int, int) const;
490 class QDomProcessingInstructionPrivate : public QDomNodePrivate
493 QDomProcessingInstructionPrivate(QDomDocumentPrivate*, QDomNodePrivate* parent, const QString& target,
494 const QString& data);
495 QDomProcessingInstructionPrivate(QDomProcessingInstructionPrivate* n, bool deep);
497 // Reimplemented from QDomNodePrivate
498 QDomNodePrivate* cloneNode(bool deep = true);
499 virtual bool isProcessingInstruction() const { return true; }
500 QDomNode::NodeType nodeType() const { return QDomNode::ProcessingInstructionNode; }
501 virtual void save(QTextStream& s, int, int) const;
504 class QDomDocumentPrivate : public QDomNodePrivate
507 QDomDocumentPrivate();
508 QDomDocumentPrivate(const QString& name);
509 QDomDocumentPrivate(QDomDocumentTypePrivate* dt);
510 QDomDocumentPrivate(QDomDocumentPrivate* n, bool deep);
511 ~QDomDocumentPrivate();
513 bool setContent(QXmlInputSource *source, bool namespaceProcessing, QString *errorMsg, int *errorLine, int *errorColumn);
514 bool setContent(QXmlInputSource *source, QXmlReader *reader, QString *errorMsg, int *errorLine, int *errorColumn);
517 QDomDocumentTypePrivate* doctype() { return type.data(); }
518 QDomImplementationPrivate* implementation() { return impl.data(); }
519 QDomElementPrivate* documentElement();
522 QDomElementPrivate* createElement(const QString& tagName);
523 QDomElementPrivate* createElementNS(const QString& nsURI, const QString& qName);
524 QDomDocumentFragmentPrivate* createDocumentFragment();
525 QDomTextPrivate* createTextNode(const QString& data);
526 QDomCommentPrivate* createComment(const QString& data);
527 QDomCDATASectionPrivate* createCDATASection(const QString& data);
528 QDomProcessingInstructionPrivate* createProcessingInstruction(const QString& target, const QString& data);
529 QDomAttrPrivate* createAttribute(const QString& name);
530 QDomAttrPrivate* createAttributeNS(const QString& nsURI, const QString& qName);
531 QDomEntityReferencePrivate* createEntityReference(const QString& name);
533 QDomNodePrivate* importNode(const QDomNodePrivate* importedNode, bool deep);
535 // Reimplemented from QDomNodePrivate
536 QDomNodePrivate* cloneNode(bool deep = true);
537 bool isDocument() const { return true; }
538 QDomNode::NodeType nodeType() const { return QDomNode::DocumentNode; }
542 QExplicitlySharedDataPointer<QDomImplementationPrivate> impl;
543 QExplicitlySharedDataPointer<QDomDocumentTypePrivate> type;
545 void saveDocument(QTextStream& stream, const int indent, QDomNode::EncodingPolicy encUsed) const;
548 Counter for the QDomNodeListPrivate timestamps.
550 This is a cache optimization, that might in some cases be effective. The
551 dilemma is that QDomNode::childNodes() returns a list, but the
552 implementation stores the children in a linked list. Hence, in order to
553 get the children out through childNodes(), a list must be populated each
556 DOM has the requirement of node references being live, see DOM Core
557 Level 3, 1.1.1 The DOM Structure Model, which means that changes to the
558 underlying documents must be reflected in node lists.
560 This mechanism, nodeListTime, is a caching optimization that reduces the
561 amount of times the node list is rebuilt, by only doing so when the
562 document actually changes. However, a change to anywhere in any document
563 invalidate all lists, since no dependency tracking is done.
565 It functions by that all modifying functions(insertBefore() and so on)
566 increment the count; each QDomNodeListPrivate copies nodeListTime on
567 construction, and compares its own value to nodeListTime in order to
568 determine whether it needs to rebuild.
570 This is reentrant. The nodeListTime may overflow, but that's ok since we
571 check for equalness, not whether nodeListTime is smaller than the list's
577 /**************************************************************
581 **************************************************************/
583 class QDomHandler : public QXmlDefaultHandler
586 QDomHandler(QDomDocumentPrivate* d, bool namespaceProcessing);
591 bool startElement(const QString& nsURI, const QString& localName, const QString& qName, const QXmlAttributes& atts);
592 bool endElement(const QString& nsURI, const QString& localName, const QString& qName);
593 bool characters(const QString& ch);
594 bool processingInstruction(const QString& target, const QString& data);
595 bool skippedEntity(const QString& name);
598 bool fatalError(const QXmlParseException& exception);
603 bool startEntity(const QString &);
604 bool endEntity(const QString &);
605 bool startDTD(const QString& name, const QString& publicId, const QString& systemId);
606 bool comment(const QString& ch);
609 bool externalEntityDecl(const QString &name, const QString &publicId, const QString &systemId) ;
612 bool notationDecl(const QString & name, const QString & publicId, const QString & systemId);
613 bool unparsedEntityDecl(const QString &name, const QString &publicId, const QString &systemId, const QString ¬ationName) ;
615 void setDocumentLocator(QXmlLocator *locator);
622 QDomDocumentPrivate *doc;
623 QDomNodePrivate *node;
627 QXmlLocator *locator;
630 /**************************************************************
632 * Functions for verifying legal data
634 **************************************************************/
635 QDomImplementation::InvalidDataPolicy QDomImplementationPrivate::invalidDataPolicy
636 = QDomImplementation::AcceptInvalidChars;
638 // [5] Name ::= (Letter | '_' | ':') (NameChar)*
640 static QString fixedXmlName(const QString &_name, bool *ok, bool namespaces = false)
642 QString name, prefix;
644 qt_split_namespace(prefix, name, _name, true);
648 if (name.isEmpty()) {
653 if (QDomImplementationPrivate::invalidDataPolicy == QDomImplementation::AcceptInvalidChars) {
659 bool firstChar = true;
660 for (int i = 0; i < name.size(); ++i) {
661 QChar c = name.at(i);
663 if (QXmlUtils::isLetter(c) || c.unicode() == '_' || c.unicode() == ':') {
666 } else if (QDomImplementationPrivate::invalidDataPolicy == QDomImplementation::ReturnNullNode) {
671 if (QXmlUtils::isNameChar(c))
673 else if (QDomImplementationPrivate::invalidDataPolicy == QDomImplementation::ReturnNullNode) {
680 if (result.isEmpty()) {
686 if (namespaces && !prefix.isEmpty())
687 return prefix + QLatin1Char(':') + result;
691 // [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)
692 // '<', '&' and "]]>" will be escaped when writing
694 static QString fixedCharData(const QString &data, bool *ok)
696 if (QDomImplementationPrivate::invalidDataPolicy == QDomImplementation::AcceptInvalidChars) {
702 for (int i = 0; i < data.size(); ++i) {
703 QChar c = data.at(i);
704 if (QXmlUtils::isChar(c)) {
706 } else if (QDomImplementationPrivate::invalidDataPolicy == QDomImplementation::ReturnNullNode) {
716 // [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
717 // can't escape "--", since entities are not recognised within comments
719 static QString fixedComment(const QString &data, bool *ok)
721 if (QDomImplementationPrivate::invalidDataPolicy == QDomImplementation::AcceptInvalidChars) {
726 QString fixedData = fixedCharData(data, ok);
731 int idx = fixedData.indexOf(QLatin1String("--"));
734 if (QDomImplementationPrivate::invalidDataPolicy == QDomImplementation::ReturnNullNode) {
738 fixedData.remove(idx, 2);
745 // [20] CData ::= (Char* - (Char* ']]>' Char*))
746 // can't escape "]]>", since entities are not recognised within comments
748 static QString fixedCDataSection(const QString &data, bool *ok)
750 if (QDomImplementationPrivate::invalidDataPolicy == QDomImplementation::AcceptInvalidChars) {
755 QString fixedData = fixedCharData(data, ok);
760 int idx = fixedData.indexOf(QLatin1String("]]>"));
763 if (QDomImplementationPrivate::invalidDataPolicy == QDomImplementation::ReturnNullNode) {
767 fixedData.remove(idx, 3);
774 // [16] PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'
776 static QString fixedPIData(const QString &data, bool *ok)
778 if (QDomImplementationPrivate::invalidDataPolicy == QDomImplementation::AcceptInvalidChars) {
783 QString fixedData = fixedCharData(data, ok);
788 int idx = fixedData.indexOf(QLatin1String("?>"));
791 if (QDomImplementationPrivate::invalidDataPolicy == QDomImplementation::ReturnNullNode) {
795 fixedData.remove(idx, 2);
802 // [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'"
803 // The correct quote will be chosen when writing
805 static QString fixedPubidLiteral(const QString &data, bool *ok)
807 if (QDomImplementationPrivate::invalidDataPolicy == QDomImplementation::AcceptInvalidChars) {
814 if(QXmlUtils::isPublicID(data))
816 else if (QDomImplementationPrivate::invalidDataPolicy == QDomImplementation::ReturnNullNode) {
821 if (result.indexOf(QLatin1Char('\'')) != -1
822 && result.indexOf(QLatin1Char('"')) != -1) {
823 if (QDomImplementationPrivate::invalidDataPolicy == QDomImplementation::ReturnNullNode) {
827 result.remove(QLatin1Char('\''));
835 // [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'")
836 // The correct quote will be chosen when writing
838 static QString fixedSystemLiteral(const QString &data, bool *ok)
840 if (QDomImplementationPrivate::invalidDataPolicy == QDomImplementation::AcceptInvalidChars) {
845 QString result = data;
847 if (result.indexOf(QLatin1Char('\'')) != -1
848 && result.indexOf(QLatin1Char('"')) != -1) {
849 if (QDomImplementationPrivate::invalidDataPolicy == QDomImplementation::ReturnNullNode) {
853 result.remove(QLatin1Char('\''));
861 /**************************************************************
863 * QDomImplementationPrivate
865 **************************************************************/
867 QDomImplementationPrivate* QDomImplementationPrivate::clone()
869 return new QDomImplementationPrivate;
872 /**************************************************************
876 **************************************************************/
879 \class QDomImplementation
881 \brief The QDomImplementation class provides information about the
882 features of the DOM implementation.
887 This class describes the features that are supported by the DOM
888 implementation. Currently the XML subset of DOM Level 1 and DOM
889 Level 2 Core are supported.
891 Normally you will use the function QDomDocument::implementation()
892 to get the implementation object.
894 You can create a new document type with createDocumentType() and a
895 new document with createDocument().
897 For further information about the Document Object Model see
898 \l{http://www.w3.org/TR/REC-DOM-Level-1/}{Level 1} and
899 \l{http://www.w3.org/TR/DOM-Level-2-Core/}{Level 2 Core}. For a more
900 general introduction of the DOM implementation see the QDomDocument
903 The QDom classes have a few issues of nonconformance with the XML
904 specifications that cannot be fixed in Qt 4 without breaking backward
905 compatibility. The QtXmlPatterns module and the QXmlStreamReader and
906 QXmlStreamWriter classes have a higher degree of a conformance.
912 Constructs a QDomImplementation object.
914 QDomImplementation::QDomImplementation()
920 Constructs a copy of \a x.
922 QDomImplementation::QDomImplementation(const QDomImplementation &x)
929 QDomImplementation::QDomImplementation(QDomImplementationPrivate *p)
931 // We want to be co-owners, so increase the reference count
938 Assigns \a x to this DOM implementation.
940 QDomImplementation& QDomImplementation::operator=(const QDomImplementation &x)
944 if (impl && !impl->ref.deref())
951 Returns true if \a x and this DOM implementation object were
952 created from the same QDomDocument; otherwise returns false.
954 bool QDomImplementation::operator==(const QDomImplementation &x) const
956 return (impl == x.impl);
960 Returns true if \a x and this DOM implementation object were
961 created from different QDomDocuments; otherwise returns false.
963 bool QDomImplementation::operator!=(const QDomImplementation &x) const
965 return (impl != x.impl);
969 Destroys the object and frees its resources.
971 QDomImplementation::~QDomImplementation()
973 if (impl && !impl->ref.deref())
978 The function returns true if QDom implements the requested \a
979 version of a \a feature; otherwise returns false.
981 The currently supported features and their versions:
983 \header \i Feature \i Version
987 bool QDomImplementation::hasFeature(const QString& feature, const QString& version) const
989 if (feature == QLatin1String("XML")) {
990 if (version.isEmpty() || version == QLatin1String("1.0")) {
994 // ### add DOM level 2 features
999 Creates a document type node for the name \a qName.
1001 \a publicId specifies the public identifier of the external
1002 subset. If you specify an empty string (QString()) as the \a
1003 publicId, this means that the document type has no public
1006 \a systemId specifies the system identifier of the external
1007 subset. If you specify an empty string as the \a systemId, this
1008 means that the document type has no system identifier.
1010 Since you cannot have a public identifier without a system
1011 identifier, the public identifier is set to an empty string if
1012 there is no system identifier.
1014 DOM level 2 does not support any other document type declaration
1017 The only way you can use a document type that was created this
1018 way, is in combination with the createDocument() function to
1019 create a QDomDocument with this document type.
1021 In the DOM specification, this is the only way to create a non-null
1022 document. For historical reasons, Qt also allows to create the
1023 document using the default empty constructor. The resulting document
1024 is null, but becomes non-null when a factory function, for example
1025 QDomDocument::createElement(), is called. The document also becomes
1026 non-null when setContent() is called.
1028 \sa createDocument()
1030 QDomDocumentType QDomImplementation::createDocumentType(const QString& qName, const QString& publicId, const QString& systemId)
1033 QString fixedName = fixedXmlName(qName, &ok, true);
1035 return QDomDocumentType();
1037 QString fixedPublicId = fixedPubidLiteral(publicId, &ok);
1039 return QDomDocumentType();
1041 QString fixedSystemId = fixedSystemLiteral(systemId, &ok);
1043 return QDomDocumentType();
1045 QDomDocumentTypePrivate *dt = new QDomDocumentTypePrivate(0);
1046 dt->name = fixedName;
1047 if (systemId.isNull()) {
1048 dt->publicId.clear();
1049 dt->systemId.clear();
1051 dt->publicId = fixedPublicId;
1052 dt->systemId = fixedSystemId;
1055 return QDomDocumentType(dt);
1059 Creates a DOM document with the document type \a doctype. This
1060 function also adds a root element node with the qualified name \a
1061 qName and the namespace URI \a nsURI.
1063 QDomDocument QDomImplementation::createDocument(const QString& nsURI, const QString& qName, const QDomDocumentType& doctype)
1065 QDomDocument doc(doctype);
1066 QDomElement root = doc.createElementNS(nsURI, qName);
1068 return QDomDocument();
1069 doc.appendChild(root);
1074 Returns false if the object was created by
1075 QDomDocument::implementation(); otherwise returns true.
1077 bool QDomImplementation::isNull()
1083 \enum QDomImplementation::InvalidDataPolicy
1085 This enum specifies what should be done when a factory function
1086 in QDomDocument is called with invalid data.
1087 \value AcceptInvalidChars The data should be stored in the DOM object
1088 anyway. In this case the resulting XML document might not be well-formed.
1089 This is the default value and QDom's behavior in Qt < 4.1.
1090 \value DropInvalidChars The invalid characters should be removed from
1092 \value ReturnNullNode The factory function should return a null node.
1094 \sa setInvalidDataPolicy() invalidDataPolicy()
1098 \enum QDomNode::EncodingPolicy
1101 This enum specifies how QDomNode::save() determines what encoding to use
1104 \value EncodingFromDocument The encoding is fetched from the document.
1105 \value EncodingFromTextStream The encoding is fetched from the QTextStream.
1107 See also the overload of the save() function that takes an EncodingPolicy.
1114 Returns the invalid data policy, which specifies what should be done when
1115 a factory function in QDomDocument is passed invalid data.
1117 \sa setInvalidDataPolicy() InvalidDataPolicy
1120 QDomImplementation::InvalidDataPolicy QDomImplementation::invalidDataPolicy()
1122 return QDomImplementationPrivate::invalidDataPolicy;
1129 Sets the invalid data policy, which specifies what should be done when
1130 a factory function in QDomDocument is passed invalid data.
1132 The \a policy is set for all instances of QDomDocument which already
1133 exist and which will be created in the future.
1135 \snippet doc/src/snippets/code/src_xml_dom_qdom.cpp 0
1137 \sa invalidDataPolicy() InvalidDataPolicy
1140 void QDomImplementation::setInvalidDataPolicy(InvalidDataPolicy policy)
1142 QDomImplementationPrivate::invalidDataPolicy = policy;
1145 /**************************************************************
1147 * QDomNodeListPrivate
1149 **************************************************************/
1151 QDomNodeListPrivate::QDomNodeListPrivate(QDomNodePrivate *n_impl) : ref(1)
1155 node_impl->ref.ref();
1159 QDomNodeListPrivate::QDomNodeListPrivate(QDomNodePrivate *n_impl, const QString &name) :
1164 node_impl->ref.ref();
1169 QDomNodeListPrivate::QDomNodeListPrivate(QDomNodePrivate *n_impl, const QString &_nsURI, const QString &localName) :
1174 node_impl->ref.ref();
1175 tagname = localName;
1180 QDomNodeListPrivate::~QDomNodeListPrivate()
1182 if (node_impl && !node_impl->ref.deref())
1186 bool QDomNodeListPrivate::operator==(const QDomNodeListPrivate &other) const
1188 return (node_impl == other.node_impl) && (tagname == other.tagname);
1191 bool QDomNodeListPrivate::operator!=(const QDomNodeListPrivate &other) const
1193 return (node_impl != other.node_impl) || (tagname != other.tagname);
1196 void QDomNodeListPrivate::createList()
1201 const QDomDocumentPrivate *const doc = node_impl->ownerDocument();
1202 if (doc && timestamp != doc->nodeListTime)
1203 timestamp = doc->nodeListTime;
1205 QDomNodePrivate* p = node_impl->first;
1208 if (tagname.isNull()) {
1213 } else if (nsURI.isNull()) {
1214 while (p && p != node_impl) {
1215 if (p->isElement() && p->nodeName() == tagname) {
1224 while (p && p != node_impl && !p->next)
1226 if (p && p != node_impl)
1231 while (p && p != node_impl) {
1232 if (p->isElement() && p->name==tagname && p->namespaceURI==nsURI) {
1241 while (p && p != node_impl && !p->next)
1243 if (p && p != node_impl)
1250 QDomNodePrivate* QDomNodeListPrivate::item(int index)
1255 const QDomDocumentPrivate *const doc = node_impl->ownerDocument();
1256 if (!doc || timestamp != doc->nodeListTime)
1259 if (index >= list.size())
1262 return list.at(index);
1265 uint QDomNodeListPrivate::length() const
1270 const QDomDocumentPrivate *const doc = node_impl->ownerDocument();
1271 if (!doc || timestamp != doc->nodeListTime) {
1272 QDomNodeListPrivate *that = const_cast<QDomNodeListPrivate *>(this);
1276 return list.count();
1279 /**************************************************************
1283 **************************************************************/
1288 \brief The QDomNodeList class is a list of QDomNode objects.
1293 Lists can be obtained by QDomDocument::elementsByTagName() and
1294 QDomNode::childNodes(). The Document Object Model (DOM) requires
1295 these lists to be "live": whenever you change the underlying
1296 document, the contents of the list will get updated.
1298 You can get a particular node from the list with item(). The
1299 number of items in the list is returned by length().
1301 For further information about the Document Object Model see
1302 \l{http://www.w3.org/TR/REC-DOM-Level-1/}{Level 1} and
1303 \l{http://www.w3.org/TR/DOM-Level-2-Core/}{Level 2 Core}.
1304 For a more general introduction of the DOM implementation see the
1305 QDomDocument documentation.
1307 \sa QDomNode::childNodes() QDomDocument::elementsByTagName()
1311 Creates an empty node list.
1313 QDomNodeList::QDomNodeList()
1318 QDomNodeList::QDomNodeList(QDomNodeListPrivate* p)
1324 Constructs a copy of \a n.
1326 QDomNodeList::QDomNodeList(const QDomNodeList& n)
1334 Assigns \a n to this node list.
1336 QDomNodeList& QDomNodeList::operator=(const QDomNodeList &n)
1340 if (impl && !impl->ref.deref())
1347 Returns true if the node list \a n and this node list are equal;
1348 otherwise returns false.
1350 bool QDomNodeList::operator==(const QDomNodeList &n) const
1354 if (!impl || !n.impl)
1356 return (*impl == *n.impl);
1360 Returns true the node list \a n and this node list are not equal;
1361 otherwise returns false.
1363 bool QDomNodeList::operator!=(const QDomNodeList &n) const
1365 return !operator==(n);
1369 Destroys the object and frees its resources.
1371 QDomNodeList::~QDomNodeList()
1373 if (impl && !impl->ref.deref())
1378 Returns the node at position \a index.
1380 If \a index is negative or if \a index >= length() then a null
1381 node is returned (i.e. a node for which QDomNode::isNull() returns
1386 QDomNode QDomNodeList::item(int index) const
1391 return QDomNode(impl->item(index));
1395 Returns the number of nodes in the list.
1397 uint QDomNodeList::length() const
1401 return impl->length();
1405 \fn bool QDomNodeList::isEmpty() const
1407 Returns true if the list contains no items; otherwise returns false.
1408 This function is provided for Qt API consistency.
1412 \fn int QDomNodeList::count() const
1414 This function is provided for Qt API consistency. It is equivalent to length().
1418 \fn int QDomNodeList::size() const
1420 This function is provided for Qt API consistency. It is equivalent to length().
1424 \fn QDomNode QDomNodeList::at(int index) const
1426 This function is provided for Qt API consistency. It is equivalent
1429 If \a index is negative or if \a index >= length() then a null
1430 node is returned (i.e. a node for which QDomNode::isNull() returns
1434 /**************************************************************
1438 **************************************************************/
1440 inline void QDomNodePrivate::setOwnerDocument(QDomDocumentPrivate *doc)
1446 QDomNodePrivate::QDomNodePrivate(QDomDocumentPrivate *doc, QDomNodePrivate *par) : ref(1)
1451 setOwnerDocument(doc);
1456 createdWithDom1Interface = true;
1461 QDomNodePrivate::QDomNodePrivate(QDomNodePrivate *n, bool deep) : ref(1)
1463 setOwnerDocument(n->ownerDocument());
1472 namespaceURI = n->namespaceURI;
1473 createdWithDom1Interface = n->createdWithDom1Interface;
1480 for (QDomNodePrivate* x = n->first; x; x = x->next)
1481 appendChild(x->cloneNode(true));
1484 QDomNodePrivate::~QDomNodePrivate()
1486 QDomNodePrivate* p = first;
1491 if (!p->ref.deref())
1501 void QDomNodePrivate::clear()
1503 QDomNodePrivate* p = first;
1508 if (!p->ref.deref())
1516 QDomNodePrivate* QDomNodePrivate::namedItem(const QString &n)
1518 QDomNodePrivate* p = first;
1520 if (p->nodeName() == n)
1528 QDomNodePrivate* QDomNodePrivate::insertBefore(QDomNodePrivate* newChild, QDomNodePrivate* refChild)
1535 if (newChild == refChild)
1539 if (refChild && refChild->parent() != this)
1542 // "mark lists as dirty"
1543 QDomDocumentPrivate *const doc = ownerDocument();
1545 doc->nodeListTime++;
1547 // Special handling for inserting a fragment. We just insert
1548 // all elements of the fragment instead of the fragment itself.
1549 if (newChild->isDocumentFragment()) {
1550 // Fragment is empty ?
1551 if (newChild->first == 0)
1555 QDomNodePrivate* n = newChild->first;
1561 // Insert at the beginning ?
1562 if (!refChild || refChild->prev == 0) {
1564 first->prev = newChild->last;
1565 newChild->last->next = first;
1567 last = newChild->last;
1568 first = newChild->first;
1570 // Insert in the middle
1571 newChild->last->next = refChild;
1572 newChild->first->prev = refChild->prev;
1573 refChild->prev->next = newChild->first;
1574 refChild->prev = newChild->last;
1577 // No need to increase the reference since QDomDocumentFragment
1578 // does not decrease the reference.
1580 // Remove the nodes from the fragment
1581 newChild->first = 0;
1586 // No more errors can occur now, so we take
1587 // ownership of the node.
1588 newChild->ref.ref();
1590 if (newChild->parent())
1591 newChild->parent()->removeChild(newChild);
1593 newChild->setParent(this);
1597 first->prev = newChild;
1598 newChild->next = first;
1605 if (refChild->prev == 0) {
1607 first->prev = newChild;
1608 newChild->next = first;
1615 newChild->next = refChild;
1616 newChild->prev = refChild->prev;
1617 refChild->prev->next = newChild;
1618 refChild->prev = newChild;
1623 QDomNodePrivate* QDomNodePrivate::insertAfter(QDomNodePrivate* newChild, QDomNodePrivate* refChild)
1630 if (newChild == refChild)
1634 if (refChild && refChild->parent() != this)
1637 // "mark lists as dirty"
1638 QDomDocumentPrivate *const doc = ownerDocument();
1640 doc->nodeListTime++;
1642 // Special handling for inserting a fragment. We just insert
1643 // all elements of the fragment instead of the fragment itself.
1644 if (newChild->isDocumentFragment()) {
1645 // Fragment is empty ?
1646 if (newChild->first == 0)
1650 QDomNodePrivate* n = newChild->first;
1656 // Insert at the end
1657 if (!refChild || refChild->next == 0) {
1659 last->next = newChild->first;
1660 newChild->first->prev = last;
1662 first = newChild->first;
1663 last = newChild->last;
1664 } else { // Insert in the middle
1665 newChild->first->prev = refChild;
1666 newChild->last->next = refChild->next;
1667 refChild->next->prev = newChild->last;
1668 refChild->next = newChild->first;
1671 // No need to increase the reference since QDomDocumentFragment
1672 // does not decrease the reference.
1674 // Remove the nodes from the fragment
1675 newChild->first = 0;
1680 // Release new node from its current parent
1681 if (newChild->parent())
1682 newChild->parent()->removeChild(newChild);
1684 // No more errors can occur now, so we take
1685 // ownership of the node
1686 newChild->ref.ref();
1688 newChild->setParent(this);
1690 // Insert at the end
1693 last->next = newChild;
1694 newChild->prev = last;
1701 if (refChild->next == 0) {
1703 last->next = newChild;
1704 newChild->prev = last;
1711 newChild->prev = refChild;
1712 newChild->next = refChild->next;
1713 refChild->next->prev = newChild;
1714 refChild->next = newChild;
1719 QDomNodePrivate* QDomNodePrivate::replaceChild(QDomNodePrivate* newChild, QDomNodePrivate* oldChild)
1721 if (!newChild || !oldChild)
1723 if (oldChild->parent() != this)
1725 if (newChild == oldChild)
1728 // mark lists as dirty
1729 QDomDocumentPrivate *const doc = ownerDocument();
1731 doc->nodeListTime++;
1733 // Special handling for inserting a fragment. We just insert
1734 // all elements of the fragment instead of the fragment itself.
1735 if (newChild->isDocumentFragment()) {
1736 // Fragment is empty ?
1737 if (newChild->first == 0)
1741 QDomNodePrivate* n = newChild->first;
1749 oldChild->next->prev = newChild->last;
1751 oldChild->prev->next = newChild->first;
1753 newChild->last->next = oldChild->next;
1754 newChild->first->prev = oldChild->prev;
1756 if (first == oldChild)
1757 first = newChild->first;
1758 if (last == oldChild)
1759 last = newChild->last;
1761 oldChild->setNoParent();
1765 // No need to increase the reference since QDomDocumentFragment
1766 // does not decrease the reference.
1768 // Remove the nodes from the fragment
1769 newChild->first = 0;
1772 // We are no longer interested in the old node
1774 oldChild->ref.deref();
1779 // No more errors can occur now, so we take
1780 // ownership of the node
1781 newChild->ref.ref();
1783 // Release new node from its current parent
1784 if (newChild->parent())
1785 newChild->parent()->removeChild(newChild);
1787 newChild->setParent(this);
1790 oldChild->next->prev = newChild;
1792 oldChild->prev->next = newChild;
1794 newChild->next = oldChild->next;
1795 newChild->prev = oldChild->prev;
1797 if (first == oldChild)
1799 if (last == oldChild)
1802 oldChild->setNoParent();
1806 // We are no longer interested in the old node
1808 oldChild->ref.deref();
1813 QDomNodePrivate* QDomNodePrivate::removeChild(QDomNodePrivate* oldChild)
1816 if (oldChild->parent() != this)
1819 // "mark lists as dirty"
1820 QDomDocumentPrivate *const doc = ownerDocument();
1822 doc->nodeListTime++;
1824 // Perhaps oldChild was just created with "createElement" or that. In this case
1825 // its parent is QDomDocument but it is not part of the documents child list.
1826 if (oldChild->next == 0 && oldChild->prev == 0 && first != oldChild)
1830 oldChild->next->prev = oldChild->prev;
1832 oldChild->prev->next = oldChild->next;
1834 if (last == oldChild)
1835 last = oldChild->prev;
1836 if (first == oldChild)
1837 first = oldChild->next;
1839 oldChild->setNoParent();
1843 // We are no longer interested in the old node
1844 oldChild->ref.deref();
1849 QDomNodePrivate* QDomNodePrivate::appendChild(QDomNodePrivate* newChild)
1851 // No reference manipulation needed. Done in insertAfter.
1852 return insertAfter(newChild, 0);
1855 QDomDocumentPrivate* QDomNodePrivate::ownerDocument()
1857 QDomNodePrivate* p = this;
1858 while (p && !p->isDocument()) {
1860 return (QDomDocumentPrivate*)p->ownerNode;
1864 return static_cast<QDomDocumentPrivate *>(p);
1867 QDomNodePrivate* QDomNodePrivate::cloneNode(bool deep)
1869 QDomNodePrivate* p = new QDomNodePrivate(this, deep);
1870 // We are not interested in this node
1875 static void qNormalizeNode(QDomNodePrivate* n)
1877 QDomNodePrivate* p = n->first;
1878 QDomTextPrivate* t = 0;
1883 QDomNodePrivate* tmp = p->next;
1884 t->appendData(p->nodeValue());
1888 t = (QDomTextPrivate*)p;
1897 void QDomNodePrivate::normalize()
1899 // ### This one has moved from QDomElementPrivate to this position. It is
1901 qNormalizeNode(this);
1905 \a depth is used for indentation, it seems.
1907 void QDomNodePrivate::save(QTextStream& s, int depth, int indent) const
1909 const QDomNodePrivate* n = first;
1911 n->save(s, depth, indent);
1916 void QDomNodePrivate::setLocation(int lineNumber, int columnNumber)
1918 this->lineNumber = lineNumber;
1919 this->columnNumber = columnNumber;
1922 /**************************************************************
1926 **************************************************************/
1928 #define IMPL ((QDomNodePrivate*)impl)
1933 \brief The QDomNode class is the base class for all the nodes in a DOM tree.
1939 Many functions in the DOM return a QDomNode.
1941 You can find out the type of a node using isAttr(),
1942 isCDATASection(), isDocumentFragment(), isDocument(),
1943 isDocumentType(), isElement(), isEntityReference(), isText(),
1944 isEntity(), isNotation(), isProcessingInstruction(),
1945 isCharacterData() and isComment().
1947 A QDomNode can be converted into one of its subclasses using
1948 toAttr(), toCDATASection(), toDocumentFragment(), toDocument(),
1949 toDocumentType(), toElement(), toEntityReference(), toText(),
1950 toEntity(), toNotation(), toProcessingInstruction(),
1951 toCharacterData() or toComment(). You can convert a node to a null
1954 Copies of the QDomNode class share their data using explicit
1955 sharing. This means that modifying one node will change all
1956 copies. This is especially useful in combination with functions
1957 which return a QDomNode, e.g. firstChild(). You can make an
1958 independent (deep) copy of the node with cloneNode().
1960 A QDomNode can be null, much like a null pointer. Creating a copy
1961 of a null node results in another null node. It is not
1962 possible to modify a null node, but it is possible to assign another,
1963 possibly non-null node to it. In this case, the copy of the null node
1964 will remain null. You can check if a QDomNode is null by calling isNull().
1965 The empty constructor of a QDomNode (or any of the derived classes) creates
1968 Nodes are inserted with insertBefore(), insertAfter() or
1969 appendChild(). You can replace one node with another using
1970 replaceChild() and remove a node with removeChild().
1972 To traverse nodes use firstChild() to get a node's first child (if
1973 any), and nextSibling() to traverse. QDomNode also provides
1974 lastChild(), previousSibling() and parentNode(). To find the first
1975 child node with a particular node name use namedItem().
1977 To find out if a node has children use hasChildNodes() and to get
1978 a list of all of a node's children use childNodes().
1980 The node's name and value (the meaning of which varies depending
1981 on its type) is returned by nodeName() and nodeValue()
1982 respectively. The node's type is returned by nodeType(). The
1983 node's value can be set with setNodeValue().
1985 The document to which the node belongs is returned by
1988 Adjacent QDomText nodes can be merged into a single node with
1991 \l QDomElement nodes have attributes which can be retrieved with
1994 QDomElement and QDomAttr nodes can have namespaces which can be
1995 retrieved with namespaceURI(). Their local name is retrieved with
1996 localName(), and their prefix with prefix(). The prefix can be set
1999 You can write the XML representation of the node to a text stream
2002 The following example looks for the first element in an XML document and
2003 prints the names of all the elements that are its direct children.
2004 \snippet doc/src/snippets/code/src_xml_dom_qdom.cpp 1
2006 For further information about the Document Object Model see
2007 \l{http://www.w3.org/TR/REC-DOM-Level-1/}{Level 1} and
2008 \l{http://www.w3.org/TR/DOM-Level-2-Core/}{Level 2 Core}.
2009 For a more general introduction of the DOM implementation see the
2010 QDomDocument documentation.
2014 Constructs a \link isNull() null\endlink node.
2016 QDomNode::QDomNode()
2022 Constructs a copy of \a n.
2024 The data of the copy is shared (shallow copy): modifying one node
2025 will also change the other. If you want to make a deep copy, use
2028 QDomNode::QDomNode(const QDomNode &n)
2036 Constructs a new node for the data \a n.
2038 QDomNode::QDomNode(QDomNodePrivate *n)
2046 Assigns a copy of \a n to this DOM node.
2048 The data of the copy is shared (shallow copy): modifying one node
2049 will also change the other. If you want to make a deep copy, use
2052 QDomNode& QDomNode::operator=(const QDomNode &n)
2056 if (impl && !impl->ref.deref())
2063 Returns true if \a n and this DOM node are equal; otherwise
2066 Any instance of QDomNode acts as a reference to an underlying data
2067 structure in QDomDocument. The test for equality checks if the two
2068 references point to the same underlying node. For example:
2070 \snippet doc/src/snippets/code/src_xml_dom_qdom.cpp 2
2072 The two nodes (QDomElement is a QDomNode subclass) both refer to
2073 the document's root element, and \c {element1 == element2} will
2074 return true. On the other hand:
2076 \snippet doc/src/snippets/code/src_xml_dom_qdom.cpp 3
2078 Even though both nodes are empty elements carrying the same name,
2079 \c {element3 == element4} will return false because they refer to
2080 two different nodes in the underlying data structure.
2082 bool QDomNode::operator== (const QDomNode& n) const
2084 return (impl == n.impl);
2088 Returns true if \a n and this DOM node are not equal; otherwise
2091 bool QDomNode::operator!= (const QDomNode& n) const
2093 return (impl != n.impl);
2097 Destroys the object and frees its resources.
2099 QDomNode::~QDomNode()
2101 if (impl && !impl->ref.deref())
2106 Returns the name of the node.
2108 The meaning of the name depends on the subclass:
2111 \header \i Name \i Meaning
2112 \row \i QDomAttr \i The name of the attribute
2113 \row \i QDomCDATASection \i The string "#cdata-section"
2114 \row \i QDomComment \i The string "#comment"
2115 \row \i QDomDocument \i The string "#document"
2116 \row \i QDomDocumentFragment \i The string "#document-fragment"
2117 \row \i QDomDocumentType \i The name of the document type
2118 \row \i QDomElement \i The tag name
2119 \row \i QDomEntity \i The name of the entity
2120 \row \i QDomEntityReference \i The name of the referenced entity
2121 \row \i QDomNotation \i The name of the notation
2122 \row \i QDomProcessingInstruction \i The target of the processing instruction
2123 \row \i QDomText \i The string "#text"
2126 \bold{Note:} This function does not take the presence of namespaces into account
2127 when processing the names of element and attribute nodes. As a result, the
2128 returned name can contain any namespace prefix that may be present.
2129 To obtain the node name of an element or attribute, use localName(); to
2130 obtain the namespace prefix, use namespaceURI().
2134 QString QDomNode::nodeName() const
2139 if (!IMPL->prefix.isEmpty())
2140 return IMPL->prefix + QLatin1Char(':') + IMPL->name;
2145 Returns the value of the node.
2147 The meaning of the value depends on the subclass:
2149 \header \i Name \i Meaning
2150 \row \i QDomAttr \i The attribute value
2151 \row \i QDomCDATASection \i The content of the CDATA section
2152 \row \i QDomComment \i The comment
2153 \row \i QDomProcessingInstruction \i The data of the processing instruction
2154 \row \i QDomText \i The text
2157 All the other subclasses do not have a node value and will return
2160 \sa setNodeValue() nodeName()
2162 QString QDomNode::nodeValue() const
2170 Sets the node's value to \a v.
2174 void QDomNode::setNodeValue(const QString& v)
2178 IMPL->setNodeValue(v);
2182 \enum QDomNode::NodeType
2184 This enum defines the type of the node:
2186 \value AttributeNode
2188 \value CDATASectionNode
2189 \value EntityReferenceNode
2191 \value ProcessingInstructionNode
2194 \value DocumentTypeNode
2195 \value DocumentFragmentNode
2197 \value BaseNode A QDomNode object, i.e. not a QDomNode subclass.
2198 \value CharacterDataNode
2202 Returns the type of the node.
2204 \sa toAttr(), toCDATASection(), toDocumentFragment(),
2205 toDocument() toDocumentType(), toElement(), toEntityReference(),
2206 toText(), toEntity() toNotation(), toProcessingInstruction(),
2207 toCharacterData(), toComment()
2209 QDomNode::NodeType QDomNode::nodeType() const
2212 return QDomNode::BaseNode;
2213 return IMPL->nodeType();
2217 Returns the parent node. If this node has no parent, a null node
2218 is returned (i.e. a node for which isNull() returns true).
2220 QDomNode QDomNode::parentNode() const
2224 return QDomNode(IMPL->parent());
2228 Returns a list of all direct child nodes.
2230 Most often you will call this function on a QDomElement object.
2232 For example, if the XML document looks like this:
2233 \snippet doc/src/snippets/code/src_xml_dom_qdom.cpp 4
2234 Then the list of child nodes for the "body"-element will contain
2235 the node created by the <h1> tag and the node created by the
2238 The nodes in the list are not copied; so changing the nodes in the
2239 list will also change the children of this node.
2241 \sa firstChild() lastChild()
2243 QDomNodeList QDomNode::childNodes() const
2246 return QDomNodeList();
2247 return QDomNodeList(new QDomNodeListPrivate(impl));
2251 Returns the first child of the node. If there is no child node, a
2252 \link isNull() null node\endlink is returned. Changing the
2253 returned node will also change the node in the document tree.
2255 \sa lastChild() childNodes()
2257 QDomNode QDomNode::firstChild() const
2261 return QDomNode(IMPL->first);
2265 Returns the last child of the node. If there is no child node, a
2266 \link isNull() null node\endlink is returned. Changing the
2267 returned node will also change the node in the document tree.
2269 \sa firstChild() childNodes()
2271 QDomNode QDomNode::lastChild() const
2275 return QDomNode(IMPL->last);
2279 Returns the previous sibling in the document tree. Changing the
2280 returned node will also change the node in the document tree.
2282 For example, if you have XML like this:
2283 \snippet doc/src/snippets/code/src_xml_dom_qdom.cpp 5
2284 and this QDomNode represents the <p> tag, previousSibling()
2285 will return the node representing the <h1> tag.
2289 QDomNode QDomNode::previousSibling() const
2293 return QDomNode(IMPL->prev);
2297 Returns the next sibling in the document tree. Changing the
2298 returned node will also change the node in the document tree.
2300 If you have XML like this:
2301 \snippet doc/src/snippets/code/src_xml_dom_qdom.cpp 6
2302 and this QDomNode represents the <p> tag, nextSibling() will
2303 return the node representing the <h2> tag.
2305 \sa previousSibling()
2307 QDomNode QDomNode::nextSibling() const
2311 return QDomNode(IMPL->next);
2315 // ###### don't think this is part of the DOM and
2317 Returns a named node map of all attributes. Attributes are only
2318 provided for \l{QDomElement}s.
2320 Changing the attributes in the map will also change the attributes
2323 QDomNamedNodeMap QDomNode::attributes() const
2325 if (!impl || !impl->isElement())
2326 return QDomNamedNodeMap();
2328 return QDomNamedNodeMap(static_cast<QDomElementPrivate *>(impl)->attributes());
2332 Returns the document to which this node belongs.
2334 QDomDocument QDomNode::ownerDocument() const
2337 return QDomDocument();
2338 return QDomDocument(IMPL->ownerDocument());
2342 Creates a deep (not shallow) copy of the QDomNode.
2344 If \a deep is true, then the cloning is done recursively which
2345 means that all the node's children are deep copied too. If \a deep
2346 is false only the node itself is copied and the copy will have no
2349 QDomNode QDomNode::cloneNode(bool deep) const
2353 return QDomNode(IMPL->cloneNode(deep));
2357 Calling normalize() on an element converts all its children into a
2358 standard form. This means that adjacent QDomText objects will be
2359 merged into a single text object (QDomCDATASection nodes are not
2362 void QDomNode::normalize()
2370 Returns true if the DOM implementation implements the feature \a
2371 feature and this feature is supported by this node in the version
2372 \a version; otherwise returns false.
2374 \sa QDomImplementation::hasFeature()
2376 bool QDomNode::isSupported(const QString& feature, const QString& version) const
2378 QDomImplementation i;
2379 return i.hasFeature(feature, version);
2383 Returns the namespace URI of this node or an empty string if the
2384 node has no namespace URI.
2386 Only nodes of type \link QDomNode::NodeType ElementNode\endlink or
2387 \link QDomNode::NodeType AttributeNode\endlink can have
2388 namespaces. A namespace URI must be specified at creation time and
2389 cannot be changed later.
2391 \sa prefix() localName() QDomDocument::createElementNS()
2392 QDomDocument::createAttributeNS()
2394 QString QDomNode::namespaceURI() const
2398 return IMPL->namespaceURI;
2402 Returns the namespace prefix of the node or an empty string if the
2403 node has no namespace prefix.
2405 Only nodes of type \link QDomNode::NodeType ElementNode\endlink or
2406 \link QDomNode::NodeType AttributeNode\endlink can have
2407 namespaces. A namespace prefix must be specified at creation time.
2408 If a node was created with a namespace prefix, you can change it
2409 later with setPrefix().
2411 If you create an element or attribute with
2412 QDomDocument::createElement() or QDomDocument::createAttribute(),
2413 the prefix will be an empty string. If you use
2414 QDomDocument::createElementNS() or
2415 QDomDocument::createAttributeNS() instead, the prefix will not be
2416 an empty string; but it might be an empty string if the name does
2419 \sa setPrefix() localName() namespaceURI()
2420 QDomDocument::createElementNS() QDomDocument::createAttributeNS()
2422 QString QDomNode::prefix() const
2426 return IMPL->prefix;
2430 If the node has a namespace prefix, this function changes the
2431 namespace prefix of the node to \a pre. Otherwise this function
2434 Only nodes of type \link QDomNode::NodeType ElementNode\endlink or
2435 \link QDomNode::NodeType AttributeNode\endlink can have
2436 namespaces. A namespace prefix must have be specified at creation
2437 time; it is not possible to add a namespace prefix afterwards.
2439 \sa prefix() localName() namespaceURI()
2440 QDomDocument::createElementNS() QDomDocument::createAttributeNS()
2442 void QDomNode::setPrefix(const QString& pre)
2444 if (!impl || IMPL->prefix.isNull())
2446 if (isAttr() || isElement())
2451 If the node uses namespaces, this function returns the local name
2452 of the node; otherwise it returns an empty string.
2454 Only nodes of type \link QDomNode::NodeType ElementNode\endlink or
2455 \link QDomNode::NodeType AttributeNode\endlink can have
2456 namespaces. A namespace must have been specified at creation time;
2457 it is not possible to add a namespace afterwards.
2459 \sa prefix() namespaceURI() QDomDocument::createElementNS()
2460 QDomDocument::createAttributeNS()
2462 QString QDomNode::localName() const
2464 if (!impl || IMPL->createdWithDom1Interface)
2470 Returns true if the node has attributes; otherwise returns false.
2474 bool QDomNode::hasAttributes() const
2476 if (!impl || !impl->isElement())
2478 return static_cast<QDomElementPrivate *>(impl)->hasAttributes();
2482 Inserts the node \a newChild before the child node \a refChild.
2483 \a refChild must be a direct child of this node. If \a refChild is
2484 \link isNull() null\endlink then \a newChild is inserted as the
2487 If \a newChild is the child of another node, it is reparented to
2488 this node. If \a newChild is a child of this node, then its
2489 position in the list of children is changed.
2491 If \a newChild is a QDomDocumentFragment, then the children of the
2492 fragment are removed from the fragment and inserted before \a
2495 Returns a new reference to \a newChild on success or a \link
2496 isNull() null node\endlink on failure.
2498 The DOM specification disallow inserting attribute nodes, but due
2499 to historical reasons QDom accept them nevertheless.
2501 \sa insertAfter() replaceChild() removeChild() appendChild()
2503 QDomNode QDomNode::insertBefore(const QDomNode& newChild, const QDomNode& refChild)
2507 return QDomNode(IMPL->insertBefore(newChild.impl, refChild.impl));
2511 Inserts the node \a newChild after the child node \a refChild. \a
2512 refChild must be a direct child of this node. If \a refChild is
2513 \link isNull() null\endlink then \a newChild is appended as this
2516 If \a newChild is the child of another node, it is reparented to
2517 this node. If \a newChild is a child of this node, then its
2518 position in the list of children is changed.
2520 If \a newChild is a QDomDocumentFragment, then the children of the
2521 fragment are removed from the fragment and inserted after \a
2524 Returns a new reference to \a newChild on success or a \link
2525 isNull() null node\endlink on failure.
2527 The DOM specification disallow inserting attribute nodes, but due
2528 to historical reasons QDom accept them nevertheless.
2530 \sa insertBefore() replaceChild() removeChild() appendChild()
2532 QDomNode QDomNode::insertAfter(const QDomNode& newChild, const QDomNode& refChild)
2536 return QDomNode(IMPL->insertAfter(newChild.impl, refChild.impl));
2540 Replaces \a oldChild with \a newChild. \a oldChild must be a
2541 direct child of this node.
2543 If \a newChild is the child of another node, it is reparented to
2544 this node. If \a newChild is a child of this node, then its
2545 position in the list of children is changed.
2547 If \a newChild is a QDomDocumentFragment, then \a oldChild is
2548 replaced by all of the children of the fragment.
2550 Returns a new reference to \a oldChild on success or a \link
2551 isNull() null node\endlink an failure.
2553 \sa insertBefore() insertAfter() removeChild() appendChild()
2555 QDomNode QDomNode::replaceChild(const QDomNode& newChild, const QDomNode& oldChild)
2557 if (!impl || !newChild.impl || !oldChild.impl)
2559 return QDomNode(IMPL->replaceChild(newChild.impl, oldChild.impl));
2563 Removes \a oldChild from the list of children. \a oldChild must be
2564 a direct child of this node.
2566 Returns a new reference to \a oldChild on success or a \link
2567 isNull() null node\endlink on failure.
2569 \sa insertBefore() insertAfter() replaceChild() appendChild()
2571 QDomNode QDomNode::removeChild(const QDomNode& oldChild)
2576 if (oldChild.isNull())
2579 return QDomNode(IMPL->removeChild(oldChild.impl));
2583 Appends \a newChild as the node's last child.
2585 If \a newChild is the child of another node, it is reparented to
2586 this node. If \a newChild is a child of this node, then its
2587 position in the list of children is changed.
2589 If \a newChild is a QDomDocumentFragment, then the children of the
2590 fragment are removed from the fragment and appended.
2592 If \a newChild is a QDomElement and this node is a QDomDocument that
2593 already has an element node as a child, \a newChild is not added as
2594 a child and a null node is returned.
2596 Returns a new reference to \a newChild on success or a \link
2597 isNull() null node\endlink on failure.
2599 Calling this function on a null node(created, for example, with
2600 the default constructor) does nothing and returns a \link isNull()
2603 The DOM specification disallow inserting attribute nodes, but for
2604 historical reasons, QDom accepts them anyway.
2606 \sa insertBefore() insertAfter() replaceChild() removeChild()
2608 QDomNode QDomNode::appendChild(const QDomNode& newChild)
2611 qWarning("Calling appendChild() on a null node does nothing.");
2614 return QDomNode(IMPL->appendChild(newChild.impl));
2618 Returns true if the node has one or more children; otherwise
2621 bool QDomNode::hasChildNodes() const
2625 return IMPL->first != 0;
2629 Returns true if this node is null (i.e. if it has no type or
2630 contents); otherwise returns false.
2632 bool QDomNode::isNull() const
2638 Converts the node into a null node; if it was not a null node
2639 before, its type and contents are deleted.
2643 void QDomNode::clear()
2645 if (impl && !impl->ref.deref())
2651 Returns the first direct child node for which nodeName() equals \a
2654 If no such direct child exists, a \link isNull() null node\endlink
2659 QDomNode QDomNode::namedItem(const QString& name) const
2663 return QDomNode(impl->namedItem(name));
2667 Writes the XML representation of the node and all its children to
2668 the stream \a str. This function uses \a indent as the amount of
2669 space to indent the node.
2671 If this node is a document node, the encoding of text stream \a str's encoding is
2672 set by treating a processing instruction by name "xml" as an XML declaration, if such a one exists,
2673 and otherwise defaults to UTF-8. XML declarations are not processing instructions, but this
2674 behavior exists for historical reasons. If this node is not a document node,
2675 the text stream's encoding is used.
2677 If the document contains invalid XML characters or characters that cannot be
2678 encoded in the given encoding, the result and behavior is undefined.
2681 void QDomNode::save(QTextStream& str, int indent) const
2683 save(str, indent, QDomNode::EncodingFromDocument);
2687 If \a encodingPolicy is QDomNode::EncodingFromDocument, this function behaves as save(QTextStream &str, int indent).
2689 If \a encodingPolicy is EncodingFromTextStream and this node is a document node, this
2690 function behaves as save(QTextStream &str, int indent) with the exception that the encoding
2691 specified in the text stream \a str is used.
2693 If the document contains invalid XML characters or characters that cannot be
2694 encoded in the given encoding, the result and behavior is undefined.
2698 void QDomNode::save(QTextStream& str, int indent, EncodingPolicy encodingPolicy) const
2704 static_cast<const QDomDocumentPrivate *>(impl)->saveDocument(str, indent, encodingPolicy);
2706 IMPL->save(str, 1, indent);
2712 Writes the XML representation of the node \a node and all its
2713 children to the stream \a str.
2715 QTextStream& operator<<(QTextStream& str, const QDomNode& node)
2723 Returns true if the node is an attribute; otherwise returns false.
2725 If this function returns true, it does not imply that this object
2726 is a QDomAttribute; you can get the QDomAttribute with
2731 bool QDomNode::isAttr() const
2734 return impl->isAttr();
2739 Returns true if the node is a CDATA section; otherwise returns
2742 If this function returns true, it does not imply that this object
2743 is a QDomCDATASection; you can get the QDomCDATASection with
2746 \sa toCDATASection()
2748 bool QDomNode::isCDATASection() const
2751 return impl->isCDATASection();
2756 Returns true if the node is a document fragment; otherwise returns
2759 If this function returns true, it does not imply that this object
2760 is a QDomDocumentFragment; you can get the QDomDocumentFragment
2761 with toDocumentFragment().
2763 \sa toDocumentFragment()
2765 bool QDomNode::isDocumentFragment() const
2768 return impl->isDocumentFragment();
2773 Returns true if the node is a document; otherwise returns false.
2775 If this function returns true, it does not imply that this object
2776 is a QDomDocument; you can get the QDomDocument with toDocument().
2780 bool QDomNode::isDocument() const
2783 return impl->isDocument();
2788 Returns true if the node is a document type; otherwise returns
2791 If this function returns true, it does not imply that this object
2792 is a QDomDocumentType; you can get the QDomDocumentType with
2795 \sa toDocumentType()
2797 bool QDomNode::isDocumentType() const
2800 return impl->isDocumentType();
2805 Returns true if the node is an element; otherwise returns false.
2807 If this function returns true, it does not imply that this object
2808 is a QDomElement; you can get the QDomElement with toElement().
2812 bool QDomNode::isElement() const
2815 return impl->isElement();
2820 Returns true if the node is an entity reference; otherwise returns
2823 If this function returns true, it does not imply that this object
2824 is a QDomEntityReference; you can get the QDomEntityReference with
2825 toEntityReference().
2827 \sa toEntityReference()
2829 bool QDomNode::isEntityReference() const
2832 return impl->isEntityReference();
2837 Returns true if the node is a text node; otherwise returns false.
2839 If this function returns true, it does not imply that this object
2840 is a QDomText; you can get the QDomText with toText().
2844 bool QDomNode::isText() const
2847 return impl->isText();
2852 Returns true if the node is an entity; otherwise returns false.
2854 If this function returns true, it does not imply that this object
2855 is a QDomEntity; you can get the QDomEntity with toEntity().
2859 bool QDomNode::isEntity() const
2862 return impl->isEntity();
2867 Returns true if the node is a notation; otherwise returns false.
2869 If this function returns true, it does not imply that this object
2870 is a QDomNotation; you can get the QDomNotation with toNotation().
2874 bool QDomNode::isNotation() const
2877 return impl->isNotation();
2882 Returns true if the node is a processing instruction; otherwise
2885 If this function returns true, it does not imply that this object
2886 is a QDomProcessingInstruction; you can get the
2887 QProcessingInstruction with toProcessingInstruction().
2889 \sa toProcessingInstruction()
2891 bool QDomNode::isProcessingInstruction() const
2894 return impl->isProcessingInstruction();
2899 Returns true if the node is a character data node; otherwise
2902 If this function returns true, it does not imply that this object
2903 is a QDomCharacterData; you can get the QDomCharacterData with
2906 \sa toCharacterData()
2908 bool QDomNode::isCharacterData() const
2911 return impl->isCharacterData();
2916 Returns true if the node is a comment; otherwise returns false.
2918 If this function returns true, it does not imply that this object
2919 is a QDomComment; you can get the QDomComment with toComment().
2923 bool QDomNode::isComment() const
2926 return impl->isComment();
2933 Returns the first child element with tag name \a tagName if tagName is non-empty;
2934 otherwise returns the first child element. Returns a null element if no
2937 \sa lastChildElement() previousSiblingElement() nextSiblingElement()
2940 QDomElement QDomNode::firstChildElement(const QString &tagName) const
2942 for (QDomNode child = firstChild(); !child.isNull(); child = child.nextSibling()) {
2943 if (child.isElement()) {
2944 QDomElement elt = child.toElement();
2945 if (tagName.isEmpty() || elt.tagName() == tagName)
2949 return QDomElement();
2953 Returns the last child element with tag name \a tagName if tagName is non-empty;
2954 otherwise returns the last child element. Returns a null element if no
2957 \sa firstChildElement() previousSiblingElement() nextSiblingElement()
2960 QDomElement QDomNode::lastChildElement(const QString &tagName) const
2962 for (QDomNode child = lastChild(); !child.isNull(); child = child.previousSibling()) {
2963 if (child.isElement()) {
2964 QDomElement elt = child.toElement();
2965 if (tagName.isEmpty() || elt.tagName() == tagName)
2969 return QDomElement();
2973 Returns the next sibling element with tag name \a tagName if \a tagName
2974 is non-empty; otherwise returns any next sibling element.
2975 Returns a null element if no such sibling exists.
2977 \sa firstChildElement() previousSiblingElement() lastChildElement()
2980 QDomElement QDomNode::nextSiblingElement(const QString &tagName) const
2982 for (QDomNode sib = nextSibling(); !sib.isNull(); sib = sib.nextSibling()) {
2983 if (sib.isElement()) {
2984 QDomElement elt = sib.toElement();
2985 if (tagName.isEmpty() || elt.tagName() == tagName)
2989 return QDomElement();
2993 Returns the previous sibilng element with tag name \a tagName if \a tagName
2994 is non-empty; otherwise returns any previous sibling element.
2995 Returns a null element if no such sibling exists.
2997 \sa firstChildElement(), nextSiblingElement(), lastChildElement()
3000 QDomElement QDomNode::previousSiblingElement(const QString &tagName) const
3002 for (QDomNode sib = previousSibling(); !sib.isNull(); sib = sib.previousSibling()) {
3003 if (sib.isElement()) {
3004 QDomElement elt = sib.toElement();
3005 if (tagName.isEmpty() || elt.tagName() == tagName)
3009 return QDomElement();
3015 For nodes created by QDomDocument::setContent(), this function
3016 returns the line number in the XML document where the node was parsed.
3017 Otherwise, -1 is returned.
3019 \sa columnNumber(), QDomDocument::setContent()
3021 int QDomNode::lineNumber() const
3023 return impl ? impl->lineNumber : -1;
3029 For nodes created by QDomDocument::setContent(), this function
3030 returns the column number in the XML document where the node was parsed.
3031 Otherwise, -1 is returned.
3033 \sa lineNumber(), QDomDocument::setContent()
3035 int QDomNode::columnNumber() const
3037 return impl ? impl->columnNumber : -1;
3041 /**************************************************************
3043 * QDomNamedNodeMapPrivate
3045 **************************************************************/
3047 QDomNamedNodeMapPrivate::QDomNamedNodeMapPrivate(QDomNodePrivate* n) : ref(1)
3051 appendToParent = false;
3054 QDomNamedNodeMapPrivate::~QDomNamedNodeMapPrivate()
3059 QDomNamedNodeMapPrivate* QDomNamedNodeMapPrivate::clone(QDomNodePrivate* p)
3061 QScopedPointer<QDomNamedNodeMapPrivate> m(new QDomNamedNodeMapPrivate(p));
3062 m->readonly = readonly;
3063 m->appendToParent = appendToParent;
3065 QHash<QString, QDomNodePrivate*>::const_iterator it = map.constBegin();
3066 for (; it != map.constEnd(); ++it) {
3067 QDomNodePrivate *new_node = (*it)->cloneNode();
3068 new_node->setParent(p);
3069 m->setNamedItem(new_node);
3072 // we are no longer interested in ownership
3077 void QDomNamedNodeMapPrivate::clearMap()
3079 // Dereference all of our children if we took references
3080 if (!appendToParent) {
3081 QHash<QString, QDomNodePrivate *>::const_iterator it = map.constBegin();
3082 for (; it != map.constEnd(); ++it)
3083 if (!(*it)->ref.deref())
3089 QDomNodePrivate* QDomNamedNodeMapPrivate::namedItem(const QString& name) const
3091 QDomNodePrivate* p = map[name];
3095 QDomNodePrivate* QDomNamedNodeMapPrivate::namedItemNS(const QString& nsURI, const QString& localName) const
3097 QHash<QString, QDomNodePrivate *>::const_iterator it = map.constBegin();
3099 for (; it != map.constEnd(); ++it) {
3101 if (!n->prefix.isNull()) {
3102 // node has a namespace
3103 if (n->namespaceURI == nsURI && n->name == localName)
3110 QDomNodePrivate* QDomNamedNodeMapPrivate::setNamedItem(QDomNodePrivate* arg)
3112 if (readonly || !arg)
3116 return parent->appendChild(arg);
3118 QDomNodePrivate *n = map.value(arg->nodeName());
3119 // We take a reference
3121 map.insertMulti(arg->nodeName(), arg);
3125 QDomNodePrivate* QDomNamedNodeMapPrivate::setNamedItemNS(QDomNodePrivate* arg)
3127 if (readonly || !arg)
3131 return parent->appendChild(arg);
3133 if (!arg->prefix.isNull()) {
3134 // node has a namespace
3135 QDomNodePrivate *n = namedItemNS(arg->namespaceURI, arg->name);
3136 // We take a reference
3138 map.insertMulti(arg->nodeName(), arg);
3141 // ### check the following code if it is ok
3142 return setNamedItem(arg);
3146 QDomNodePrivate* QDomNamedNodeMapPrivate::removeNamedItem(const QString& name)
3151 QDomNodePrivate* p = namedItem(name);
3155 return parent->removeChild(p);
3157 map.remove(p->nodeName());
3158 // We took a reference, so we have to free one here
3163 QDomNodePrivate* QDomNamedNodeMapPrivate::item(int index) const
3165 if ((uint)index >= length())
3167 return *(map.constBegin() + index);
3170 // ### Qt 5: convert all length/size() functions in QDom to use int instead of uint.
3171 uint QDomNamedNodeMapPrivate::length() const
3176 bool QDomNamedNodeMapPrivate::contains(const QString& name) const
3178 return map.value(name) != 0;
3181 bool QDomNamedNodeMapPrivate::containsNS(const QString& nsURI, const QString & localName) const
3183 return namedItemNS(nsURI, localName) != 0;
3186 /**************************************************************
3190 **************************************************************/
3192 #define IMPL ((QDomNamedNodeMapPrivate*)impl)
3195 \class QDomNamedNodeMap
3197 \brief The QDomNamedNodeMap class contains a collection of nodes
3198 that can be accessed by name.
3203 Note that QDomNamedNodeMap does not inherit from QDomNodeList.
3204 QDomNamedNodeMaps do not provide any specific node ordering.
3205 Although nodes in a QDomNamedNodeMap may be accessed by an ordinal
3206 index, this is simply to allow a convenient enumeration of the
3207 contents of a QDomNamedNodeMap, and does not imply that the DOM
3208 specifies an ordering of the nodes.
3210 The QDomNamedNodeMap is used in three places:
3212 \i QDomDocumentType::entities() returns a map of all entities
3213 described in the DTD.
3214 \i QDomDocumentType::notations() returns a map of all notations
3215 described in the DTD.
3216 \i QDomNode::attributes() returns a map of all attributes of an
3220 Items in the map are identified by the name which QDomNode::name()
3221 returns. Nodes are retrieved using namedItem(), namedItemNS() or
3222 item(). New nodes are inserted with setNamedItem() or
3223 setNamedItemNS() and removed with removeNamedItem() or
3224 removeNamedItemNS(). Use contains() to see if an item with the
3225 given name is in the named node map. The number of items is
3226 returned by length().
3228 Terminology: in this class we use "item" and "node"
3233 Constructs an empty named node map.
3235 QDomNamedNodeMap::QDomNamedNodeMap()
3241 Constructs a copy of \a n.
3243 QDomNamedNodeMap::QDomNamedNodeMap(const QDomNamedNodeMap &n)
3250 QDomNamedNodeMap::QDomNamedNodeMap(QDomNamedNodeMapPrivate *n)
3258 Assigns \a n to this named node map.
3260 QDomNamedNodeMap& QDomNamedNodeMap::operator=(const QDomNamedNodeMap &n)
3264 if (impl && !impl->ref.deref())
3271 Returns true if \a n and this named node map are equal; otherwise
3274 bool QDomNamedNodeMap::operator== (const QDomNamedNodeMap& n) const
3276 return (impl == n.impl);
3280 Returns true if \a n and this named node map are not equal;
3281 otherwise returns false.
3283 bool QDomNamedNodeMap::operator!= (const QDomNamedNodeMap& n) const
3285 return (impl != n.impl);
3289 Destroys the object and frees its resources.
3291 QDomNamedNodeMap::~QDomNamedNodeMap()
3293 if (impl && !impl->ref.deref())
3298 Returns the node called \a name.
3300 If the named node map does not contain such a node, a \link
3301 QDomNode::isNull() null node\endlink is returned. A node's name is
3302 the name returned by QDomNode::nodeName().
3304 \sa setNamedItem() namedItemNS()
3306 QDomNode QDomNamedNodeMap::namedItem(const QString& name) const
3310 return QDomNode(IMPL->namedItem(name));
3314 Inserts the node \a newNode into the named node map. The name used
3315 by the map is the node name of \a newNode as returned by
3316 QDomNode::nodeName().
3318 If the new node replaces an existing node, i.e. the map contains a
3319 node with the same name, the replaced node is returned.
3321 \sa namedItem() removeNamedItem() setNamedItemNS()
3323 QDomNode QDomNamedNodeMap::setNamedItem(const QDomNode& newNode)
3327 return QDomNode(IMPL->setNamedItem((QDomNodePrivate*)newNode.impl));
3331 Removes the node called \a name from the map.
3333 The function returns the removed node or a \link
3334 QDomNode::isNull() null node\endlink if the map did not contain a
3335 node called \a name.
3337 \sa setNamedItem() namedItem() removeNamedItemNS()
3339 QDomNode QDomNamedNodeMap::removeNamedItem(const QString& name)
3343 return QDomNode(IMPL->removeNamedItem(name));
3347 Retrieves the node at position \a index.
3349 This can be used to iterate over the map. Note that the nodes in
3350 the map are ordered arbitrarily.
3354 QDomNode QDomNamedNodeMap::item(int index) const
3358 return QDomNode(IMPL->item(index));
3362 Returns the node associated with the local name \a localName and
3363 the namespace URI \a nsURI.
3365 If the map does not contain such a node, a \link
3366 QDomNode::isNull() null node\endlink is returned.
3368 \sa setNamedItemNS() namedItem()
3370 QDomNode QDomNamedNodeMap::namedItemNS(const QString& nsURI, const QString& localName) const
3374 return QDomNode(IMPL->namedItemNS(nsURI, localName));
3378 Inserts the node \a newNode in the map. If a node with the same
3379 namespace URI and the same local name already exists in the map,
3380 it is replaced by \a newNode. If the new node replaces an existing
3381 node, the replaced node is returned.
3383 \sa namedItemNS() removeNamedItemNS() setNamedItem()
3385 QDomNode QDomNamedNodeMap::setNamedItemNS(const QDomNode& newNode)
3389 return QDomNode(IMPL->setNamedItemNS((QDomNodePrivate*)newNode.impl));
3393 Removes the node with the local name \a localName and the
3394 namespace URI \a nsURI from the map.
3396 The function returns the removed node or a \link
3397 QDomNode::isNull() null node\endlink if the map did not contain a
3398 node with the local name \a localName and the namespace URI \a
3401 \sa setNamedItemNS() namedItemNS() removeNamedItem()
3403 QDomNode QDomNamedNodeMap::removeNamedItemNS(const QString& nsURI, const QString& localName)
3407 QDomNodePrivate *n = IMPL->namedItemNS(nsURI, localName);
3410 return QDomNode(IMPL->removeNamedItem(n->name));
3414 Returns the number of nodes in the map.
3418 uint QDomNamedNodeMap::length() const
3422 return IMPL->length();
3426 \fn bool QDomNamedNodeMap::isEmpty() const
3428 Returns true if the map is empty; otherwise returns false. This function is
3429 provided for Qt API consistency.
3433 \fn int QDomNamedNodeMap::count() const
3435 This function is provided for Qt API consistency. It is equivalent to length().
3439 \fn int QDomNamedNodeMap::size() const
3441 This function is provided for Qt API consistency. It is equivalent to length().
3445 Returns true if the map contains a node called \a name; otherwise
3448 \bold{Note:} This function does not take the presence of namespaces into account.
3449 Use namedItemNS() to test whether the map contains a node with a specific namespace
3452 bool QDomNamedNodeMap::contains(const QString& name) const
3456 return IMPL->contains(name);
3461 /**************************************************************
3463 * QDomDocumentTypePrivate
3465 **************************************************************/
3467 QDomDocumentTypePrivate::QDomDocumentTypePrivate(QDomDocumentPrivate* doc, QDomNodePrivate* parent)
3468 : QDomNodePrivate(doc, parent)
3473 QDomDocumentTypePrivate::QDomDocumentTypePrivate(QDomDocumentTypePrivate* n, bool deep)
3474 : QDomNodePrivate(n, deep)
3477 // Refill the maps with our new children
3478 QDomNodePrivate* p = first;
3481 // Dont use normal insert function since we would create infinite recursion
3482 entities->map.insertMulti(p->nodeName(), p);
3483 if (p->isNotation())
3484 // Dont use normal insert function since we would create infinite recursion
3485 notations->map.insertMulti(p->nodeName(), p);
3490 QDomDocumentTypePrivate::~QDomDocumentTypePrivate()
3492 if (!entities->ref.deref())
3494 if (!notations->ref.deref())
3498 void QDomDocumentTypePrivate::init()
3500 entities = new QDomNamedNodeMapPrivate(this);
3502 notations = new QDomNamedNodeMapPrivate(this);
3505 internalSubset.clear();
3507 entities->setAppendToParent(true);
3508 notations->setAppendToParent(true);
3515 QDomNodePrivate* QDomDocumentTypePrivate::cloneNode(bool deep)
3517 QDomNodePrivate* p = new QDomDocumentTypePrivate(this, deep);
3518 // We are not interested in this node
3523 QDomNodePrivate* QDomDocumentTypePrivate::insertBefore(QDomNodePrivate* newChild, QDomNodePrivate* refChild)
3525 // Call the origianl implementation
3526 QDomNodePrivate* p = QDomNodePrivate::insertBefore(newChild, refChild);
3528 if (p && p->isEntity())
3529 entities->map.insertMulti(p->nodeName(), p);
3530 else if (p && p->isNotation())
3531 notations->map.insertMulti(p->nodeName(), p);
3536 QDomNodePrivate* QDomDocumentTypePrivate::insertAfter(QDomNodePrivate* newChild, QDomNodePrivate* refChild)
3538 // Call the origianl implementation
3539 QDomNodePrivate* p = QDomNodePrivate::insertAfter(newChild, refChild);
3541 if (p && p->isEntity())
3542 entities->map.insertMulti(p->nodeName(), p);
3543 else if (p && p->isNotation())
3544 notations->map.insertMulti(p->nodeName(), p);
3549 QDomNodePrivate* QDomDocumentTypePrivate::replaceChild(QDomNodePrivate* newChild, QDomNodePrivate* oldChild)
3551 // Call the origianl implementation
3552 QDomNodePrivate* p = QDomNodePrivate::replaceChild(newChild, oldChild);
3555 if (oldChild && oldChild->isEntity())
3556 entities->map.remove(oldChild->nodeName());
3557 else if (oldChild && oldChild->isNotation())
3558 notations->map.remove(oldChild->nodeName());
3561 entities->map.insertMulti(p->nodeName(), p);
3562 else if (p->isNotation())
3563 notations->map.insertMulti(p->nodeName(), p);
3569 QDomNodePrivate* QDomDocumentTypePrivate::removeChild(QDomNodePrivate* oldChild)
3571 // Call the origianl implementation
3572 QDomNodePrivate* p = QDomNodePrivate::removeChild( oldChild);
3574 if (p && p->isEntity())
3575 entities->map.remove(p->nodeName());
3576 else if (p && p->isNotation())
3577 notations->map.remove(p ->nodeName());
3582 QDomNodePrivate* QDomDocumentTypePrivate::appendChild(QDomNodePrivate* newChild)
3584 return insertAfter(newChild, 0);
3587 static QString quotedValue(const QString &data)
3589 QChar quote = data.indexOf(QLatin1Char('\'')) == -1
3592 return quote + data + quote;
3595 void QDomDocumentTypePrivate::save(QTextStream& s, int, int indent) const
3600 s << "<!DOCTYPE " << name;
3602 if (!publicId.isNull()) {
3603 s << " PUBLIC " << quotedValue(publicId);
3604 if (!systemId.isNull()) {
3605 s << ' ' << quotedValue(systemId);
3607 } else if (!systemId.isNull()) {
3608 s << " SYSTEM " << quotedValue(systemId);
3611 if (entities->length()>0 || notations->length()>0) {
3614 QHash<QString, QDomNodePrivate *>::const_iterator it2 = notations->map.constBegin();
3615 for (; it2 != notations->map.constEnd(); ++it2)
3616 (*it2)->save(s, 0, indent);
3618 QHash<QString, QDomNodePrivate *>::const_iterator it = entities->map.constBegin();
3619 for (; it != entities->map.constEnd(); ++it)
3620 (*it)->save(s, 0, indent);
3628 /**************************************************************
3632 **************************************************************/
3634 #define IMPL ((QDomDocumentTypePrivate*)impl)
3637 \class QDomDocumentType
3639 \brief The QDomDocumentType class is the representation of the DTD
3640 in the document tree.
3645 The QDomDocumentType class allows read-only access to some of the
3646 data structures in the DTD: it can return a map of all entities()
3647 and notations(). In addition the function name() returns the name
3648 of the document type as specified in the <!DOCTYPE name>
3649 tag. This class also provides the publicId(), systemId() and
3650 internalSubset() functions.
3656 Creates an empty QDomDocumentType object.
3658 QDomDocumentType::QDomDocumentType() : QDomNode()
3663 Constructs a copy of \a n.
3665 The data of the copy is shared (shallow copy): modifying one node
3666 will also change the other. If you want to make a deep copy, use
3669 QDomDocumentType::QDomDocumentType(const QDomDocumentType& n)
3674 QDomDocumentType::QDomDocumentType(QDomDocumentTypePrivate* n)
3680 Assigns \a n to this document type.
3682 The data of the copy is shared (shallow copy): modifying one node
3683 will also change the other. If you want to make a deep copy, use
3686 QDomDocumentType& QDomDocumentType::operator= (const QDomDocumentType& n)
3688 return (QDomDocumentType&) QDomNode::operator=(n);
3692 Returns the name of the document type as specified in the
3693 <!DOCTYPE name> tag.
3697 QString QDomDocumentType::name() const
3701 return IMPL->nodeName();
3705 Returns a map of all entities described in the DTD.
3707 QDomNamedNodeMap QDomDocumentType::entities() const
3710 return QDomNamedNodeMap();
3711 return QDomNamedNodeMap(IMPL->entities);
3715 Returns a map of all notations described in the DTD.
3717 QDomNamedNodeMap QDomDocumentType::notations() const
3720 return QDomNamedNodeMap();
3721 return QDomNamedNodeMap(IMPL->notations);
3725 Returns the public identifier of the external DTD subset or
3726 an empty string if there is no public identifier.
3728 \sa systemId() internalSubset() QDomImplementation::createDocumentType()
3730 QString QDomDocumentType::publicId() const
3734 return IMPL->publicId;
3738 Returns the system identifier of the external DTD subset or
3739 an empty string if there is no system identifier.
3741 \sa publicId() internalSubset() QDomImplementation::createDocumentType()
3743 QString QDomDocumentType::systemId() const
3747 return IMPL->systemId;
3751 Returns the internal subset of the document type or an empty
3752 string if there is no internal subset.
3754 \sa publicId() systemId()
3756 QString QDomDocumentType::internalSubset() const
3760 return IMPL->internalSubset;
3764 Are these needed at all? The only difference when removing these
3765 two methods in all subclasses is that we'd get a different type
3770 \fn QDomNode::NodeType QDomDocumentType::nodeType() const
3772 Returns \c DocumentTypeNode.
3774 \sa isDocumentType() QDomNode::toDocumentType()
3779 /**************************************************************
3781 * QDomDocumentFragmentPrivate
3783 **************************************************************/
3785 QDomDocumentFragmentPrivate::QDomDocumentFragmentPrivate(QDomDocumentPrivate* doc, QDomNodePrivate* parent)
3786 : QDomNodePrivate(doc, parent)
3788 name = QLatin1String("#document-fragment");
3791 QDomDocumentFragmentPrivate::QDomDocumentFragmentPrivate(QDomNodePrivate* n, bool deep)
3792 : QDomNodePrivate(n, deep)
3796 QDomNodePrivate* QDomDocumentFragmentPrivate::cloneNode(bool deep)
3798 QDomNodePrivate* p = new QDomDocumentFragmentPrivate(this, deep);
3799 // We are not interested in this node
3804 /**************************************************************
3806 * QDomDocumentFragment
3808 **************************************************************/
3811 \class QDomDocumentFragment
3813 \brief The QDomDocumentFragment class is a tree of QDomNodes which is not usually a complete QDomDocument.
3818 If you want to do complex tree operations it is useful to have a
3819 lightweight class to store nodes and their relations.
3820 QDomDocumentFragment stores a subtree of a document which does not
3821 necessarily represent a well-formed XML document.
3823 QDomDocumentFragment is also useful if you want to group several
3824 nodes in a list and insert them all together as children of some
3825 node. In these cases QDomDocumentFragment can be used as a
3826 temporary container for this list of children.
3828 The most important feature of QDomDocumentFragment is that it is
3829 treated in a special way by QDomNode::insertAfter(),
3830 QDomNode::insertBefore(), QDomNode::replaceChild() and
3831 QDomNode::appendChild(): instead of inserting the fragment itself, all
3832 the fragment's children are inserted.
3836 Constructs an empty document fragment.
3838 QDomDocumentFragment::QDomDocumentFragment()
3842 QDomDocumentFragment::QDomDocumentFragment(QDomDocumentFragmentPrivate* n)
3848 Constructs a copy of \a x.
3850 The data of the copy is shared (shallow copy): modifying one node
3851 will also change the other. If you want to make a deep copy, use
3854 QDomDocumentFragment::QDomDocumentFragment(const QDomDocumentFragment& x)
3860 Assigns \a x to this DOM document fragment.
3862 The data of the copy is shared (shallow copy): modifying one node
3863 will also change the other. If you want to make a deep copy, use
3866 QDomDocumentFragment& QDomDocumentFragment::operator= (const QDomDocumentFragment& x)
3868 return (QDomDocumentFragment&) QDomNode::operator=(x);
3872 \fn QDomNode::NodeType QDomDocumentFragment::nodeType() const
3874 Returns \c DocumentFragment.
3876 \sa isDocumentFragment() QDomNode::toDocumentFragment()
3879 /**************************************************************
3881 * QDomCharacterDataPrivate
3883 **************************************************************/
3885 QDomCharacterDataPrivate::QDomCharacterDataPrivate(QDomDocumentPrivate* d, QDomNodePrivate* p,
3886 const QString& data)
3887 : QDomNodePrivate(d, p)
3890 name = QLatin1String("#character-data");
3893 QDomCharacterDataPrivate::QDomCharacterDataPrivate(QDomCharacterDataPrivate* n, bool deep)
3894 : QDomNodePrivate(n, deep)
3898 QDomNodePrivate* QDomCharacterDataPrivate::cloneNode(bool deep)
3900 QDomNodePrivate* p = new QDomCharacterDataPrivate(this, deep);
3901 // We are not interested in this node
3906 uint QDomCharacterDataPrivate::dataLength() const
3908 return value.length();
3911 QString QDomCharacterDataPrivate::substringData(unsigned long offset, unsigned long n) const
3913 return value.mid(offset, n);
3916 void QDomCharacterDataPrivate::insertData(unsigned long offset, const QString& arg)
3918 value.insert(offset, arg);
3921 void QDomCharacterDataPrivate::deleteData(unsigned long offset, unsigned long n)
3923 value.remove(offset, n);
3926 void QDomCharacterDataPrivate::replaceData(unsigned long offset, unsigned long n, const QString& arg)
3928 value.replace(offset, n, arg);
3931 void QDomCharacterDataPrivate::appendData(const QString& arg)
3936 /**************************************************************
3940 **************************************************************/
3942 #define IMPL ((QDomCharacterDataPrivate*)impl)
3945 \class QDomCharacterData
3947 \brief The QDomCharacterData class represents a generic string in the DOM.
3952 Character data as used in XML specifies a generic data string.
3953 More specialized versions of this class are QDomText, QDomComment
3954 and QDomCDATASection.
3956 The data string is set with setData() and retrieved with data().
3957 You can retrieve a portion of the data string using
3958 substringData(). Extra data can be appended with appendData(), or
3959 inserted with insertData(). Portions of the data string can be
3960 deleted with deleteData() or replaced with replaceData(). The
3961 length of the data string is returned by length().
3963 The node type of the node containing this character data is
3964 returned by nodeType().
3966 \sa QDomText QDomComment QDomCDATASection
3970 Constructs an empty character data object.
3972 QDomCharacterData::QDomCharacterData()
3977 Constructs a copy of \a x.
3979 The data of the copy is shared (shallow copy): modifying one node
3980 will also change the other. If you want to make a deep copy, use
3983 QDomCharacterData::QDomCharacterData(const QDomCharacterData& x)
3988 QDomCharacterData::QDomCharacterData(QDomCharacterDataPrivate* n)
3994 Assigns \a x to this character data.
3996 The data of the copy is shared (shallow copy): modifying one node
3997 will also change the other. If you want to make a deep copy, use
4000 QDomCharacterData& QDomCharacterData::operator= (const QDomCharacterData& x)
4002 return (QDomCharacterData&) QDomNode::operator=(x);
4006 Returns the string stored in this object.
4008 If the node is a \link isNull() null node\endlink, it will return
4011 QString QDomCharacterData::data() const
4015 return impl->nodeValue();
4019 Sets this object's string to \a v.
4021 void QDomCharacterData::setData(const QString& v)
4024 impl->setNodeValue(v);
4028 Returns the length of the stored string.
4030 uint QDomCharacterData::length() const
4033 return IMPL->dataLength();
4038 Returns the substring of length \a count from position \a offset.
4040 QString QDomCharacterData::substringData(unsigned long offset, unsigned long count)
4044 return IMPL->substringData(offset, count);
4048 Appends the string \a arg to the stored string.
4050 void QDomCharacterData::appendData(const QString& arg)
4053 IMPL->appendData(arg);
4057 Inserts the string \a arg into the stored string at position \a offset.
4059 void QDomCharacterData::insertData(unsigned long offset, const QString& arg)
4062 IMPL->insertData(offset, arg);
4066 Deletes a substring of length \a count from position \a offset.
4068 void QDomCharacterData::deleteData(unsigned long offset, unsigned long count)
4071 IMPL->deleteData(offset, count);
4075 Replaces the substring of length \a count starting at position \a
4076 offset with the string \a arg.
4078 void QDomCharacterData::replaceData(unsigned long offset, unsigned long count, const QString& arg)
4081 IMPL->replaceData(offset, count, arg);
4085 Returns the type of node this object refers to (i.e. \c TextNode,
4086 \c CDATASectionNode, \c CommentNode or \c CharacterDataNode). For
4087 a \link isNull() null node\endlink, returns \c CharacterDataNode.
4089 QDomNode::NodeType QDomCharacterData::nodeType() const
4092 return CharacterDataNode;
4093 return QDomNode::nodeType();
4098 /**************************************************************
4102 **************************************************************/
4104 QDomAttrPrivate::QDomAttrPrivate(QDomDocumentPrivate* d, QDomNodePrivate* parent, const QString& name_)
4105 : QDomNodePrivate(d, parent)
4108 m_specified = false;
4111 QDomAttrPrivate::QDomAttrPrivate(QDomDocumentPrivate* d, QDomNodePrivate* p, const QString& nsURI, const QString& qName)
4112 : QDomNodePrivate(d, p)
4114 qt_split_namespace(prefix, name, qName, !nsURI.isNull());
4115 namespaceURI = nsURI;
4116 createdWithDom1Interface = false;
4117 m_specified = false;
4120 QDomAttrPrivate::QDomAttrPrivate(QDomAttrPrivate* n, bool deep)
4121 : QDomNodePrivate(n, deep)
4123 m_specified = n->specified();
4126 void QDomAttrPrivate::setNodeValue(const QString& v)
4129 QDomTextPrivate *t = new QDomTextPrivate(0, this, v);
4130 // keep the refcount balanced: appendChild() does a ref anyway.
4133 delete removeChild(first);
4138 QDomNodePrivate* QDomAttrPrivate::cloneNode(bool deep)
4140 QDomNodePrivate* p = new QDomAttrPrivate(this, deep);
4141 // We are not interested in this node
4146 bool QDomAttrPrivate::specified() const
4152 Encode & escape \a str. Yes, it makes no sense to return a QString,
4153 but is so for legacy reasons.
4155 Remember that content produced should be able to roundtrip with 2.11 End-of-Line Handling
4156 and 3.3.3 Attribute-Value Normalization.
4158 If \a performAVN is true, characters will be escaped to survive Attribute Value Normalization.
4159 If \a encodeEOLs is true, characters will be escaped to survive End-of-Line Handling.
4161 static QString encodeText(const QString &str,
4163 const bool encodeQuotes = true,
4164 const bool performAVN = false,
4165 const bool encodeEOLs = false)
4167 #ifdef QT_NO_TEXTCODEC
4170 const QTextCodec *const codec = s.codec();
4173 QString retval(str);
4174 int len = retval.length();
4178 const QChar ati(retval.at(i));
4180 if (ati == QLatin1Char('<')) {
4181 retval.replace(i, 1, QLatin1String("<"));
4184 } else if (encodeQuotes && (ati == QLatin1Char('"'))) {
4185 retval.replace(i, 1, QLatin1String("""));
4188 } else if (ati == QLatin1Char('&')) {
4189 retval.replace(i, 1, QLatin1String("&"));
4192 } else if (ati == QLatin1Char('>') && i >= 2 && retval[i - 1] == QLatin1Char(']') && retval[i - 2] == QLatin1Char(']')) {
4193 retval.replace(i, 1, QLatin1String(">"));
4196 } else if (performAVN &&
4197 (ati == QChar(0xA) ||
4198 ati == QChar(0xD) ||
4199 ati == QChar(0x9))) {
4200 const QString replacement(QLatin1String("&#x") + QString::number(ati.unicode(), 16) + QLatin1Char(';'));
4201 retval.replace(i, 1, replacement);
4202 i += replacement.length();
4203 len += replacement.length() - 1;
4204 } else if (encodeEOLs && ati == QChar(0xD)) {
4205 retval.replace(i, 1, QLatin1String("
")); // Replace a single 0xD with a ref for 0xD
4209 #ifndef QT_NO_TEXTCODEC
4210 if(codec->canEncode(ati))
4215 // We have to use a character reference to get it through.
4216 const ushort codepoint(ati.unicode());
4217 const QString replacement(QLatin1String("&#x") + QString::number(codepoint, 16) + QLatin1Char(';'));
4218 retval.replace(i, 1, replacement);
4219 i += replacement.length();
4220 len += replacement.length() - 1;
4228 void QDomAttrPrivate::save(QTextStream& s, int, int) const
4230 if (namespaceURI.isNull()) {
4231 s << name << "=\"" << encodeText(value, s, true, true) << '\"';
4233 s << prefix << ':' << name << "=\"" << encodeText(value, s, true, true) << '\"';
4234 /* This is a fix for 138243, as good as it gets.
4236 * QDomElementPrivate::save() output a namespace declaration if
4237 * the element is in a namespace, no matter what. This function do as well, meaning
4238 * that we get two identical namespace declaration if we don't have the if-
4241 * This doesn't work when the parent element has the same prefix as us but
4242 * a different namespace. However, this can only occur by the user modifying the element,
4243 * and we don't do fixups by that anyway, and hence it's the user responsibility to not
4244 * arrive in those situations. */
4246 ownerNode->prefix != prefix) {
4247 s << " xmlns:" << prefix << "=\"" << encodeText(namespaceURI, s, true, true) << '\"';
4252 /**************************************************************
4256 **************************************************************/
4258 #define IMPL ((QDomAttrPrivate*)impl)
4263 \brief The QDomAttr class represents one attribute of a QDomElement.
4268 For example, the following piece of XML produces an element with
4269 no children, but two attributes:
4271 \snippet doc/src/snippets/code/src_xml_dom_qdom.cpp 7
4273 You can access the attributes of an element with code like this:
4275 \snippet doc/src/snippets/code/src_xml_dom_qdom.cpp 8
4277 This example also shows that changing an attribute received from
4278 an element changes the attribute of the element. If you do not
4279 want to change the value of the element's attribute you must
4280 use cloneNode() to get an independent copy of the attribute.
4282 QDomAttr can return the name() and value() of an attribute. An
4283 attribute's value is set with setValue(). If specified() returns
4284 true the value was set with setValue(). The node this
4285 attribute is attached to (if any) is returned by ownerElement().
4287 For further information about the Document Object Model see
4288 \l{http://www.w3.org/TR/REC-DOM-Level-1/} and
4289 \l{http://www.w3.org/TR/DOM-Level-2-Core/}.
4290 For a more general introduction of the DOM implementation see the
4291 QDomDocument documentation.
4296 Constructs an empty attribute.
4298 QDomAttr::QDomAttr()
4303 Constructs a copy of \a x.
4305 The data of the copy is shared (shallow copy): modifying one node
4306 will also change the other. If you want to make a deep copy, use
4309 QDomAttr::QDomAttr(const QDomAttr& x)
4314 QDomAttr::QDomAttr(QDomAttrPrivate* n)
4320 Assigns \a x to this DOM attribute.
4322 The data of the copy is shared (shallow copy): modifying one node
4323 will also change the other. If you want to make a deep copy, use
4326 QDomAttr& QDomAttr::operator= (const QDomAttr& x)
4328 return (QDomAttr&) QDomNode::operator=(x);
4332 Returns the attribute's name.
4334 QString QDomAttr::name() const
4338 return impl->nodeName();
4342 Returns true if the attribute has been set by the user with setValue().
4343 Returns false if the value hasn't been specified or set.
4347 bool QDomAttr::specified() const
4351 return IMPL->specified();
4355 Returns the element node this attribute is attached to or a \link
4356 QDomNode::isNull() null node\endlink if this attribute is not
4357 attached to any element.
4359 QDomElement QDomAttr::ownerElement() const
4361 Q_ASSERT(impl->parent());
4362 if (!impl->parent()->isElement())
4363 return QDomElement();
4364 return QDomElement((QDomElementPrivate*)(impl->parent()));
4368 Returns the value of the attribute or an empty string if the
4369 attribute has not been specified.
4371 \sa specified() setValue()
4373 QString QDomAttr::value() const
4377 return impl->nodeValue();
4381 Sets the attribute's value to \a v.
4385 void QDomAttr::setValue(const QString& v)
4389 impl->setNodeValue(v);
4390 IMPL->m_specified = true;
4394 \fn QDomNode::NodeType QDomAttr::nodeType() const
4396 Returns \link QDomNode::NodeType AttributeNode\endlink.
4401 /**************************************************************
4403 * QDomElementPrivate
4405 **************************************************************/
4407 QDomElementPrivate::QDomElementPrivate(QDomDocumentPrivate* d, QDomNodePrivate* p,
4408 const QString& tagname)
4409 : QDomNodePrivate(d, p)
4412 m_attr = new QDomNamedNodeMapPrivate(this);
4415 QDomElementPrivate::QDomElementPrivate(QDomDocumentPrivate* d, QDomNodePrivate* p,
4416 const QString& nsURI, const QString& qName)
4417 : QDomNodePrivate(d, p)
4419 qt_split_namespace(prefix, name, qName, !nsURI.isNull());
4420 namespaceURI = nsURI;
4421 createdWithDom1Interface = false;
4422 m_attr = new QDomNamedNodeMapPrivate(this);
4425 QDomElementPrivate::QDomElementPrivate(QDomElementPrivate* n, bool deep) :
4426 QDomNodePrivate(n, deep)
4428 m_attr = n->m_attr->clone(this);
4429 // Reference is down to 0, so we set it to 1 here.
4433 QDomElementPrivate::~QDomElementPrivate()
4435 if (!m_attr->ref.deref())
4439 QDomNodePrivate* QDomElementPrivate::cloneNode(bool deep)
4441 QDomNodePrivate* p = new QDomElementPrivate(this, deep);
4442 // We are not interested in this node
4447 QString QDomElementPrivate::attribute(const QString& name_, const QString& defValue) const
4449 QDomNodePrivate* n = m_attr->namedItem(name_);
4453 return n->nodeValue();
4456 QString QDomElementPrivate::attributeNS(const QString& nsURI, const QString& localName, const QString& defValue) const
4458 QDomNodePrivate* n = m_attr->namedItemNS(nsURI, localName);
4462 return n->nodeValue();
4465 void QDomElementPrivate::setAttribute(const QString& aname, const QString& newValue)
4467 QDomNodePrivate* n = m_attr->namedItem(aname);
4469 n = new QDomAttrPrivate(ownerDocument(), this, aname);
4470 n->setNodeValue(newValue);
4472 // Referencing is done by the map, so we set the reference counter back
4473 // to 0 here. This is ok since we created the QDomAttrPrivate.
4475 m_attr->setNamedItem(n);
4477 n->setNodeValue(newValue);
4481 void QDomElementPrivate::setAttributeNS(const QString& nsURI, const QString& qName, const QString& newValue)
4483 QString prefix, localName;
4484 qt_split_namespace(prefix, localName, qName, true);
4485 QDomNodePrivate* n = m_attr->namedItemNS(nsURI, localName);
4487 n = new QDomAttrPrivate(ownerDocument(), this, nsURI, qName);
4488 n->setNodeValue(newValue);
4490 // Referencing is done by the map, so we set the reference counter back
4491 // to 0 here. This is ok since we created the QDomAttrPrivate.
4493 m_attr->setNamedItem(n);
4495 n->setNodeValue(newValue);
4500 void QDomElementPrivate::removeAttribute(const QString& aname)
4502 QDomNodePrivate* p = m_attr->removeNamedItem(aname);
4503 if (p && p->ref.load() == 0)
4507 QDomAttrPrivate* QDomElementPrivate::attributeNode(const QString& aname)
4509 return (QDomAttrPrivate*)m_attr->namedItem(aname);
4512 QDomAttrPrivate* QDomElementPrivate::attributeNodeNS(const QString& nsURI, const QString& localName)
4514 return (QDomAttrPrivate*)m_attr->namedItemNS(nsURI, localName);
4517 QDomAttrPrivate* QDomElementPrivate::setAttributeNode(QDomAttrPrivate* newAttr)
4519 QDomNodePrivate* n = m_attr->namedItem(newAttr->nodeName());
4521 // Referencing is done by the maps
4522 m_attr->setNamedItem(newAttr);
4524 newAttr->setParent(this);
4526 return (QDomAttrPrivate*)n;
4529 QDomAttrPrivate* QDomElementPrivate::setAttributeNodeNS(QDomAttrPrivate* newAttr)
4531 QDomNodePrivate* n = 0;
4532 if (!newAttr->prefix.isNull())
4533 n = m_attr->namedItemNS(newAttr->namespaceURI, newAttr->name);
4535 // Referencing is done by the maps
4536 m_attr->setNamedItem(newAttr);
4538 return (QDomAttrPrivate*)n;
4541 QDomAttrPrivate* QDomElementPrivate::removeAttributeNode(QDomAttrPrivate* oldAttr)
4543 return (QDomAttrPrivate*)m_attr->removeNamedItem(oldAttr->nodeName());
4546 bool QDomElementPrivate::hasAttribute(const QString& aname)
4548 return m_attr->contains(aname);
4551 bool QDomElementPrivate::hasAttributeNS(const QString& nsURI, const QString& localName)
4553 return m_attr->containsNS(nsURI, localName);
4556 QString QDomElementPrivate::text()
4558 QString t(QLatin1String(""));
4560 QDomNodePrivate* p = first;
4562 if (p->isText() || p->isCDATASection())
4563 t += p->nodeValue();
4564 else if (p->isElement())
4565 t += ((QDomElementPrivate*)p)->text();
4572 void QDomElementPrivate::save(QTextStream& s, int depth, int indent) const
4574 if (!(prev && prev->isText()))
4575 s << QString(indent < 1 ? 0 : depth * indent, QLatin1Char(' '));
4577 QString qName(name);
4578 QString nsDecl(QLatin1String(""));
4579 if (!namespaceURI.isNull()) {
4582 * If we still have QDom, optimize this so that we only declare namespaces that are not
4583 * yet declared. We loose default namespace mappings, so maybe we should rather store
4584 * the information that we get from startPrefixMapping()/endPrefixMapping() and use them.
4585 * Modifications becomes more complex then, however.
4587 * We cannot do this during the Qt 4 series because it would require too invasive changes, and
4588 * hence possibly behavioral changes.
4590 if (prefix.isEmpty()) {
4591 nsDecl = QLatin1String(" xmlns");
4593 qName = prefix + QLatin1Char(':') + name;
4594 nsDecl = QLatin1String(" xmlns:") + prefix;
4596 nsDecl += QLatin1String("=\"") + encodeText(namespaceURI, s) + QLatin1Char('\"');
4598 s << '<' << qName << nsDecl;
4600 QSet<QString> outputtedPrefixes;
4602 /* Write out attributes. */
4603 if (!m_attr->map.isEmpty()) {
4604 QHash<QString, QDomNodePrivate *>::const_iterator it = m_attr->map.constBegin();
4605 for (; it != m_attr->map.constEnd(); ++it) {
4607 if (it.value()->namespaceURI.isNull()) {
4608 s << it.value()->name << "=\"" << encodeText(it.value()->value, s, true, true) << '\"';
4610 s << it.value()->prefix << ':' << it.value()->name << "=\"" << encodeText(it.value()->value, s, true, true) << '\"';
4611 /* This is a fix for 138243, as good as it gets.
4613 * QDomElementPrivate::save() output a namespace declaration if
4614 * the element is in a namespace, no matter what. This function do as well, meaning
4615 * that we get two identical namespace declaration if we don't have the if-
4618 * This doesn't work when the parent element has the same prefix as us but
4619 * a different namespace. However, this can only occur by the user modifying the element,
4620 * and we don't do fixups by that anyway, and hence it's the user responsibility to not
4621 * arrive in those situations. */
4622 if((!it.value()->ownerNode ||
4623 it.value()->ownerNode->prefix != it.value()->prefix) &&
4624 !outputtedPrefixes.contains(it.value()->prefix)) {
4625 s << " xmlns:" << it.value()->prefix << "=\"" << encodeText(it.value()->namespaceURI, s, true, true) << '\"';
4626 outputtedPrefixes.insert(it.value()->prefix);
4634 if (first->isText())
4639 /* -1 disables new lines. */
4643 QDomNodePrivate::save(s, depth + 1, indent); if (!last->isText())
4644 s << QString(indent < 1 ? 0 : depth * indent, QLatin1Char(' '));
4646 s << "</" << qName << '>';
4650 if (!(next && next->isText())) {
4651 /* -1 disables new lines. */
4657 /**************************************************************
4661 **************************************************************/
4663 #define IMPL ((QDomElementPrivate*)impl)
4668 \brief The QDomElement class represents one element in the DOM tree.
4673 Elements have a tagName() and zero or more attributes associated
4674 with them. The tag name can be changed with setTagName().
4676 Element attributes are represented by QDomAttr objects that can
4677 be queried using the attribute() and attributeNode() functions.
4678 You can set attributes with the setAttribute() and
4679 setAttributeNode() functions. Attributes can be removed with
4680 removeAttribute(). There are namespace-aware equivalents to these
4681 functions, i.e. setAttributeNS(), setAttributeNodeNS() and
4682 removeAttributeNS().
4684 If you want to access the text of a node use text(), e.g.
4685 \snippet doc/src/snippets/code/src_xml_dom_qdom.cpp 9
4686 The text() function operates recursively to find the text (since
4687 not all elements contain text). If you want to find all the text
4688 in all of a node's children, iterate over the children looking for
4689 QDomText nodes, e.g.
4690 \snippet doc/src/snippets/code/src_xml_dom_qdom.cpp 10
4691 Note that we attempt to convert each node to a text node and use
4692 text() rather than using firstChild().toText().data() or
4693 n.toText().data() directly on the node, because the node may not
4696 You can get a list of all the decendents of an element which have
4697 a specified tag name with elementsByTagName() or
4698 elementsByTagNameNS().
4700 To browse the elements of a dom document use firstChildElement(), lastChildElement(),
4701 nextSiblingElement() and previousSiblingElement(). For example, to iterate over all
4702 child elements called "entry" in a root element called "database", you can use:
4703 \snippet doc/src/snippets/code/src_xml_dom_qdom.cpp 11
4705 For further information about the Document Object Model see
4706 \l{http://www.w3.org/TR/REC-DOM-Level-1/}{Level 1} and
4707 \l{http://www.w3.org/TR/DOM-Level-2-Core/}{Level 2 Core}.
4708 For a more general introduction of the DOM implementation see the
4709 QDomDocument documentation.
4713 Constructs an empty element. Use the QDomDocument::createElement()
4714 function to construct elements with content.
4716 QDomElement::QDomElement()
4722 Constructs a copy of \a x.
4724 The data of the copy is shared (shallow copy): modifying one node
4725 will also change the other. If you want to make a deep copy, use
4728 QDomElement::QDomElement(const QDomElement& x)
4733 QDomElement::QDomElement(QDomElementPrivate* n)
4739 Assigns \a x to this DOM element.
4741 The data of the copy is shared (shallow copy): modifying one node
4742 will also change the other. If you want to make a deep copy, use
4745 QDomElement& QDomElement::operator= (const QDomElement& x)
4747 return (QDomElement&) QDomNode::operator=(x);
4751 \fn QDomNode::NodeType QDomElement::nodeType() const
4753 Returns \c ElementNode.
4757 Sets this element's tag name to \a name.
4761 void QDomElement::setTagName(const QString& name)
4768 Returns the tag name of this element. For an XML element like this:
4770 \snippet doc/src/snippets/code/src_xml_dom_qdom.cpp 12
4772 the tagname would return "img".
4776 QString QDomElement::tagName() const
4780 return impl->nodeName();
4785 Returns a QDomNamedNodeMap containing all this element's attributes.
4787 \sa attribute() setAttribute() attributeNode() setAttributeNode()
4789 QDomNamedNodeMap QDomElement::attributes() const
4792 return QDomNamedNodeMap();
4793 return QDomNamedNodeMap(IMPL->attributes());
4797 Returns the attribute called \a name. If the attribute does not
4798 exist \a defValue is returned.
4800 \sa setAttribute() attributeNode() setAttributeNode() attributeNS()
4802 QString QDomElement::attribute(const QString& name, const QString& defValue) const
4806 return IMPL->attribute(name, defValue);
4810 Adds an attribute called \a name with value \a value. If an
4811 attribute with the same name exists, its value is replaced by \a
4814 \sa attribute() setAttributeNode() setAttributeNS()
4816 void QDomElement::setAttribute(const QString& name, const QString& value)
4820 IMPL->setAttribute(name, value);
4824 \fn void QDomElement::setAttribute(const QString& name, int value)
4827 The number is formatted according to the current locale.
4831 \fn void QDomElement::setAttribute(const QString& name, uint value)
4834 The number is formatted according to the current locale.
4840 The number is formatted according to the current locale.
4842 void QDomElement::setAttribute(const QString& name, qlonglong value)
4848 IMPL->setAttribute(name, x);
4854 The number is formatted according to the current locale.
4856 void QDomElement::setAttribute(const QString& name, qulonglong value)
4862 IMPL->setAttribute(name, x);
4868 The number is formatted according to the current locale.
4870 void QDomElement::setAttribute(const QString& name, float value)
4876 IMPL->setAttribute(name, x);
4882 The number is formatted according to the current locale.
4884 void QDomElement::setAttribute(const QString& name, double value)
4890 int count = qsnprintf(buf, sizeof(buf), "%.16g", value);
4892 x = QString::fromLatin1(buf, count);
4894 x.setNum(value); // Fallback
4895 IMPL->setAttribute(name, x);
4899 Removes the attribute called name \a name from this element.
4901 \sa setAttribute() attribute() removeAttributeNS()
4903 void QDomElement::removeAttribute(const QString& name)
4907 IMPL->removeAttribute(name);
4911 Returns the QDomAttr object that corresponds to the attribute
4912 called \a name. If no such attribute exists a \link
4913 QDomNode::isNull() null attribute\endlink is returned.
4915 \sa setAttributeNode() attribute() setAttribute() attributeNodeNS()
4917 QDomAttr QDomElement::attributeNode(const QString& name)
4921 return QDomAttr(IMPL->attributeNode(name));
4925 Adds the attribute \a newAttr to this element.
4927 If the element has another attribute that has the same name as \a
4928 newAttr, this function replaces that attribute and returns it;
4929 otherwise the function returns a \link QDomNode::isNull() null
4932 \sa attributeNode() setAttribute() setAttributeNodeNS()
4934 QDomAttr QDomElement::setAttributeNode(const QDomAttr& newAttr)
4938 return QDomAttr(IMPL->setAttributeNode(((QDomAttrPrivate*)newAttr.impl)));
4942 Removes the attribute \a oldAttr from the element and returns it.
4944 \sa attributeNode() setAttributeNode()
4946 QDomAttr QDomElement::removeAttributeNode(const QDomAttr& oldAttr)
4949 return QDomAttr(); // ### should this return oldAttr?
4950 return QDomAttr(IMPL->removeAttributeNode(((QDomAttrPrivate*)oldAttr.impl)));
4954 Returns a QDomNodeList containing all descendants of this element
4955 named \a tagname encountered during a preorder traversal of the
4956 element subtree with this element as its root. The order of the
4957 elements in the returned list is the order they are encountered
4958 during the preorder traversal.
4960 \sa elementsByTagNameNS() QDomDocument::elementsByTagName()
4962 QDomNodeList QDomElement::elementsByTagName(const QString& tagname) const
4964 return QDomNodeList(new QDomNodeListPrivate(impl, tagname));
4968 Returns true if this element has an attribute called \a name;
4969 otherwise returns false.
4971 \bold{Note:} This function does not take the presence of namespaces
4972 into account. As a result, the specified name will be tested
4973 against fully-qualified attribute names that include any namespace
4974 prefixes that may be present.
4976 Use hasAttributeNS() to explicitly test for attributes with specific
4977 namespaces and names.
4979 bool QDomElement::hasAttribute(const QString& name) const
4983 return IMPL->hasAttribute(name);
4987 Returns the attribute with the local name \a localName and the
4988 namespace URI \a nsURI. If the attribute does not exist \a
4989 defValue is returned.
4991 \sa setAttributeNS() attributeNodeNS() setAttributeNodeNS() attribute()
4993 QString QDomElement::attributeNS(const QString nsURI, const QString& localName, const QString& defValue) const
4997 return IMPL->attributeNS(nsURI, localName, defValue);
5001 Adds an attribute with the qualified name \a qName and the
5002 namespace URI \a nsURI with the value \a value. If an attribute
5003 with the same local name and namespace URI exists, its prefix is
5004 replaced by the prefix of \a qName and its value is repaced by \a
5007 Although \a qName is the qualified name, the local name is used to
5008 decide if an existing attribute's value should be replaced.
5010 \sa attributeNS() setAttributeNodeNS() setAttribute()
5012 void QDomElement::setAttributeNS(const QString nsURI, const QString& qName, const QString& value)
5016 IMPL->setAttributeNS(nsURI, qName, value);
5020 \fn void QDomElement::setAttributeNS(const QString nsURI, const QString& qName, int value)
5026 \fn void QDomElement::setAttributeNS(const QString nsURI, const QString& qName, uint value)
5034 void QDomElement::setAttributeNS(const QString nsURI, const QString& qName, qlonglong value)
5040 IMPL->setAttributeNS(nsURI, qName, x);
5046 void QDomElement::setAttributeNS(const QString nsURI, const QString& qName, qulonglong value)
5052 IMPL->setAttributeNS(nsURI, qName, x);
5058 void QDomElement::setAttributeNS(const QString nsURI, const QString& qName, double value)
5064 IMPL->setAttributeNS(nsURI, qName, x);
5068 Removes the attribute with the local name \a localName and the
5069 namespace URI \a nsURI from this element.
5071 \sa setAttributeNS() attributeNS() removeAttribute()
5073 void QDomElement::removeAttributeNS(const QString& nsURI, const QString& localName)
5077 QDomNodePrivate *n = IMPL->attributeNodeNS(nsURI, localName);
5080 IMPL->removeAttribute(n->nodeName());
5084 Returns the QDomAttr object that corresponds to the attribute
5085 with the local name \a localName and the namespace URI \a nsURI.
5086 If no such attribute exists a \l{QDomNode::isNull()}{null
5087 attribute} is returned.
5089 \sa setAttributeNode() attribute() setAttribute()
5091 QDomAttr QDomElement::attributeNodeNS(const QString& nsURI, const QString& localName)
5095 return QDomAttr(IMPL->attributeNodeNS(nsURI, localName));
5099 Adds the attribute \a newAttr to this element.
5101 If the element has another attribute that has the same local name
5102 and namespace URI as \a newAttr, this function replaces that
5103 attribute and returns it; otherwise the function returns a \link
5104 QDomNode::isNull() null attribute\endlink.
5106 \sa attributeNodeNS() setAttributeNS() setAttributeNode()
5108 QDomAttr QDomElement::setAttributeNodeNS(const QDomAttr& newAttr)
5112 return QDomAttr(IMPL->setAttributeNodeNS(((QDomAttrPrivate*)newAttr.impl)));
5116 Returns a QDomNodeList containing all descendants of this element
5117 with local name \a localName and namespace URI \a nsURI encountered
5118 during a preorder traversal of the element subtree with this element
5119 as its root. The order of the elements in the returned list is the
5120 order they are encountered during the preorder traversal.
5122 \sa elementsByTagName() QDomDocument::elementsByTagNameNS()
5124 QDomNodeList QDomElement::elementsByTagNameNS(const QString& nsURI, const QString& localName) const
5126 return QDomNodeList(new QDomNodeListPrivate(impl, nsURI, localName));
5130 Returns true if this element has an attribute with the local name
5131 \a localName and the namespace URI \a nsURI; otherwise returns
5134 bool QDomElement::hasAttributeNS(const QString& nsURI, const QString& localName) const
5138 return IMPL->hasAttributeNS(nsURI, localName);
5142 Returns the element's text or an empty string.
5145 \snippet doc/src/snippets/code/src_xml_dom_qdom.cpp 13
5147 The function text() of the QDomElement for the \c{<h1>} tag,
5148 will return the following text:
5150 \snippet doc/src/snippets/code/src_xml_dom_qdom.cpp 14
5152 Comments are ignored by this function. It only evaluates QDomText
5153 and QDomCDATASection objects.
5155 QString QDomElement::text() const
5159 return IMPL->text();
5164 /**************************************************************
5168 **************************************************************/
5170 QDomTextPrivate::QDomTextPrivate(QDomDocumentPrivate* d, QDomNodePrivate* parent, const QString& val)
5171 : QDomCharacterDataPrivate(d, parent, val)
5173 name = QLatin1String("#text");
5176 QDomTextPrivate::QDomTextPrivate(QDomTextPrivate* n, bool deep)
5177 : QDomCharacterDataPrivate(n, deep)
5181 QDomNodePrivate* QDomTextPrivate::cloneNode(bool deep)
5183 QDomNodePrivate* p = new QDomTextPrivate(this, deep);
5184 // We are not interested in this node
5189 QDomTextPrivate* QDomTextPrivate::splitText(int offset)
5192 qWarning("QDomText::splitText The node has no parent. So I can not split");
5196 QDomTextPrivate* t = new QDomTextPrivate(ownerDocument(), 0, value.mid(offset));
5197 value.truncate(offset);
5199 parent()->insertAfter(t, this);
5204 void QDomTextPrivate::save(QTextStream& s, int, int) const
5206 QDomTextPrivate *that = const_cast<QDomTextPrivate*>(this);
5207 s << encodeText(value, s, !(that->parent() && that->parent()->isElement()), false, true);
5210 /**************************************************************
5214 **************************************************************/
5216 #define IMPL ((QDomTextPrivate*)impl)
5221 \brief The QDomText class represents text data in the parsed XML document.
5226 You can split the text in a QDomText object over two QDomText
5227 objecs with splitText().
5229 For further information about the Document Object Model see
5230 \l{http://www.w3.org/TR/REC-DOM-Level-1/}{Level 1} and
5231 \l{http://www.w3.org/TR/DOM-Level-2-Core/}{Level 2 Core}.
5232 For a more general introduction of the DOM implementation see the
5233 QDomDocument documentation.
5237 Constructs an empty QDomText object.
5239 To construct a QDomText with content, use QDomDocument::createTextNode().
5241 QDomText::QDomText()
5242 : QDomCharacterData()
5247 Constructs a copy of \a x.
5249 The data of the copy is shared (shallow copy): modifying one node
5250 will also change the other. If you want to make a deep copy, use
5253 QDomText::QDomText(const QDomText& x)
5254 : QDomCharacterData(x)
5258 QDomText::QDomText(QDomTextPrivate* n)
5259 : QDomCharacterData(n)
5264 Assigns \a x to this DOM text.
5266 The data of the copy is shared (shallow copy): modifying one node
5267 will also change the other. If you want to make a deep copy, use
5270 QDomText& QDomText::operator= (const QDomText& x)
5272 return (QDomText&) QDomNode::operator=(x);
5276 \fn QDomNode::NodeType QDomText::nodeType() const
5278 Returns \c TextNode.
5282 Splits this DOM text object into two QDomText objects. This object
5283 keeps its first \a offset characters and the second (newly
5284 created) object is inserted into the document tree after this
5285 object with the remaining characters.
5287 The function returns the newly created object.
5289 \sa QDomNode::normalize()
5291 QDomText QDomText::splitText(int offset)
5295 return QDomText(IMPL->splitText(offset));
5300 /**************************************************************
5302 * QDomCommentPrivate
5304 **************************************************************/
5306 QDomCommentPrivate::QDomCommentPrivate(QDomDocumentPrivate* d, QDomNodePrivate* parent, const QString& val)
5307 : QDomCharacterDataPrivate(d, parent, val)
5309 name = QLatin1String("#comment");
5312 QDomCommentPrivate::QDomCommentPrivate(QDomCommentPrivate* n, bool deep)
5313 : QDomCharacterDataPrivate(n, deep)
5318 QDomNodePrivate* QDomCommentPrivate::cloneNode(bool deep)
5320 QDomNodePrivate* p = new QDomCommentPrivate(this, deep);
5321 // We are not interested in this node
5326 void QDomCommentPrivate::save(QTextStream& s, int depth, int indent) const
5328 /* We don't output whitespace if we would pollute a text node. */
5329 if (!(prev && prev->isText()))
5330 s << QString(indent < 1 ? 0 : depth * indent, QLatin1Char(' '));
5332 s << "<!--" << value;
5333 if (value.endsWith(QLatin1Char('-')))
5334 s << ' '; // Ensures that XML comment doesn't end with --->
5337 if (!(next && next->isText()))
5341 /**************************************************************
5345 **************************************************************/
5350 \brief The QDomComment class represents an XML comment.
5355 A comment in the parsed XML such as this:
5356 \snippet doc/src/snippets/code/src_xml_dom_qdom.cpp 15
5357 is represented by QDomComment objects in the parsed Dom tree.
5359 For further information about the Document Object Model see
5360 \l{http://www.w3.org/TR/REC-DOM-Level-1/} and
5361 \l{http://www.w3.org/TR/DOM-Level-2-Core/}.
5362 For a more general introduction of the DOM implementation see the
5363 QDomDocument documentation.
5367 Constructs an empty comment. To construct a comment with content,
5368 use the QDomDocument::createComment() function.
5370 QDomComment::QDomComment()
5371 : QDomCharacterData()
5376 Constructs a copy of \a x.
5378 The data of the copy is shared (shallow copy): modifying one node
5379 will also change the other. If you want to make a deep copy, use
5382 QDomComment::QDomComment(const QDomComment& x)
5383 : QDomCharacterData(x)
5387 QDomComment::QDomComment(QDomCommentPrivate* n)
5388 : QDomCharacterData(n)
5393 Assigns \a x to this DOM comment.
5395 The data of the copy is shared (shallow copy): modifying one node
5396 will also change the other. If you want to make a deep copy, use
5399 QDomComment& QDomComment::operator= (const QDomComment& x)
5401 return (QDomComment&) QDomNode::operator=(x);
5405 \fn QDomNode::NodeType QDomComment::nodeType() const
5407 Returns \c CommentNode.
5410 /**************************************************************
5412 * QDomCDATASectionPrivate
5414 **************************************************************/
5416 QDomCDATASectionPrivate::QDomCDATASectionPrivate(QDomDocumentPrivate* d, QDomNodePrivate* parent,
5418 : QDomTextPrivate(d, parent, val)
5420 name = QLatin1String("#cdata-section");
5423 QDomCDATASectionPrivate::QDomCDATASectionPrivate(QDomCDATASectionPrivate* n, bool deep)
5424 : QDomTextPrivate(n, deep)
5428 QDomNodePrivate* QDomCDATASectionPrivate::cloneNode(bool deep)
5430 QDomNodePrivate* p = new QDomCDATASectionPrivate(this, deep);
5431 // We are not interested in this node
5436 void QDomCDATASectionPrivate::save(QTextStream& s, int, int) const
5438 // ### How do we escape "]]>" ?
5439 // "]]>" is not allowed; so there should be none in value anyway
5440 s << "<![CDATA[" << value << "]]>";
5443 /**************************************************************
5447 **************************************************************/
5450 \class QDomCDATASection
5452 \brief The QDomCDATASection class represents an XML CDATA section.
5457 CDATA sections are used to escape blocks of text containing
5458 characters that would otherwise be regarded as markup. The only
5459 delimiter that is recognized in a CDATA section is the "]]>"
5460 string that terminates the CDATA section. CDATA sections cannot be
5461 nested. Their primary purpose is for including material such as
5462 XML fragments, without needing to escape all the delimiters.
5464 Adjacent QDomCDATASection nodes are not merged by the
5465 QDomNode::normalize() function.
5467 For further information about the Document Object Model see
5468 \l{http://www.w3.org/TR/REC-DOM-Level-1/} and
5469 \l{http://www.w3.org/TR/DOM-Level-2-Core/}.
5470 For a more general introduction of the DOM implementation see the
5471 QDomDocument documentation.
5475 Constructs an empty CDATA section. To create a CDATA section with
5476 content, use the QDomDocument::createCDATASection() function.
5478 QDomCDATASection::QDomCDATASection()
5484 Constructs a copy of \a x.
5486 The data of the copy is shared (shallow copy): modifying one node
5487 will also change the other. If you want to make a deep copy, use
5490 QDomCDATASection::QDomCDATASection(const QDomCDATASection& x)
5495 QDomCDATASection::QDomCDATASection(QDomCDATASectionPrivate* n)
5501 Assigns \a x to this CDATA section.
5503 The data of the copy is shared (shallow copy): modifying one node
5504 will also change the other. If you want to make a deep copy, use
5507 QDomCDATASection& QDomCDATASection::operator= (const QDomCDATASection& x)
5509 return (QDomCDATASection&) QDomNode::operator=(x);
5513 \fn QDomNode::NodeType QDomCDATASection::nodeType() const
5515 Returns \c CDATASection.
5518 /**************************************************************
5520 * QDomNotationPrivate
5522 **************************************************************/
5524 QDomNotationPrivate::QDomNotationPrivate(QDomDocumentPrivate* d, QDomNodePrivate* parent,
5525 const QString& aname,
5526 const QString& pub, const QString& sys)
5527 : QDomNodePrivate(d, parent)
5534 QDomNotationPrivate::QDomNotationPrivate(QDomNotationPrivate* n, bool deep)
5535 : QDomNodePrivate(n, deep)
5541 QDomNodePrivate* QDomNotationPrivate::cloneNode(bool deep)
5543 QDomNodePrivate* p = new QDomNotationPrivate(this, deep);
5544 // We are not interested in this node
5549 void QDomNotationPrivate::save(QTextStream& s, int, int) const
5551 s << "<!NOTATION " << name << ' ';
5552 if (!m_pub.isNull()) {
5553 s << "PUBLIC " << quotedValue(m_pub);
5554 if (!m_sys.isNull())
5555 s << ' ' << quotedValue(m_sys);
5557 s << "SYSTEM " << quotedValue(m_sys);
5562 /**************************************************************
5566 **************************************************************/
5568 #define IMPL ((QDomNotationPrivate*)impl)
5573 \brief The QDomNotation class represents an XML notation.
5578 A notation either declares, by name, the format of an unparsed
5579 entity (see section 4.7 of the XML 1.0 specification), or is used
5580 for formal declaration of processing instruction targets (see
5581 section 2.6 of the XML 1.0 specification).
5583 DOM does not support editing notation nodes; they are therefore
5586 A notation node does not have any parent.
5588 You can retrieve the publicId() and systemId() from a notation
5591 For further information about the Document Object Model see
5592 \l{http://www.w3.org/TR/REC-DOM-Level-1/}{Level 1} and
5593 \l{http://www.w3.org/TR/DOM-Level-2-Core/}{Level 2 Core}.
5594 For a more general introduction of the DOM implementation see the
5595 QDomDocument documentation.
5602 QDomNotation::QDomNotation()
5608 Constructs a copy of \a x.
5610 The data of the copy is shared (shallow copy): modifying one node
5611 will also change the other. If you want to make a deep copy, use
5614 QDomNotation::QDomNotation(const QDomNotation& x)
5619 QDomNotation::QDomNotation(QDomNotationPrivate* n)
5625 Assigns \a x to this DOM notation.
5627 The data of the copy is shared (shallow copy): modifying one node
5628 will also change the other. If you want to make a deep copy, use
5631 QDomNotation& QDomNotation::operator= (const QDomNotation& x)
5633 return (QDomNotation&) QDomNode::operator=(x);
5637 \fn QDomNode::NodeType QDomNotation::nodeType() const
5639 Returns \c NotationNode.
5643 Returns the public identifier of this notation.
5645 QString QDomNotation::publicId() const
5653 Returns the system identifier of this notation.
5655 QString QDomNotation::systemId() const
5664 /**************************************************************
5668 **************************************************************/
5670 QDomEntityPrivate::QDomEntityPrivate(QDomDocumentPrivate* d, QDomNodePrivate* parent,
5671 const QString& aname,
5672 const QString& pub, const QString& sys, const QString& notation)
5673 : QDomNodePrivate(d, parent)
5678 m_notationName = notation;
5681 QDomEntityPrivate::QDomEntityPrivate(QDomEntityPrivate* n, bool deep)
5682 : QDomNodePrivate(n, deep)
5686 m_notationName = n->m_notationName;
5689 QDomNodePrivate* QDomEntityPrivate::cloneNode(bool deep)
5691 QDomNodePrivate* p = new QDomEntityPrivate(this, deep);
5692 // We are not interested in this node
5698 Encode an entity value upon saving.
5700 static QByteArray encodeEntity(const QByteArray& str)
5702 QByteArray tmp(str);
5703 uint len = tmp.size();
5705 const char* d = tmp.data();
5708 tmp.replace(i, 1, "<");
5713 else if (d[i] == '"') {
5714 tmp.replace(i, 1, """);
5718 } else if (d[i] == '&' && i + 1 < len && d[i+1] == '#') {
5719 // Dont encode < or " or &custom;.
5720 // Only encode character references
5721 tmp.replace(i, 1, "&");
5733 void QDomEntityPrivate::save(QTextStream& s, int, int) const
5735 QString _name = name;
5736 if (_name.startsWith(QLatin1Char('%')))
5737 _name = QLatin1String("% ") + _name.mid(1);
5739 if (m_sys.isNull() && m_pub.isNull()) {
5740 s << "<!ENTITY " << _name << " \"" << encodeEntity(value.toUtf8()) << "\">" << endl;
5742 s << "<!ENTITY " << _name << ' ';
5743 if (m_pub.isNull()) {
5744 s << "SYSTEM " << quotedValue(m_sys);
5746 s << "PUBLIC " << quotedValue(m_pub) << ' ' << quotedValue(m_sys);
5748 if (! m_notationName.isNull()) {
5749 s << " NDATA " << m_notationName;
5755 /**************************************************************
5759 **************************************************************/
5761 #define IMPL ((QDomEntityPrivate*)impl)
5766 \brief The QDomEntity class represents an XML entity.
5771 This class represents an entity in an XML document, either parsed
5772 or unparsed. Note that this models the entity itself not the
5775 DOM does not support editing entity nodes; if a user wants to make
5776 changes to the contents of an entity, every related
5777 QDomEntityReference node must be replaced in the DOM tree by a
5778 clone of the entity's contents, and then the desired changes must
5779 be made to each of the clones instead. All the descendants of an
5780 entity node are read-only.
5782 An entity node does not have any parent.
5784 You can access the entity's publicId(), systemId() and
5785 notationName() when available.
5787 For further information about the Document Object Model see
5788 \l{http://www.w3.org/TR/REC-DOM-Level-1/}{Level 1} and
5789 \l{http://www.w3.org/TR/DOM-Level-2-Core/}{Level 2 Core}.
5790 For a more general introduction of the DOM implementation see the
5791 QDomDocument documentation.
5796 Constructs an empty entity.
5798 QDomEntity::QDomEntity()
5805 Constructs a copy of \a x.
5807 The data of the copy is shared (shallow copy): modifying one node
5808 will also change the other. If you want to make a deep copy, use
5811 QDomEntity::QDomEntity(const QDomEntity& x)
5816 QDomEntity::QDomEntity(QDomEntityPrivate* n)
5822 Assigns \a x to this DOM entity.
5824 The data of the copy is shared (shallow copy): modifying one node
5825 will also change the other. If you want to make a deep copy, use
5828 QDomEntity& QDomEntity::operator= (const QDomEntity& x)
5830 return (QDomEntity&) QDomNode::operator=(x);
5834 \fn QDomNode::NodeType QDomEntity::nodeType() const
5836 Returns \c EntityNode.
5840 Returns the public identifier associated with this entity. If the
5841 public identifier was not specified an empty string is returned.
5843 QString QDomEntity::publicId() const
5851 Returns the system identifier associated with this entity. If the
5852 system identifier was not specified an empty string is returned.
5854 QString QDomEntity::systemId() const
5862 For unparsed entities this function returns the name of the
5863 notation for the entity. For parsed entities this function returns
5866 QString QDomEntity::notationName() const
5870 return IMPL->m_notationName;
5875 /**************************************************************
5877 * QDomEntityReferencePrivate
5879 **************************************************************/
5881 QDomEntityReferencePrivate::QDomEntityReferencePrivate(QDomDocumentPrivate* d, QDomNodePrivate* parent, const QString& aname)
5882 : QDomNodePrivate(d, parent)
5887 QDomEntityReferencePrivate::QDomEntityReferencePrivate(QDomNodePrivate* n, bool deep)
5888 : QDomNodePrivate(n, deep)
5892 QDomNodePrivate* QDomEntityReferencePrivate::cloneNode(bool deep)
5894 QDomNodePrivate* p = new QDomEntityReferencePrivate(this, deep);
5895 // We are not interested in this node
5900 void QDomEntityReferencePrivate::save(QTextStream& s, int, int) const
5902 s << '&' << name << ';';
5905 /**************************************************************
5907 * QDomEntityReference
5909 **************************************************************/
5912 \class QDomEntityReference
5914 \brief The QDomEntityReference class represents an XML entity reference.
5919 A QDomEntityReference object may be inserted into the DOM tree
5920 when an entity reference is in the source document, or when the
5921 user wishes to insert an entity reference.
5923 Note that character references and references to predefined
5924 entities are expanded by the XML processor so that characters are
5925 represented by their Unicode equivalent rather than by an entity
5928 Moreover, the XML processor may completely expand references to
5929 entities while building the DOM tree, instead of providing
5930 QDomEntityReference objects.
5932 If it does provide such objects, then for a given entity reference
5933 node, it may be that there is no entity node representing the
5934 referenced entity; but if such an entity exists, then the child
5935 list of the entity reference node is the same as that of the
5936 entity node. As with the entity node, all descendants of the
5937 entity reference are read-only.
5939 For further information about the Document Object Model see
5940 \l{http://www.w3.org/TR/REC-DOM-Level-1/}{Level 1} and
5941 \l{http://www.w3.org/TR/DOM-Level-2-Core/}{Level 2 Core}.
5942 For a more general introduction of the DOM implementation see the
5943 QDomDocument documentation.
5947 Constructs an empty entity reference. Use
5948 QDomDocument::createEntityReference() to create a entity reference
5951 QDomEntityReference::QDomEntityReference()
5957 Constructs a copy of \a x.
5959 The data of the copy is shared (shallow copy): modifying one node
5960 will also change the other. If you want to make a deep copy, use
5963 QDomEntityReference::QDomEntityReference(const QDomEntityReference& x)
5968 QDomEntityReference::QDomEntityReference(QDomEntityReferencePrivate* n)
5974 Assigns \a x to this entity reference.
5976 The data of the copy is shared (shallow copy): modifying one node
5977 will also change the other. If you want to make a deep copy, use
5980 QDomEntityReference& QDomEntityReference::operator= (const QDomEntityReference& x)
5982 return (QDomEntityReference&) QDomNode::operator=(x);
5986 \fn QDomNode::NodeType QDomEntityReference::nodeType() const
5988 Returns \c EntityReference.
5991 /**************************************************************
5993 * QDomProcessingInstructionPrivate
5995 **************************************************************/
5997 QDomProcessingInstructionPrivate::QDomProcessingInstructionPrivate(QDomDocumentPrivate* d,
5998 QDomNodePrivate* parent, const QString& target, const QString& data)
5999 : QDomNodePrivate(d, parent)
6005 QDomProcessingInstructionPrivate::QDomProcessingInstructionPrivate(QDomProcessingInstructionPrivate* n, bool deep)
6006 : QDomNodePrivate(n, deep)
6011 QDomNodePrivate* QDomProcessingInstructionPrivate::cloneNode(bool deep)
6013 QDomNodePrivate* p = new QDomProcessingInstructionPrivate(this, deep);
6014 // We are not interested in this node
6019 void QDomProcessingInstructionPrivate::save(QTextStream& s, int, int) const
6021 s << "<?" << name << ' ' << value << "?>" << endl;
6024 /**************************************************************
6026 * QDomProcessingInstruction
6028 **************************************************************/
6031 \class QDomProcessingInstruction
6033 \brief The QDomProcessingInstruction class represents an XML processing
6039 Processing instructions are used in XML to keep processor-specific
6040 information in the text of the document.
6042 The XML declaration that appears at the top of an XML document,
6043 typically \tt{<?xml version='1.0' encoding='UTF-8'?>}, is treated by QDom as a
6044 processing instruction. This is unfortunate, since the XML declaration is
6045 not a processing instruction; among other differences, it cannot be
6046 inserted into a document anywhere but on the first line.
6048 Do not use this function to create an xml declaration, since although it
6049 has the same syntax as a processing instruction, it isn't, and might not
6050 be treated by QDom as such.
6052 The content of the processing instruction is retrieved with data()
6053 and set with setData(). The processing instruction's target is
6054 retrieved with target().
6056 For further information about the Document Object Model see
6057 \l{http://www.w3.org/TR/REC-DOM-Level-1/}{Level 1} and
6058 \l{http://www.w3.org/TR/DOM-Level-2-Core/}{Level 2 Core}.
6059 For a more general introduction of the DOM implementation see the
6060 QDomDocument documentation.
6064 Constructs an empty processing instruction. Use
6065 QDomDocument::createProcessingInstruction() to create a processing
6066 instruction with content.
6068 QDomProcessingInstruction::QDomProcessingInstruction()
6074 Constructs a copy of \a x.
6076 The data of the copy is shared (shallow copy): modifying one node
6077 will also change the other. If you want to make a deep copy, use
6080 QDomProcessingInstruction::QDomProcessingInstruction(const QDomProcessingInstruction& x)
6085 QDomProcessingInstruction::QDomProcessingInstruction(QDomProcessingInstructionPrivate* n)
6091 Assigns \a x to this processing instruction.
6093 The data of the copy is shared (shallow copy): modifying one node
6094 will also change the other. If you want to make a deep copy, use
6097 QDomProcessingInstruction& QDomProcessingInstruction::operator= (const QDomProcessingInstruction& x)
6099 return (QDomProcessingInstruction&) QDomNode::operator=(x);
6103 \fn QDomNode::NodeType QDomProcessingInstruction::nodeType() const
6105 Returns \c ProcessingInstructionNode.
6109 Returns the target of this processing instruction.
6113 QString QDomProcessingInstruction::target() const
6117 return impl->nodeName();
6121 Returns the content of this processing instruction.
6123 \sa setData() target()
6125 QString QDomProcessingInstruction::data() const
6129 return impl->nodeValue();
6133 Sets the data contained in the processing instruction to \a d.
6137 void QDomProcessingInstruction::setData(const QString& d)
6141 impl->setNodeValue(d);
6144 /**************************************************************
6146 * QDomDocumentPrivate
6148 **************************************************************/
6150 QDomDocumentPrivate::QDomDocumentPrivate()
6151 : QDomNodePrivate(0),
6152 impl(new QDomImplementationPrivate),
6155 type = new QDomDocumentTypePrivate(this, this);
6158 name = QLatin1String("#document");
6161 QDomDocumentPrivate::QDomDocumentPrivate(const QString& aname)
6162 : QDomNodePrivate(0),
6163 impl(new QDomImplementationPrivate),
6166 type = new QDomDocumentTypePrivate(this, this);
6170 name = QLatin1String("#document");
6173 QDomDocumentPrivate::QDomDocumentPrivate(QDomDocumentTypePrivate* dt)
6174 : QDomNodePrivate(0),
6175 impl(new QDomImplementationPrivate),
6181 type = new QDomDocumentTypePrivate(this, this);
6185 name = QLatin1String("#document");
6188 QDomDocumentPrivate::QDomDocumentPrivate(QDomDocumentPrivate* n, bool deep)
6189 : QDomNodePrivate(n, deep),
6190 impl(n->impl->clone()),
6193 type = static_cast<QDomDocumentTypePrivate*>(n->type->cloneNode());
6194 type->setParent(this);
6197 QDomDocumentPrivate::~QDomDocumentPrivate()
6201 void QDomDocumentPrivate::clear()
6205 QDomNodePrivate::clear();
6208 static void initializeReader(QXmlSimpleReader &reader, bool namespaceProcessing)
6210 reader.setFeature(QLatin1String("http://xml.org/sax/features/namespaces"), namespaceProcessing);
6211 reader.setFeature(QLatin1String("http://xml.org/sax/features/namespace-prefixes"), !namespaceProcessing);
6212 reader.setFeature(QLatin1String("http://trolltech.com/xml/features/report-whitespace-only-CharData"), false); // Shouldn't change in Qt 4
6215 bool QDomDocumentPrivate::setContent(QXmlInputSource *source, bool namespaceProcessing, QString *errorMsg, int *errorLine, int *errorColumn)
6217 QXmlSimpleReader reader;
6218 initializeReader(reader, namespaceProcessing);
6219 return setContent(source, &reader, errorMsg, errorLine, errorColumn);
6222 bool QDomDocumentPrivate::setContent(QXmlInputSource *source, QXmlReader *reader, QString *errorMsg, int *errorLine, int *errorColumn)
6225 impl = new QDomImplementationPrivate;
6226 type = new QDomDocumentTypePrivate(this, this);
6229 bool namespaceProcessing = reader->feature(QLatin1String("http://xml.org/sax/features/namespaces"))
6230 && !reader->feature(QLatin1String("http://xml.org/sax/features/namespace-prefixes"));
6232 QDomHandler hnd(this, namespaceProcessing);
6233 reader->setContentHandler(&hnd);
6234 reader->setErrorHandler(&hnd);
6235 reader->setLexicalHandler(&hnd);
6236 reader->setDeclHandler(&hnd);
6237 reader->setDTDHandler(&hnd);
6239 if (!reader->parse(source)) {
6241 *errorMsg = hnd.errorMsg;
6243 *errorLine = hnd.errorLine;
6245 *errorColumn = hnd.errorColumn;
6252 QDomNodePrivate* QDomDocumentPrivate::cloneNode(bool deep)
6254 QDomNodePrivate *p = new QDomDocumentPrivate(this, deep);
6255 // We are not interested in this node
6260 QDomElementPrivate* QDomDocumentPrivate::documentElement()
6262 QDomNodePrivate *p = first;
6263 while (p && !p->isElement())
6266 return static_cast<QDomElementPrivate *>(p);
6269 QDomElementPrivate* QDomDocumentPrivate::createElement(const QString &tagName)
6272 QString fixedName = fixedXmlName(tagName, &ok);
6276 QDomElementPrivate *e = new QDomElementPrivate(this, 0, fixedName);
6281 QDomElementPrivate* QDomDocumentPrivate::createElementNS(const QString &nsURI, const QString &qName)
6284 QString fixedName = fixedXmlName(qName, &ok, true);
6288 QDomElementPrivate *e = new QDomElementPrivate(this, 0, nsURI, fixedName);
6293 QDomDocumentFragmentPrivate* QDomDocumentPrivate::createDocumentFragment()
6295 QDomDocumentFragmentPrivate *f = new QDomDocumentFragmentPrivate(this, (QDomNodePrivate*)0);
6300 QDomTextPrivate* QDomDocumentPrivate::createTextNode(const QString &data)
6303 QString fixedData = fixedCharData(data, &ok);
6307 QDomTextPrivate *t = new QDomTextPrivate(this, 0, fixedData);
6312 QDomCommentPrivate* QDomDocumentPrivate::createComment(const QString &data)
6315 QString fixedData = fixedComment(data, &ok);
6319 QDomCommentPrivate *c = new QDomCommentPrivate(this, 0, fixedData);
6324 QDomCDATASectionPrivate* QDomDocumentPrivate::createCDATASection(const QString &data)
6327 QString fixedData = fixedCDataSection(data, &ok);
6331 QDomCDATASectionPrivate *c = new QDomCDATASectionPrivate(this, 0, fixedData);
6336 QDomProcessingInstructionPrivate* QDomDocumentPrivate::createProcessingInstruction(const QString &target,
6337 const QString &data)
6340 QString fixedData = fixedPIData(data, &ok);
6343 // [17] PITarget ::= Name - (('X' | 'x') ('M' | 'm') ('L' | 'l'))
6344 QString fixedTarget = fixedXmlName(target, &ok);
6348 QDomProcessingInstructionPrivate *p = new QDomProcessingInstructionPrivate(this, 0, fixedTarget, fixedData);
6352 QDomAttrPrivate* QDomDocumentPrivate::createAttribute(const QString &aname)
6355 QString fixedName = fixedXmlName(aname, &ok);
6359 QDomAttrPrivate *a = new QDomAttrPrivate(this, 0, fixedName);
6364 QDomAttrPrivate* QDomDocumentPrivate::createAttributeNS(const QString &nsURI, const QString &qName)
6367 QString fixedName = fixedXmlName(qName, &ok, true);
6371 QDomAttrPrivate *a = new QDomAttrPrivate(this, 0, nsURI, fixedName);
6376 QDomEntityReferencePrivate* QDomDocumentPrivate::createEntityReference(const QString &aname)
6379 QString fixedName = fixedXmlName(aname, &ok);
6383 QDomEntityReferencePrivate *e = new QDomEntityReferencePrivate(this, 0, fixedName);
6388 QDomNodePrivate* QDomDocumentPrivate::importNode(const QDomNodePrivate *importedNode, bool deep)
6390 QDomNodePrivate *node = 0;
6391 switch (importedNode->nodeType()) {
6392 case QDomNode::AttributeNode:
6393 node = new QDomAttrPrivate((QDomAttrPrivate*)importedNode, true);
6395 case QDomNode::DocumentFragmentNode:
6396 node = new QDomDocumentFragmentPrivate((QDomDocumentFragmentPrivate*)importedNode, deep);
6398 case QDomNode::ElementNode:
6399 node = new QDomElementPrivate((QDomElementPrivate*)importedNode, deep);
6401 case QDomNode::EntityNode:
6402 node = new QDomEntityPrivate((QDomEntityPrivate*)importedNode, deep);
6404 case QDomNode::EntityReferenceNode:
6405 node = new QDomEntityReferencePrivate((QDomEntityReferencePrivate*)importedNode, false);
6407 case QDomNode::NotationNode:
6408 node = new QDomNotationPrivate((QDomNotationPrivate*)importedNode, deep);
6410 case QDomNode::ProcessingInstructionNode:
6411 node = new QDomProcessingInstructionPrivate((QDomProcessingInstructionPrivate*)importedNode, deep);
6413 case QDomNode::TextNode:
6414 node = new QDomTextPrivate((QDomTextPrivate*)importedNode, deep);
6416 case QDomNode::CDATASectionNode:
6417 node = new QDomCDATASectionPrivate((QDomCDATASectionPrivate*)importedNode, deep);
6419 case QDomNode::CommentNode:
6420 node = new QDomCommentPrivate((QDomCommentPrivate*)importedNode, deep);
6426 node->setOwnerDocument(this);
6427 // The QDomNode constructor increases the refcount, so deref first to
6428 // keep refcount balanced.
6434 void QDomDocumentPrivate::saveDocument(QTextStream& s, const int indent, QDomNode::EncodingPolicy encUsed) const
6436 const QDomNodePrivate* n = first;
6438 if(encUsed == QDomNode::EncodingFromDocument) {
6439 #ifndef QT_NO_TEXTCODEC
6440 const QDomNodePrivate* n = first;
6442 QTextCodec *codec = 0;
6444 if (n && n->isProcessingInstruction() && n->nodeName() == QLatin1String("xml")) {
6445 // we have an XML declaration
6446 QString data = n->nodeValue();
6447 QRegExp encoding(QString::fromLatin1("encoding\\s*=\\s*((\"([^\"]*)\")|('([^']*)'))"));
6448 encoding.indexIn(data);
6449 QString enc = encoding.cap(3);
6451 enc = encoding.cap(5);
6453 codec = QTextCodec::codecForName(enc.toLatin1().data());
6456 codec = QTextCodec::codecForName("UTF-8");
6463 if (!doc && !(n->isProcessingInstruction() && n->nodeName() == QLatin1String("xml"))) {
6464 // save doctype after XML declaration
6465 type->save(s, 0, indent);
6468 n->save(s, 0, indent);
6474 // Write out the XML declaration.
6475 #ifdef QT_NO_TEXTCODEC
6476 const QLatin1String codecName("iso-8859-1");
6478 const QTextCodec *const codec = s.codec();
6479 Q_ASSERT_X(codec, "QDomNode::save()", "A codec must be specified in the text stream.");
6480 const QByteArray codecName = codec->name();
6483 s << "<?xml version=\"1.0\" encoding=\""
6487 // Skip the first processing instruction by name "xml", if any such exists.
6488 const QDomNodePrivate* startNode = n;
6490 // First, we try to find the PI and sets the startNode to the one appearing after it.
6492 if(n->isProcessingInstruction() && n->nodeName() == QLatin1String("xml")) {
6493 startNode = n->next;
6500 // Now we serialize all the nodes after the faked XML declaration(the PI).
6502 startNode->save(s, 0, indent);
6503 startNode = startNode->next;
6508 /**************************************************************
6512 **************************************************************/
6514 #define IMPL ((QDomDocumentPrivate*)impl)
6519 \brief The QDomDocument class represents an XML document.
6525 The QDomDocument class represents the entire XML document.
6526 Conceptually, it is the root of the document tree, and provides
6527 the primary access to the document's data.
6529 Since elements, text nodes, comments, processing instructions,
6530 etc., cannot exist outside the context of a document, the document
6531 class also contains the factory functions needed to create these
6532 objects. The node objects created have an ownerDocument() function
6533 which associates them with the document within whose context they
6534 were created. The DOM classes that will be used most often are
6535 QDomNode, QDomDocument, QDomElement and QDomText.
6537 The parsed XML is represented internally by a tree of objects that
6538 can be accessed using the various QDom classes. All QDom classes
6539 only \e reference objects in the internal tree. The internal
6540 objects in the DOM tree will get deleted once the last QDom
6541 object referencing them and the QDomDocument itself are deleted.
6543 Creation of elements, text nodes, etc. is done using the various
6544 factory functions provided in this class. Using the default
6545 constructors of the QDom classes will only result in empty
6546 objects that cannot be manipulated or inserted into the Document.
6548 The QDomDocument class has several functions for creating document
6549 data, for example, createElement(), createTextNode(),
6550 createComment(), createCDATASection(),
6551 createProcessingInstruction(), createAttribute() and
6552 createEntityReference(). Some of these functions have versions
6553 that support namespaces, i.e. createElementNS() and
6554 createAttributeNS(). The createDocumentFragment() function is used
6555 to hold parts of the document; this is useful for manipulating for
6558 The entire content of the document is set with setContent(). This
6559 function parses the string it is passed as an XML document and
6560 creates the DOM tree that represents the document. The root
6561 element is available using documentElement(). The textual
6562 representation of the document can be obtained using toString().
6564 \note The DOM tree might end up reserving a lot of memory if the XML
6565 document is big. For such documents, the QXmlStreamReader or the
6566 QXmlQuery classes might be better solutions.
6568 It is possible to insert a node from another document into the
6569 document using importNode().
6571 You can obtain a list of all the elements that have a particular
6572 tag with elementsByTagName() or with elementsByTagNameNS().
6574 The QDom classes are typically used as follows:
6575 \snippet doc/src/snippets/code/src_xml_dom_qdom.cpp 16
6577 Once \c doc and \c elem go out of scope, the whole internal tree
6578 representing the XML document is deleted.
6580 To create a document using DOM use code like this:
6581 \snippet doc/src/snippets/code/src_xml_dom_qdom.cpp 17
6583 For further information about the Document Object Model see
6584 the Document Object Model (DOM)
6585 \l{http://www.w3.org/TR/REC-DOM-Level-1/}{Level 1} and
6586 \l{http://www.w3.org/TR/DOM-Level-2-Core/}{Level 2 Core}
6589 \sa {DOM Bookmarks Example}, {Simple DOM Model Example}
6594 Constructs an empty document.
6596 QDomDocument::QDomDocument()
6602 Creates a document and sets the name of the document type to \a
6605 QDomDocument::QDomDocument(const QString& name)
6607 // We take over ownership
6608 impl = new QDomDocumentPrivate(name);
6612 Creates a document with the document type \a doctype.
6614 \sa QDomImplementation::createDocumentType()
6616 QDomDocument::QDomDocument(const QDomDocumentType& doctype)
6618 impl = new QDomDocumentPrivate((QDomDocumentTypePrivate*)(doctype.impl));
6622 Constructs a copy of \a x.
6624 The data of the copy is shared (shallow copy): modifying one node
6625 will also change the other. If you want to make a deep copy, use
6628 QDomDocument::QDomDocument(const QDomDocument& x)
6633 QDomDocument::QDomDocument(QDomDocumentPrivate* x)
6639 Assigns \a x to this DOM document.
6641 The data of the copy is shared (shallow copy): modifying one node
6642 will also change the other. If you want to make a deep copy, use
6645 QDomDocument& QDomDocument::operator= (const QDomDocument& x)
6647 return (QDomDocument&) QDomNode::operator=(x);
6651 Destroys the object and frees its resources.
6653 QDomDocument::~QDomDocument()
6660 This function reads the XML document from the string \a text, returning
6661 true if the content was successfully parsed; otherwise returns false.
6662 Since \a text is already a Unicode string, no encoding detection
6665 bool QDomDocument::setContent(const QString& text, bool namespaceProcessing, QString *errorMsg, int *errorLine, int *errorColumn)
6668 impl = new QDomDocumentPrivate();
6669 QXmlInputSource source;
6670 source.setData(text);
6671 return IMPL->setContent(&source, namespaceProcessing, errorMsg, errorLine, errorColumn);
6677 This function parses the XML document from the byte array \a
6678 data and sets it as the content of the document. It tries to
6679 detect the encoding of the document as required by the XML
6682 If \a namespaceProcessing is true, the parser recognizes
6683 namespaces in the XML file and sets the prefix name, local name
6684 and namespace URI to appropriate values. If \a namespaceProcessing
6685 is false, the parser does no namespace processing when it reads
6688 If a parse error occurs, this function returns false and the error
6689 message is placed in \c{*}\a{errorMsg}, the line number in
6690 \c{*}\a{errorLine} and the column number in \c{*}\a{errorColumn}
6691 (unless the associated pointer is set to 0); otherwise this
6692 function returns true. The various error messages are described in
6693 the QXmlParseException class documentation. Note that, if you
6694 want to display these error messages to your application's users,
6695 they will be displayed in English unless they are explicitly
6698 If \a namespaceProcessing is true, the function QDomNode::prefix()
6699 returns a string for all elements and attributes. It returns an
6700 empty string if the element or attribute has no prefix.
6702 Text nodes consisting only of whitespace are stripped and won't
6703 appear in the QDomDocument. If this behavior is not desired,
6704 one can use the setContent() overload that allows a QXmlReader to be
6707 If \a namespaceProcessing is false, the functions
6708 QDomNode::prefix(), QDomNode::localName() and
6709 QDomNode::namespaceURI() return an empty string.
6711 Entity references are handled as follows:
6713 \o References to internal general entities and character entities occurring in the
6714 content are included. The result is a QDomText node with the references replaced
6715 by their corresponding entity values.
6716 \o References to parameter entities occurring in the internal subset are included.
6717 The result is a QDomDocumentType node which contains entity and notation declarations
6718 with the references replaced by their corresponding entity values.
6719 \o Any general parsed entity reference which is not defined in the internal subset and
6720 which occurs in the content is represented as a QDomEntityReference node.
6721 \o Any parsed entity reference which is not defined in the internal subset and which
6722 occurs outside of the content is replaced with an empty string.
6723 \o Any unparsed entity reference is replaced with an empty string.
6726 \sa QDomNode::namespaceURI() QDomNode::localName()
6727 QDomNode::prefix() QString::isNull() QString::isEmpty()
6729 bool QDomDocument::setContent(const QByteArray &data, bool namespaceProcessing, QString *errorMsg, int *errorLine, int *errorColumn)
6732 impl = new QDomDocumentPrivate();
6735 QXmlInputSource source(&buf);
6736 return IMPL->setContent(&source, namespaceProcessing, errorMsg, errorLine, errorColumn);
6742 This function reads the XML document from the IO device \a dev, returning
6743 true if the content was successfully parsed; otherwise returns false.
6745 bool QDomDocument::setContent(QIODevice* dev, bool namespaceProcessing, QString *errorMsg, int *errorLine, int *errorColumn)
6748 impl = new QDomDocumentPrivate();
6749 QXmlInputSource source(dev);
6750 return IMPL->setContent(&source, namespaceProcessing, errorMsg, errorLine, errorColumn);
6757 This function reads the XML document from the QXmlInputSource \a source,
6758 returning true if the content was successfully parsed; otherwise returns false.
6761 bool QDomDocument::setContent(QXmlInputSource *source, bool namespaceProcessing, QString *errorMsg, int *errorLine, int *errorColumn )
6764 impl = new QDomDocumentPrivate();
6765 QXmlSimpleReader reader;
6766 initializeReader(reader, namespaceProcessing);
6767 return IMPL->setContent(source, &reader, errorMsg, errorLine, errorColumn);
6773 This function reads the XML document from the string \a text, returning
6774 true if the content was successfully parsed; otherwise returns false.
6775 Since \a text is already a Unicode string, no encoding detection
6778 No namespace processing is performed either.
6780 bool QDomDocument::setContent(const QString& text, QString *errorMsg, int *errorLine, int *errorColumn)
6782 return setContent(text, false, errorMsg, errorLine, errorColumn);
6788 This function reads the XML document from the byte array \a buffer,
6789 returning true if the content was successfully parsed; otherwise returns
6792 No namespace processing is performed.
6794 bool QDomDocument::setContent(const QByteArray& buffer, QString *errorMsg, int *errorLine, int *errorColumn )
6796 return setContent(buffer, false, errorMsg, errorLine, errorColumn);
6802 This function reads the XML document from the IO device \a dev, returning
6803 true if the content was successfully parsed; otherwise returns false.
6805 No namespace processing is performed.
6807 bool QDomDocument::setContent(QIODevice* dev, QString *errorMsg, int *errorLine, int *errorColumn )
6809 return setContent(dev, false, errorMsg, errorLine, errorColumn);
6815 This function reads the XML document from the QXmlInputSource \a source and
6816 parses it with the QXmlReader \a reader, returning true if the content was
6817 successfully parsed; otherwise returns false.
6819 This function doesn't change the features of the \a reader. If you want to
6820 use certain features for parsing you can use this function to set up the
6821 reader appropriately.
6823 \sa QXmlSimpleReader
6825 bool QDomDocument::setContent(QXmlInputSource *source, QXmlReader *reader, QString *errorMsg, int *errorLine, int *errorColumn )
6828 impl = new QDomDocumentPrivate();
6829 return IMPL->setContent(source, reader, errorMsg, errorLine, errorColumn);
6833 Converts the parsed document back to its textual representation.
6835 This function uses \a indent as the amount of space to indent
6838 If \a indent is -1, no whitespace at all is added.
6840 QString QDomDocument::toString(int indent) const
6843 QTextStream s(&str, QIODevice::WriteOnly);
6849 Converts the parsed document back to its textual representation
6850 and returns a QByteArray containing the data encoded as UTF-8.
6852 This function uses \a indent as the amount of space to indent
6857 QByteArray QDomDocument::toByteArray(int indent) const
6859 // ### if there is an encoding specified in the xml declaration, this
6860 // encoding declaration should be changed to utf8
6861 return toString(indent).toUtf8();
6866 Returns the document type of this document.
6868 QDomDocumentType QDomDocument::doctype() const
6871 return QDomDocumentType();
6872 return QDomDocumentType(IMPL->doctype());
6876 Returns a QDomImplementation object.
6878 QDomImplementation QDomDocument::implementation() const
6881 return QDomImplementation();
6882 return QDomImplementation(IMPL->implementation());
6886 Returns the root element of the document.
6888 QDomElement QDomDocument::documentElement() const
6891 return QDomElement();
6892 return QDomElement(IMPL->documentElement());
6896 Creates a new element called \a tagName that can be inserted into
6897 the DOM tree, e.g. using QDomNode::appendChild().
6899 If \a tagName is not a valid XML name, the behavior of this function is governed
6900 by QDomImplementation::InvalidDataPolicy.
6902 \sa createElementNS() QDomNode::appendChild() QDomNode::insertBefore()
6903 QDomNode::insertAfter()
6905 QDomElement QDomDocument::createElement(const QString& tagName)
6908 impl = new QDomDocumentPrivate();
6909 return QDomElement(IMPL->createElement(tagName));
6913 Creates a new document fragment, that can be used to hold parts of
6914 the document, e.g. when doing complex manipulations of the
6917 QDomDocumentFragment QDomDocument::createDocumentFragment()
6920 impl = new QDomDocumentPrivate();
6921 return QDomDocumentFragment(IMPL->createDocumentFragment());
6925 Creates a text node for the string \a value that can be inserted
6926 into the document tree, e.g. using QDomNode::appendChild().
6928 If \a value contains characters which cannot be stored as character
6929 data of an XML document (even in the form of character references), the
6930 behavior of this function is governed by QDomImplementation::InvalidDataPolicy.
6932 \sa QDomNode::appendChild() QDomNode::insertBefore() QDomNode::insertAfter()
6934 QDomText QDomDocument::createTextNode(const QString& value)
6937 impl = new QDomDocumentPrivate();
6938 return QDomText(IMPL->createTextNode(value));
6942 Creates a new comment for the string \a value that can be inserted
6943 into the document, e.g. using QDomNode::appendChild().
6945 If \a value contains characters which cannot be stored in an XML comment,
6946 the behavior of this function is governed by QDomImplementation::InvalidDataPolicy.
6948 \sa QDomNode::appendChild() QDomNode::insertBefore() QDomNode::insertAfter()
6950 QDomComment QDomDocument::createComment(const QString& value)
6953 impl = new QDomDocumentPrivate();
6954 return QDomComment(IMPL->createComment(value));
6958 Creates a new CDATA section for the string \a value that can be
6959 inserted into the document, e.g. using QDomNode::appendChild().
6961 If \a value contains characters which cannot be stored in a CDATA section,
6962 the behavior of this function is governed by
6963 QDomImplementation::InvalidDataPolicy.
6965 \sa QDomNode::appendChild() QDomNode::insertBefore() QDomNode::insertAfter()
6967 QDomCDATASection QDomDocument::createCDATASection(const QString& value)
6970 impl = new QDomDocumentPrivate();
6971 return QDomCDATASection(IMPL->createCDATASection(value));
6975 Creates a new processing instruction that can be inserted into the
6976 document, e.g. using QDomNode::appendChild(). This function sets
6977 the target for the processing instruction to \a target and the
6980 If \a target is not a valid XML name, or data if contains characters which cannot
6981 appear in a processing instruction, the behavior of this function is governed by
6982 QDomImplementation::InvalidDataPolicy.
6984 \sa QDomNode::appendChild() QDomNode::insertBefore() QDomNode::insertAfter()
6986 QDomProcessingInstruction QDomDocument::createProcessingInstruction(const QString& target,
6987 const QString& data)
6990 impl = new QDomDocumentPrivate();
6991 return QDomProcessingInstruction(IMPL->createProcessingInstruction(target, data));
6996 Creates a new attribute called \a name that can be inserted into
6997 an element, e.g. using QDomElement::setAttributeNode().
6999 If \a name is not a valid XML name, the behavior of this function is governed by
7000 QDomImplementation::InvalidDataPolicy.
7002 \sa createAttributeNS()
7004 QDomAttr QDomDocument::createAttribute(const QString& name)
7007 impl = new QDomDocumentPrivate();
7008 return QDomAttr(IMPL->createAttribute(name));
7012 Creates a new entity reference called \a name that can be inserted
7013 into the document, e.g. using QDomNode::appendChild().
7015 If \a name is not a valid XML name, the behavior of this function is governed by
7016 QDomImplementation::InvalidDataPolicy.
7018 \sa QDomNode::appendChild() QDomNode::insertBefore() QDomNode::insertAfter()
7020 QDomEntityReference QDomDocument::createEntityReference(const QString& name)
7023 impl = new QDomDocumentPrivate();
7024 return QDomEntityReference(IMPL->createEntityReference(name));
7028 Returns a QDomNodeList, that contains all the elements in the
7029 document with the name \a tagname. The order of the node list is
7030 the order they are encountered in a preorder traversal of the
7033 \sa elementsByTagNameNS() QDomElement::elementsByTagName()
7035 QDomNodeList QDomDocument::elementsByTagName(const QString& tagname) const
7037 return QDomNodeList(new QDomNodeListPrivate(impl, tagname));
7041 Imports the node \a importedNode from another document to this
7042 document. \a importedNode remains in the original document; this
7043 function creates a copy that can be used within this document.
7045 This function returns the imported node that belongs to this
7046 document. The returned node has no parent. It is not possible to
7047 import QDomDocument and QDomDocumentType nodes. In those cases
7048 this function returns a \link QDomNode::isNull() null node\endlink.
7050 If \a deep is true, this function imports not only the node \a
7051 importedNode but its whole subtree; if it is false, only the \a
7052 importedNode is imported. The argument \a deep has no effect on
7053 QDomAttr and QDomEntityReference nodes, since the descendants of
7054 QDomAttr nodes are always imported and those of
7055 QDomEntityReference nodes are never imported.
7057 The behavior of this function is slightly different depending on
7060 \header \i Node Type \i Behavior
7062 \i The owner element is set to 0 and the specified flag is
7063 set to true in the generated attribute. The whole subtree
7064 of \a importedNode is always imported for attribute nodes:
7065 \a deep has no effect.
7066 \row \i QDomDocument
7067 \i Document nodes cannot be imported.
7068 \row \i QDomDocumentFragment
7069 \i If \a deep is true, this function imports the whole
7070 document fragment; otherwise it only generates an empty
7072 \row \i QDomDocumentType
7073 \i Document type nodes cannot be imported.
7075 \i Attributes for which QDomAttr::specified() is true are
7076 also imported, other attributes are not imported. If \a
7077 deep is true, this function also imports the subtree of \a
7078 importedNode; otherwise it imports only the element node
7079 (and some attributes, see above).
7081 \i Entity nodes can be imported, but at the moment there is
7082 no way to use them since the document type is read-only in
7084 \row \i QDomEntityReference
7085 \i Descendants of entity reference nodes are never imported:
7086 \a deep has no effect.
7087 \row \i QDomNotation
7088 \i Notation nodes can be imported, but at the moment there is
7089 no way to use them since the document type is read-only in
7091 \row \i QDomProcessingInstruction
7092 \i The target and value of the processing instruction is
7093 copied to the new node.
7095 \i The text is copied to the new node.
7096 \row \i QDomCDATASection
7097 \i The text is copied to the new node.
7099 \i The text is copied to the new node.
7102 \sa QDomElement::setAttribute() QDomNode::insertBefore()
7103 QDomNode::insertAfter() QDomNode::replaceChild() QDomNode::removeChild()
7104 QDomNode::appendChild()
7106 QDomNode QDomDocument::importNode(const QDomNode& importedNode, bool deep)
7109 impl = new QDomDocumentPrivate();
7110 return QDomNode(IMPL->importNode(importedNode.impl, deep));
7114 Creates a new element with namespace support that can be inserted
7115 into the DOM tree. The name of the element is \a qName and the
7116 namespace URI is \a nsURI. This function also sets
7117 QDomNode::prefix() and QDomNode::localName() to appropriate values
7118 (depending on \a qName).
7120 If \a qName is an empty string, returns a null element regardless of
7121 whether the invalid data policy is set.
7125 QDomElement QDomDocument::createElementNS(const QString& nsURI, const QString& qName)
7128 impl = new QDomDocumentPrivate();
7129 return QDomElement(IMPL->createElementNS(nsURI, qName));
7133 Creates a new attribute with namespace support that can be
7134 inserted into an element. The name of the attribute is \a qName
7135 and the namespace URI is \a nsURI. This function also sets
7136 QDomNode::prefix() and QDomNode::localName() to appropriate values
7137 (depending on \a qName).
7139 If \a qName is not a valid XML name, the behavior of this function is governed by
7140 QDomImplementation::InvalidDataPolicy.
7142 \sa createAttribute()
7144 QDomAttr QDomDocument::createAttributeNS(const QString& nsURI, const QString& qName)
7147 impl = new QDomDocumentPrivate();
7148 return QDomAttr(IMPL->createAttributeNS(nsURI, qName));
7152 Returns a QDomNodeList that contains all the elements in the
7153 document with the local name \a localName and a namespace URI of
7154 \a nsURI. The order of the node list is the order they are
7155 encountered in a preorder traversal of the element tree.
7157 \sa elementsByTagName() QDomElement::elementsByTagNameNS()
7159 QDomNodeList QDomDocument::elementsByTagNameNS(const QString& nsURI, const QString& localName)
7161 return QDomNodeList(new QDomNodeListPrivate(impl, nsURI, localName));
7165 Returns the element whose ID is equal to \a elementId. If no
7166 element with the ID was found, this function returns a \link
7167 QDomNode::isNull() null element\endlink.
7169 Since the QDomClasses do not know which attributes are element
7170 IDs, this function returns always a \link QDomNode::isNull() null
7171 element\endlink. This may change in a future version.
7173 QDomElement QDomDocument::elementById(const QString& /*elementId*/)
7175 qWarning("elementById() is not implemented and will always return a null node.");
7176 return QDomElement();
7180 \fn QDomNode::NodeType QDomDocument::nodeType() const
7182 Returns \c DocumentNode.
7187 /**************************************************************
7189 * Node casting functions
7191 **************************************************************/
7194 Converts a QDomNode into a QDomAttr. If the node is not an
7195 attribute, the returned object will be \link QDomNode::isNull()
7200 QDomAttr QDomNode::toAttr() const
7202 if (impl && impl->isAttr())
7203 return QDomAttr(((QDomAttrPrivate*)impl));
7208 Converts a QDomNode into a QDomCDATASection. If the node is not a
7209 CDATA section, the returned object will be \link
7210 QDomNode::isNull() null\endlink.
7212 \sa isCDATASection()
7214 QDomCDATASection QDomNode::toCDATASection() const
7216 if (impl && impl->isCDATASection())
7217 return QDomCDATASection(((QDomCDATASectionPrivate*)impl));
7218 return QDomCDATASection();
7222 Converts a QDomNode into a QDomDocumentFragment. If the node is
7223 not a document fragment the returned object will be \link
7224 QDomNode::isNull() null\endlink.
7226 \sa isDocumentFragment()
7228 QDomDocumentFragment QDomNode::toDocumentFragment() const
7230 if (impl && impl->isDocumentFragment())
7231 return QDomDocumentFragment(((QDomDocumentFragmentPrivate*)impl));
7232 return QDomDocumentFragment();
7236 Converts a QDomNode into a QDomDocument. If the node is not a
7237 document the returned object will be \link QDomNode::isNull()
7242 QDomDocument QDomNode::toDocument() const
7244 if (impl && impl->isDocument())
7245 return QDomDocument(((QDomDocumentPrivate*)impl));
7246 return QDomDocument();
7250 Converts a QDomNode into a QDomDocumentType. If the node is not a
7251 document type the returned object will be \link QDomNode::isNull()
7254 \sa isDocumentType()
7256 QDomDocumentType QDomNode::toDocumentType() const
7258 if (impl && impl->isDocumentType())
7259 return QDomDocumentType(((QDomDocumentTypePrivate*)impl));
7260 return QDomDocumentType();
7264 Converts a QDomNode into a QDomElement. If the node is not an
7265 element the returned object will be \link QDomNode::isNull()
7270 QDomElement QDomNode::toElement() const
7272 if (impl && impl->isElement())
7273 return QDomElement(((QDomElementPrivate*)impl));
7274 return QDomElement();
7278 Converts a QDomNode into a QDomEntityReference. If the node is not
7279 an entity reference, the returned object will be \link
7280 QDomNode::isNull() null\endlink.
7282 \sa isEntityReference()
7284 QDomEntityReference QDomNode::toEntityReference() const
7286 if (impl && impl->isEntityReference())
7287 return QDomEntityReference(((QDomEntityReferencePrivate*)impl));
7288 return QDomEntityReference();
7292 Converts a QDomNode into a QDomText. If the node is not a text,
7293 the returned object will be \link QDomNode::isNull() null\endlink.
7297 QDomText QDomNode::toText() const
7299 if (impl && impl->isText())
7300 return QDomText(((QDomTextPrivate*)impl));
7305 Converts a QDomNode into a QDomEntity. If the node is not an
7306 entity the returned object will be \link QDomNode::isNull()
7311 QDomEntity QDomNode::toEntity() const
7313 if (impl && impl->isEntity())
7314 return QDomEntity(((QDomEntityPrivate*)impl));
7315 return QDomEntity();
7319 Converts a QDomNode into a QDomNotation. If the node is not a
7320 notation the returned object will be \link QDomNode::isNull()
7325 QDomNotation QDomNode::toNotation() const
7327 if (impl && impl->isNotation())
7328 return QDomNotation(((QDomNotationPrivate*)impl));
7329 return QDomNotation();
7333 Converts a QDomNode into a QDomProcessingInstruction. If the node
7334 is not a processing instruction the returned object will be \link
7335 QDomNode::isNull() null\endlink.
7337 \sa isProcessingInstruction()
7339 QDomProcessingInstruction QDomNode::toProcessingInstruction() const
7341 if (impl && impl->isProcessingInstruction())
7342 return QDomProcessingInstruction(((QDomProcessingInstructionPrivate*)impl));
7343 return QDomProcessingInstruction();
7347 Converts a QDomNode into a QDomCharacterData. If the node is not a
7348 character data node the returned object will be \link
7349 QDomNode::isNull() null\endlink.
7351 \sa isCharacterData()
7353 QDomCharacterData QDomNode::toCharacterData() const
7355 if (impl && impl->isCharacterData())
7356 return QDomCharacterData(((QDomCharacterDataPrivate*)impl));
7357 return QDomCharacterData();
7361 Converts a QDomNode into a QDomComment. If the node is not a
7362 comment the returned object will be \link QDomNode::isNull()
7367 QDomComment QDomNode::toComment() const
7369 if (impl && impl->isComment())
7370 return QDomComment(((QDomCommentPrivate*)impl));
7371 return QDomComment();
7374 /**************************************************************
7378 **************************************************************/
7380 QDomHandler::QDomHandler(QDomDocumentPrivate* adoc, bool namespaceProcessing)
7381 : errorLine(0), errorColumn(0), doc(adoc), node(adoc), cdata(false),
7382 nsProcessing(namespaceProcessing), locator(0)
7386 QDomHandler::~QDomHandler()
7390 bool QDomHandler::endDocument()
7392 // ### is this really necessary? (rms)
7398 bool QDomHandler::startDTD(const QString& name, const QString& publicId, const QString& systemId)
7400 doc->doctype()->name = name;
7401 doc->doctype()->publicId = publicId;
7402 doc->doctype()->systemId = systemId;
7406 bool QDomHandler::startElement(const QString& nsURI, const QString&, const QString& qName, const QXmlAttributes& atts)
7411 n = doc->createElementNS(nsURI, qName);
7413 n = doc->createElement(qName);
7419 n->setLocation(locator->lineNumber(), locator->columnNumber());
7421 node->appendChild(n);
7425 for (int i=0; i<atts.length(); i++)
7428 ((QDomElementPrivate*)node)->setAttributeNS(atts.uri(i), atts.qName(i), atts.value(i));
7430 ((QDomElementPrivate*)node)->setAttribute(atts.qName(i), atts.value(i));
7437 bool QDomHandler::endElement(const QString&, const QString&, const QString&)
7439 if (!node || node == doc)
7441 node = node->parent();
7446 bool QDomHandler::characters(const QString& ch)
7448 // No text as child of some document
7452 QScopedPointer<QDomNodePrivate> n;
7454 n.reset(doc->createCDATASection(ch));
7455 } else if (!entityName.isEmpty()) {
7456 QScopedPointer<QDomEntityPrivate> e(new QDomEntityPrivate(doc, 0, entityName,
7457 QString(), QString(), QString()));
7460 doc->doctype()->appendChild(e.data());
7462 n.reset(doc->createEntityReference(entityName));
7464 n.reset(doc->createTextNode(ch));
7466 n->setLocation(locator->lineNumber(), locator->columnNumber());
7467 node->appendChild(n.data());
7473 bool QDomHandler::processingInstruction(const QString& target, const QString& data)
7476 n = doc->createProcessingInstruction(target, data);
7478 n->setLocation(locator->lineNumber(), locator->columnNumber());
7479 node->appendChild(n);
7486 extern bool qt_xml_skipped_entity_in_content;
7487 bool QDomHandler::skippedEntity(const QString& name)
7489 // we can only handle inserting entity references into content
7490 if (!qt_xml_skipped_entity_in_content)
7493 QDomNodePrivate *n = doc->createEntityReference(name);
7494 n->setLocation(locator->lineNumber(), locator->columnNumber());
7495 node->appendChild(n);
7499 bool QDomHandler::fatalError(const QXmlParseException& exception)
7501 errorMsg = exception.message();
7502 errorLine = exception.lineNumber();
7503 errorColumn = exception.columnNumber();
7504 return QXmlDefaultHandler::fatalError(exception);
7507 bool QDomHandler::startCDATA()
7513 bool QDomHandler::endCDATA()
7519 bool QDomHandler::startEntity(const QString &name)
7525 bool QDomHandler::endEntity(const QString &)
7531 bool QDomHandler::comment(const QString& ch)
7534 n = doc->createComment(ch);
7535 n->setLocation(locator->lineNumber(), locator->columnNumber());
7536 node->appendChild(n);
7540 bool QDomHandler::unparsedEntityDecl(const QString &name, const QString &publicId, const QString &systemId, const QString ¬ationName)
7542 QDomEntityPrivate* e = new QDomEntityPrivate(doc, 0, name,
7543 publicId, systemId, notationName);
7544 // keep the refcount balanced: appendChild() does a ref anyway.
7546 doc->doctype()->appendChild(e);
7550 bool QDomHandler::externalEntityDecl(const QString &name, const QString &publicId, const QString &systemId)
7552 return unparsedEntityDecl(name, publicId, systemId, QString());
7555 bool QDomHandler::notationDecl(const QString & name, const QString & publicId, const QString & systemId)
7557 QDomNotationPrivate* n = new QDomNotationPrivate(doc, 0, name, publicId, systemId);
7558 // keep the refcount balanced: appendChild() does a ref anyway.
7560 doc->doctype()->appendChild(n);
7564 void QDomHandler::setDocumentLocator(QXmlLocator *locator)
7566 this->locator = locator;