📄 qdom.cpp
字号:
{ // Error check if (oldChild->parent() != this) return 0; // "mark lists as dirty" qt_nodeListTime++; // Perhaps oldChild was just created with "createElement" or that. In this case // its parent is QDomDocument but it is not part of the documents child list. if (oldChild->next == 0 && oldChild->prev == 0 && first != oldChild) return 0; if (oldChild->next) oldChild->next->prev = oldChild->prev; if (oldChild->prev) oldChild->prev->next = oldChild->next; if (last == oldChild) last = oldChild->prev; if (first == oldChild) first = oldChild->next; oldChild->setNoParent(); oldChild->next = 0; oldChild->prev = 0; // We are no longer interested in the old node if (oldChild) oldChild->ref.deref(); return oldChild;}QDomNodePrivate* QDomNodePrivate::appendChild(QDomNodePrivate* newChild){ // No reference manipulation needed. Done in insertAfter. return insertAfter(newChild, 0);}QDomDocumentPrivate* QDomNodePrivate::ownerDocument(){ QDomNodePrivate* p = this; while (p && !p->isDocument()) { if (!p->hasParent) return (QDomDocumentPrivate*)p->ownerNode; p = p->parent(); } return (QDomDocumentPrivate*)p;}QDomNodePrivate* QDomNodePrivate::cloneNode(bool deep){ QDomNodePrivate* p = new QDomNodePrivate(this, deep); // We are not interested in this node p->ref.deref(); return p;}static void qNormalizeNode(QDomNodePrivate* n){ QDomNodePrivate* p = n->first; QDomTextPrivate* t = 0; while (p) { if (p->isText()) { if (t) { QDomNodePrivate* tmp = p->next; t->appendData(p->nodeValue()); n->removeChild(p); p = tmp; } else { t = (QDomTextPrivate*)p; p = p->next; } } else { p = p->next; t = 0; } }}void QDomNodePrivate::normalize(){ // ### This one has moved from QDomElementPrivate to this position. It is // not tested. qNormalizeNode(this);}/*! \internal \a depth is used for indentation, it seems. */void QDomNodePrivate::save(QTextStream& s, int depth, int indent) const{ const QDomNodePrivate* n = first; while (n) { n->save(s, depth, indent); n = n->next; }}void QDomNodePrivate::setLocation(int lineNumber, int columnNumber){ this->lineNumber = lineNumber; this->columnNumber = columnNumber;}/************************************************************** * * QDomNode * **************************************************************/#define IMPL ((QDomNodePrivate*)impl)/*! \class QDomNode \reentrant \brief The QDomNode class is the base class for all the nodes in a DOM tree. \module XML \ingroup xml-tools \mainclass Many functions in the DOM return a QDomNode. You can find out the type of a node using isAttr(), isCDATASection(), isDocumentFragment(), isDocument(), isDocumentType(), isElement(), isEntityReference(), isText(), isEntity(), isNotation(), isProcessingInstruction(), isCharacterData() and isComment(). A QDomNode can be converted into one of its subclasses using toAttr(), toCDATASection(), toDocumentFragment(), toDocument(), toDocumentType(), toElement(), toEntityReference(), toText(), toEntity(), toNotation(), toProcessingInstruction(), toCharacterData() or toComment(). You can convert a node to a null node with clear(). Copies of the QDomNode class share their data using explicit sharing. This means that modifying one node will change all copies. This is especially useful in combination with functions which return a QDomNode, e.g. firstChild(). You can make an independent (deep) copy of the node with cloneNode(). A QDomNode can be null, much like a null pointer. Creating a copy of a null node results in another null node. It is not possible to modify a null node, but it is possible to assign another, possibly non-null node to it. In this case, the copy of the null node will remain null. You can check if a QDomNode is null by calling isNull(). The empty constructor of a QDomNode (or any of the derived classes) creates a null node. Nodes are inserted with insertBefore(), insertAfter() or appendChild(). You can replace one node with another using replaceChild() and remove a node with removeChild(). To traverse nodes use firstChild() to get a node's first child (if any), and nextSibling() to traverse. QDomNode also provides lastChild(), previousSibling() and parentNode(). To find the first child node with a particular node name use namedItem(). To find out if a node has children use hasChildNodes() and to get a list of all of a node's children use childNodes(). The node's name and value (the meaning of which varies depending on its type) is returned by nodeName() and nodeValue() respectively. The node's type is returned by nodeType(). The node's value can be set with setNodeValue(). The document to which the node belongs is returned by ownerDocument(). Adjacent QDomText nodes can be merged into a single node with normalize(). \l QDomElement nodes have attributes which can be retrieved with attributes(). QDomElement and QDomAttr nodes can have namespaces which can be retrieved with namespaceURI(). Their local name is retrieved with localName(), and their prefix with prefix(). The prefix can be set with setPrefix(). You can write the XML representation of the node to a text stream with save(). The following example looks for the first element in an XML document and prints the names of all the elements that are its direct children. \code QDomDocument d; d.setContent(someXML); QDomNode n = d.firstChild(); while (!n.isNull()) { if (n.isElement()) { QDomElement e = n.toElement(); cout << "Element name: " << e.tagName() << endl; break; } n = n.nextSibling(); } \endcode For further information about the Document Object Model see \l{http://www.w3.org/TR/REC-DOM-Level-1/}{Level 1} and \l{http://www.w3.org/TR/DOM-Level-2-Core/}{Level 2 Core}. For a more general introduction of the DOM implementation see the QDomDocument documentation.*//*! Constructs a \link isNull() null\endlink node.*/QDomNode::QDomNode(){ impl = 0;}/*! Constructs a copy of \a n. The data of the copy is shared (shallow copy): modifying one node will also change the other. If you want to make a deep copy, use cloneNode().*/QDomNode::QDomNode(const QDomNode &n){ impl = n.impl; if (impl) impl->ref.ref();}/*! \internal Constructs a new node for the data \a n.*/QDomNode::QDomNode(QDomNodePrivate *n){ impl = n; if (impl) impl->ref.ref();}/*! Assigns a copy of \a n to this DOM node. The data of the copy is shared (shallow copy): modifying one node will also change the other. If you want to make a deep copy, use cloneNode().*/QDomNode& QDomNode::operator=(const QDomNode &n){ QDomNodePrivate *x = n.impl; if (x) x->ref.ref(); x = qAtomicSetPtr(&impl, x); if (x && !x->ref.deref()) delete x; return *this;}/*! Returns true if \a n and this DOM node are equal; otherwise returns false. Any instance of QDomNode acts as a reference to an underlying data structure in QDomDocument. The test for equality checks if the two references point to the same underlying node. For example: \code QDomDocument document; QDomElement element1 = document.documentElement(); QDomElement element2 = element1; \endcode The two nodes (QDomElement is a QDomNode subclass) both refer to the document's root element, and \c {element1 == element2} will return true. On the other hand: \code QDomElement element3 = document.createElement("MyElement"); QDomElement element4 = document.createElement("MyElement"); \endcode Even though both nodes are empty elements carrying the same name, \c {element3 == element4} will return false because they refer to two different nodes in the underlying data structure.*/bool QDomNode::operator== (const QDomNode& n) const{ return (impl == n.impl);}/*! Returns true if \a n and this DOM node are not equal; otherwise returns false.*/bool QDomNode::operator!= (const QDomNode& n) const{ return (impl != n.impl);}/*! Destroys the object and frees its resources.*/QDomNode::~QDomNode(){ if (impl && !impl->ref.deref()) delete impl;}/*! Returns the name of the node. The meaning of the name depends on the subclass: \table \header \i Name \i Meaning \row \i QDomAttr \i The name of the attribute \row \i QDomCDATASection \i The string "#cdata-section" \row \i QDomComment \i The string "#comment" \row \i QDomDocument \i The string "#document" \row \i QDomDocumentFragment \i The string "#document-fragment" \row \i QDomDocumentType \i The name of the document type \row \i QDomElement \i The tag name \row \i QDomEntity \i The name of the entity \row \i QDomEntityReference \i The name of the referenced entity \row \i QDomNotation \i The name of the notation \row \i QDomProcessingInstruction \i The target of the processing instruction \row \i QDomText \i The string "#text" \endtable \bold{Note:} This function does not take the presence of namespaces into account when processing the names of element and attribute nodes. As a result, the returned name can contain any namespace prefix that may be present. To obtain the node name of an element or attribute, use localName(); to obtain the namespace prefix, use namespaceURI(). \sa nodeValue()*/QString QDomNode::nodeName() const{ if (!impl) return QString(); if (!IMPL->prefix.isEmpty()) return IMPL->prefix + QLatin1Char(':') + IMPL->name; return IMPL->name;}/*! Returns the value of the node. The meaning of the value depends on the subclass: \table \header \i Name \i Meaning \row \i QDomAttr \i The attribute value \row \i QDomCDATASection \i The content of the CDATA section \row \i QDomComment \i The comment \row \i QDomProcessingInstruction \i The data of the processing instruction \row \i QDomText \i The text \endtable All the other subclasses do not have a node value and will return an empty string. \sa setNodeValue() nodeName()*/QString QDomNode::nodeValue() const{ if (!impl) return QString(); return IMPL->value;}/*! Sets the node's value to \a v. \sa nodeValue()*/void QDomNode::setNodeValue(const QString& v){ if (!impl) return; IMPL->setNodeValue(v);}/*! \enum QDomNode::NodeType This enum defines the type of the node: \value ElementNode \value AttributeNode \value TextNode \value CDATASectionNode \value EntityReferenceNode \value EntityNode \value ProcessingInstructionNode \value CommentNode \value DocumentNode \value DocumentTypeNode \value DocumentFragmentNode \value NotationNode \value BaseNode A QDomNode object, i.e. not a QDomNode subclass. \value CharacterDataNode*//*! Returns the type of the node. \sa toAttr(), toCDATASection(), toDocumentFragment(), toDocument() toDocumentType(), toElement(), toEntityReference(), toText(), toEntity() toNotation(), toProcessingInstruction(), toCharacterData(), toComment()*/QDomNode::NodeType QDomNode::nodeType() const{ if (!impl) return QDomNode::BaseNode; return IMPL->nodeType();}/*! Returns the parent node. If this node has no parent, a null node is returned (i.e. a node for which isNull() returns true).*/QDomNode QDomNode::parentNode() const{ if (!impl) return QDomNode(); return QDomNode(IMPL->parent());}/*! Returns a list of all direct child nodes. Most often you will call this function on a QDomElement object. For example, if the XML document looks like this: \code <body> <h1>Heading</h1> <p>Hello <b>you</b></p> </body> \endcode Then the list of child no
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -