📄 tree.c
字号:
xmlBufferAdd(cur->content, content, len);#endif } return(cur);}/** * xmlNewDocComment: * @doc: the document * @content: the comment content * * Creation of a new node containing a commentwithin a document. * Returns a pointer to the new node object. */xmlNodePtrxmlNewDocComment(xmlDocPtr doc, const xmlChar *content) { xmlNodePtr cur; cur = xmlNewComment(content); if (cur != NULL) cur->doc = doc; return(cur);}/** * xmlNewChild: * @parent: the parent node * @ns: a namespace if any * @name: the name of the child * @content: the XML content of the child if any. * * Creation of a new child element, added at the end of @parent children list. * @ns and @content parameters are optionnal (NULL). If content is non NULL, * a child list containing the TEXTs and ENTITY_REFs node will be created. * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities * references, but XML special chars need to be escaped first by using * xmlEncodeEntitiesReentrant(). Use xmlNewTextChild() if entities * support is not needed. * * Returns a pointer to the new node object. */xmlNodePtrxmlNewChild(xmlNodePtr parent, xmlNsPtr ns, const xmlChar *name, const xmlChar *content) { xmlNodePtr cur, prev; if (parent == NULL) {#ifdef DEBUG_TREE fprintf(stderr, "xmlNewChild : parent == NULL\n");#endif return(NULL); } if (name == NULL) {#ifdef DEBUG_TREE fprintf(stderr, "xmlNewChild : name == NULL\n");#endif return(NULL); } /* * Allocate a new node */ if (ns == NULL) cur = xmlNewDocNode(parent->doc, parent->ns, name, content); else cur = xmlNewDocNode(parent->doc, ns, name, content); if (cur == NULL) return(NULL); /* * add the new element at the end of the children list. */ cur->type = XML_ELEMENT_NODE; cur->parent = parent; cur->doc = parent->doc; if (parent->children == NULL) { parent->children = cur; parent->last = cur; } else { prev = parent->last; prev->next = cur; cur->prev = prev; parent->last = cur; } return(cur);}/** * xmlAddNextSibling: * @cur: the child node * @elem: the new node * * Add a new element @elem as the next siblings of @cur * If the new element was already inserted in a document it is * first unlinked from its existing context. * * Returns the new element or NULL in case of error. */xmlNodePtrxmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) { if (cur == NULL) {#ifdef DEBUG_TREE fprintf(stderr, "xmlAddNextSibling : cur == NULL\n");#endif return(NULL); } if (elem == NULL) {#ifdef DEBUG_TREE fprintf(stderr, "xmlAddNextSibling : elem == NULL\n");#endif return(NULL); } xmlUnlinkNode(elem); elem->doc = cur->doc; elem->parent = cur->parent; elem->prev = cur; elem->next = cur->next; cur->next = elem; if (elem->next != NULL) elem->next->prev = elem; if ((elem->parent != NULL) && (elem->parent->last == cur)) elem->parent->last = elem; return(elem);}/** * xmlAddPrevSibling: * @cur: the child node * @elem: the new node * * Add a new element @elem as the previous siblings of @cur * If the new element was already inserted in a document it is * first unlinked from its existing context. * * Returns the new element or NULL in case of error. */xmlNodePtrxmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) { if (cur == NULL) {#ifdef DEBUG_TREE fprintf(stderr, "xmlAddPrevSibling : cur == NULL\n");#endif return(NULL); } if (elem == NULL) {#ifdef DEBUG_TREE fprintf(stderr, "xmlAddPrevSibling : elem == NULL\n");#endif return(NULL); } xmlUnlinkNode(elem); elem->doc = cur->doc; elem->parent = cur->parent; elem->next = cur; elem->prev = cur->prev; cur->prev = elem; if (elem->prev != NULL) elem->prev->next = elem; if ((elem->parent != NULL) && (elem->parent->children == cur)) elem->parent->children = elem; return(elem);}/** * xmlAddSibling: * @cur: the child node * @elem: the new node * * Add a new element @elem to the list of siblings of @cur * If the new element was already inserted in a document it is * first unlinked from its existing context. * * Returns the new element or NULL in case of error. */xmlNodePtrxmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) { xmlNodePtr parent; if (cur == NULL) {#ifdef DEBUG_TREE fprintf(stderr, "xmlAddSibling : cur == NULL\n");#endif return(NULL); } if (elem == NULL) {#ifdef DEBUG_TREE fprintf(stderr, "xmlAddSibling : elem == NULL\n");#endif return(NULL); } /* * Constant time is we can rely on the ->parent->last to find * the last sibling. */ if ((cur->parent != NULL) && (cur->parent->children != NULL) && (cur->parent->last != NULL) && (cur->parent->last->next == NULL)) { cur = cur->parent->last; } else { while (cur->next != NULL) cur = cur->next; } xmlUnlinkNode(elem); if (elem->doc == NULL) elem->doc = cur->doc; /* the parent may not be linked to a doc ! */ parent = cur->parent; elem->prev = cur; elem->next = NULL; elem->parent = parent; cur->next = elem; if (parent != NULL) parent->last = elem; return(elem);}/** * xmlAddChild: * @parent: the parent node * @cur: the child node * * Add a new child element, to @parent, at the end of the child list. * Returns the child or NULL in case of error. */xmlNodePtrxmlAddChild(xmlNodePtr parent, xmlNodePtr cur) { xmlNodePtr prev; if (parent == NULL) {#ifdef DEBUG_TREE fprintf(stderr, "xmlAddChild : parent == NULL\n");#endif return(NULL); } if (cur == NULL) {#ifdef DEBUG_TREE fprintf(stderr, "xmlAddChild : child == NULL\n");#endif return(NULL); } if ((cur->doc != NULL) && (parent->doc != NULL) && (cur->doc != parent->doc)) {#ifdef DEBUG_TREE fprintf(stderr, "Elements moved to a different document\n");#endif } /* * add the new element at the end of the children list. */ cur->parent = parent; cur->doc = parent->doc; /* the parent may not be linked to a doc ! */ /* * Handle the case where parent->content != NULL, in that case it will * create a intermediate TEXT node. */ if (((parent->type == XML_ELEMENT_NODE) || (parent->type == XML_TEXT_NODE)) && (parent->content != NULL)) { xmlNodePtr text; #ifndef XML_USE_BUFFER_CONTENT text = xmlNewDocText(parent->doc, parent->content);#else text = xmlNewDocText(parent->doc, xmlBufferContent(parent->content));#endif if (text != NULL) { text->next = parent->children; if (text->next != NULL) text->next->prev = text; parent->children = text; UPDATE_LAST_CHILD(parent)#ifndef XML_USE_BUFFER_CONTENT xmlFree(parent->content);#else xmlBufferFree(parent->content);#endif parent->content = NULL; } } if (parent->children == NULL) { parent->children = cur; parent->last = cur; } else { prev = parent->last; prev->next = cur; cur->prev = prev; parent->last = cur; } return(cur);}/** * xmlGetLastChild: * @parent: the parent node * * Search the last child of a node. * Returns the last child or NULL if none. */xmlNodePtrxmlGetLastChild(xmlNodePtr parent) { if (parent == NULL) {#ifdef DEBUG_TREE fprintf(stderr, "xmlGetLastChild : parent == NULL\n");#endif return(NULL); } return(parent->last);}/** * xmlFreeNodeList: * @cur: the first node in the list * * Free a node and all its siblings, this is a recursive behaviour, all * the children are freed too. */voidxmlFreeNodeList(xmlNodePtr cur) { xmlNodePtr next; if (cur == NULL) {#ifdef DEBUG_TREE fprintf(stderr, "xmlFreeNodeList : node == NULL\n");#endif return; } while (cur != NULL) { next = cur->next; xmlFreeNode(cur); cur = next; }}/** * xmlFreeNode: * @cur: the node * * Free a node, this is a recursive behaviour, all the children are freed too. * This doesn't unlink the child from the list, use xmlUnlinkNode() first. */voidxmlFreeNode(xmlNodePtr cur) { if (cur == NULL) {#ifdef DEBUG_TREE fprintf(stderr, "xmlFreeNode : node == NULL\n");#endif return; } if (cur->type == XML_DTD_NODE) return; cur->doc = NULL; cur->parent = NULL; cur->next = NULL; cur->prev = NULL; if ((cur->children != NULL) && (cur->type != XML_ENTITY_REF_NODE)) xmlFreeNodeList(cur->children); if (cur->properties != NULL) xmlFreePropList(cur->properties); if (cur->type != XML_ENTITY_REF_NODE)#ifndef XML_USE_BUFFER_CONTENT if (cur->content != NULL) xmlFree(cur->content);#else if (cur->content != NULL) xmlBufferFree(cur->content);#endif if (cur->name != NULL) xmlFree((char *) cur->name); if (cur->nsDef != NULL) xmlFreeNsList(cur->nsDef); memset(cur, -1, sizeof(xmlNode)); xmlFree(cur);}/** * xmlUnlinkNode: * @cur: the node * * Unlink a node from it's current context, the node is not freed */voidxmlUnlinkNode(xmlNodePtr cur) { if (cur == NULL) {#ifdef DEBUG_TREE fprintf(stderr, "xmlUnlinkNode : node == NULL\n");#endif return; } if ((cur->parent != NULL) && (cur->parent->children == cur)) cur->parent->children = cur->next; if ((cur->parent != NULL) && (cur->parent->last == cur)) cur->parent->last = cur->prev; if (cur->next != NULL) cur->next->prev = cur->prev; if (cur->prev != NULL) cur->prev->next = cur->next; cur->next = cur->prev = NULL; cur->parent = NULL;}/** * xmlReplaceNode: * @old: the old node * @cur: the node * * Unlink the old node from it's current context, prune the new one * at the same place. If cur was already inserted in a document it is * first unlinked from its existing context. * * Returns the old node */xmlNodePtrxmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) { if (old == NULL) {#ifdef DEBUG_TREE fprintf(stderr, "xmlReplaceNode : old == NULL\n");#endif return(NULL); } if (cur == NULL) { xmlUnlinkNode(old); return(old); } xmlUnlinkNode(cur); cur->doc = old->doc; cur->parent = old->parent; cur->next = old->next; if (cur->next != NULL) cur->next->prev = cur; cur->prev = old->prev; if (cur->prev != NULL) cur->prev->next = cur; if (cur->parent != NULL) { if (cur->parent->children == old) cur->parent->children = cur; if (cur->parent->last == old) cur->parent->last = cur; } old->next = old->prev = NULL; old->parent = NULL; return(old);}/************************************************************************ * * * Copy operations * * * ************************************************************************/ /** * xmlCopyNamespace: * @cur: the namespace * * Do a copy of the namespace. * * Returns: a new xmlNsPtr, or NULL in case of error. */xmlNsPtrxmlCopyNamespace(xmlNsPtr cur) { xmlNsPtr ret; if (cur == NULL) return(NULL); switch (cur->type) { case XML_GLOBAL_NAMESPACE: ret = xmlNewGlobalNs(NULL, cur->href, cur->prefix); break; case XML_LOCAL_NAMESPACE: ret = xmlNewNs(NULL, cur->href, cur->prefix); break; default:#ifdef DEBUG_TREE fprintf(stderr, "xmlCopyNamespace: unknown type %d\n", cur->type);#endif return(NULL); } return(ret);}/** * xmlCopyNamespaceList: * @cur: the first namespace * * Do a copy of an namespace list. * * Returns: a new xmlNsPtr, or NULL in case of error. */xmlNsPtrxmlCopyNamespaceList(xmlNsPtr cur) { xmlNsPtr ret = NULL; xmlNsPtr p = NULL,q; while (cur != NULL) { q = xmlCopyNamespace(cur); if (p == NULL) { ret = p = q; } else { p->next = q; p = q; } cur = cur->next; } return(ret);}/** * xmlCopyProp: * @target: the element where the attribute will be grafted * @cur: the attribute * * Do a copy of the attribute. * * Returns: a new xmlAttrPtr, or NULL in case of error. */xmlAttrPtrxmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) { xmlAttrPtr ret; if (cur == NULL) return(NULL); if (cur->parent != NULL) ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL); else if (cur->children != NULL) ret = xmlNewDocProp(cur->children->doc, cur->name, NULL); else ret = xmlNewDocProp(NULL, cur->name, NULL); if (ret == NULL) return(NULL); ret->parent = target; if ((cur->ns != NULL) && (target != NULL)) { xmlNsPtr ns; ns = xmlSearchNs(target->doc, target, cur->ns->prefix); ret->ns = ns;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -