/****************************************************************************** **函数名称: _xml_delete_empty **功 能: 删除无属性节、无孩子、无节点值的节点,同时返回下一个需要处理的节点(注: 不删属性节点) **输入参数: ** xml: XML树 **输出参数: NONE **返 回: 0:success !0:failed **实现描述: **注意事项: node节点必须为子节点,否则处理过程的判断条件会有错误!!! **作 者: # Qifeng.zou # 2013.10.21 # ******************************************************************************/ static xml_node_t *_xml_delete_empty(xml_tree_t *xml, Stack_t *stack, xml_node_t *node) { xml_node_t *parent, *prev; do { parent = node->parent; prev = parent->child; if (prev == node) { parent->child = node->next; xml_node_free_one(xml, node); /* 释放空节点 */ if (NULL != parent->child) { return parent->child; /* 处理子节点的兄弟节点 */ } /* 已无兄弟: 则处理父节点 */ xml_unset_child_flag(parent); /* 继续后续处理 */ } else { while (prev->next != node) { prev = prev->next; } prev->next = node->next; xml_node_free_one(xml, node); /* 释放空节点 */ if (NULL != prev->next) { return prev->next; /* 还有兄弟: 则处理后续节点 */ } else { /* 已无兄弟: 则处理父节点 */ if (xml_is_attr(prev)) { xml_unset_child_flag(parent); } /* 继续后续处理 */ } } /* 开始处理父节点 */ node = parent; stack_pop(stack); /* 删除无属性、无孩子、无节点值的节点 */ if (!xml_has_attr(node) && !xml_has_value(node) && !xml_has_child(node)) { continue; } if (NULL != node->next) { return node->next; /* 处理父节点的兄弟节点 */ } node = stack_pop(stack); } while(NULL != node); return NULL; }
/****************************************************************************** **函数名称: xml_delete_child **功 能: 从孩子节点链表中删除指定的孩子节点 **输入参数: ** node: 需要删除孩子节点的节点 ** child: 孩子节点 **输出参数: **返 回: 0: 成功 !0: 失败 **实现描述: **注意事项: ** 从树中删除的节点,只是从树中被剥离出来,其相关内存并没有被释放. ** 释放时需调用函数xml_node_free() **作 者: # Qifeng.zou # 2013.03.02 # ******************************************************************************/ int xml_delete_child(xml_tree_t *xml, xml_node_t *node, xml_node_t *child) { xml_node_t *p1, *p2; if (node != child->parent) { log_error(xml->log, "Parent node is not right!"); return XML_ERR_PTR; } if (node->child == child) { /* 1. 要删的是子节点链表的开始节点 */ node->child = child->next; /* 剔除链表 */ if (NULL == node->child) { node->tail = NULL; if (xml_is_attr(child)) { xml_unset_attr_flag(node); } } else if (xml_is_attr(child) && !xml_is_attr(node->child)) { xml_unset_attr_flag(node); } return XML_OK; } p1 = node->child; p2 = p1->next; while (NULL != p2) { if (child == p2) { p1->next = child->next; /* 剔除链表 */ if (node->tail == child) { node->tail = p1; } if (NULL == child->next) { if (xml_is_child(child) && !xml_is_child(p1)) { xml_unset_child_flag(node); } } return XML_OK; } p1 = p2; p2 = p2->next; } return XML_OK; }