最近使用libxml2想做点东西,翻看一些example后还是有些疑问,去segmentfault问了下,感谢@pingjiang的热心解答,问题解决,记录如下

(一)如下是一个XML文件,p为根结点

<p>
<one>1</one>
<two>2</two>
<three>3</three>
</p>

我想在根节点之外再添加个标签如下:

<main>
<p>
<one>1</one>
<two>2</two>
<three>3</three>
</p>
</main>

(二)还有就是有一个子结构如下

<p>
<a1>
<one>1</one>
<two>2</two>
<three>3</three>
</a1>
<a2>
<one>1</one>
<two>2</two>
<three>3</three>
</a2>
......
</p>

向去掉最外层的p标签,而里面的内容保留(我找的示例删除节点,里面的子节点也都没有了)。

完整的实现:

/**
* section: Tree
* synopsis: Navigates a tree to print element names
* purpose: Parse a file to a tree, use xmlDocGetRootElement() to
* get the root element, then walk the document and print
* all the element name in document order.
* usage: tree1 filename_or_URL
* test: tree1 test2.xml > tree1.tmp && diff tree1.tmp $(srcdir)/tree1.res
* author: Dodji Seketeli
* copy: see Copyright for the status of this software.
*/
#include <stdio.h>
#include <string.h>
#include <libxml/parser.h>
#include <libxml/tree.h> /*
*To compile this file using gcc you can type
* gcc `xml2-config --cflags --libs` -o tree1 tree1.c
*Run this program
* ./tree1 test.xml
*/ static const char* LEVELS[] = {"", " ", " ", " ", " ", " ", " ", " " }; static void printTree(xmlNode * a_node, int level); /**
* print_element_names:
* @a_node: the initial xml node to consider.
*
* 打印所有兄弟节点和子节点的名字.
*/
static void print_element_names(xmlNode * a_node, const char* msg); // 根据标签名称获取节点(可以实现更加复杂的逻辑,获取指定节点)
static xmlNode *getNode(xmlNode *rootNode, const char* tag, xmlNode **parentNode); // 删除当前节点,但是保留子节点
static void removeNode(xmlNode *parentNode, xmlNode *nodeToDelete); // 用一个父节点包装子节点
static void wrapWithNode(xmlNode *parentNode, xmlNode *node, xmlNode *newNode); // 增加一个新节点
static void appendNewChildNode(xmlNode *parentNode, xmlNode *newNode); /**
* print_element_names:
* @a_node: the initial xml node to consider.
*
* Prints the names of the all the xml elements
* that are siblings or children of a given xml node.
*/
static int test_removeNode(const char* filepath); /**
* print_element_names:
* @a_node: the initial xml node to consider.
*
* Prints the names of the all the xml elements
* that are siblings or children of a given xml node.
*/
static int test_wrapWithNode(const char* filepath); int main(int argc, char **argv)
{
if (argc != 2) {
printf("error: invalid arguments");
return -1;
} /*
* this initialize the library and check potential ABI mismatches
* between the version it was compiled for and the actual shared
* library used.
*/
LIBXML_TEST_VERSION printf("test: removeNode:\n");
test_removeNode(argv[1]); printf("\n\ntest: wrapWithNode\n");
test_wrapWithNode(argv[1]); /*
*Free the global variables that may
*have been allocated by the parser.
*/
xmlCleanupParser(); return 0;
} void print_element_names(xmlNode * a_node, const char* msg)
{
xmlNode *cur_node = NULL; if (msg != NULL && strlen(msg) > 0) {
printf("print: %s\n", msg);
} for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
if (cur_node->type == XML_ELEMENT_NODE) {
printf("node type: Element, name: %s\n", cur_node->name);
} print_element_names(cur_node->children, "");
}
} void printTree(xmlNode * a_node, int level)
{
xmlNode *cur_node = NULL; //printf("%s", LEVELS[level]); for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
if (cur_node->type == XML_ELEMENT_NODE) {
printf("%s%s <%d>\n", LEVELS[level], cur_node->name, cur_node->type);
printTree(cur_node->children, level + 1);
} else {
printf("%s#%s <%d>\n", LEVELS[level], cur_node->name, cur_node->type);
}
}
} xmlNode *getNode(xmlNode *rootNode, const char* tag, xmlNode **parentNode) {
xmlNode *cur = rootNode;
if ((cur->type == XML_ELEMENT_NODE) && (!xmlStrcmp(cur->name, (const xmlChar *)tag))){
*parentNode = NULL;
return cur;
} *parentNode = cur;
cur = cur->xmlChildrenNode;
while (cur != NULL) {
if ((cur->type == XML_ELEMENT_NODE) && (!xmlStrcmp(cur->name, (const xmlChar *)tag))){
return cur;
} if (cur->type == XML_ELEMENT_NODE) {
*parentNode = cur;
}
cur = cur->next;
} return NULL;
} // 删除当前节点,但是保留子节点
void removeNode(xmlNode *parentNode, xmlNode *nodeToDelete) {
if (nodeToDelete == NULL) {
printf("error: nodeToDelete is null");
return;
} xmlNodePtr siblingNode = nodeToDelete->next; while (siblingNode != NULL) {
if (siblingNode->type == XML_ELEMENT_NODE) {
printf("debug: found sibling: %s\n", siblingNode->name);
break;
} siblingNode = siblingNode->next;
} printf("debug: parentNode: %s, nodeToDelete: %s\n", parentNode->name, nodeToDelete->name);
printTree(parentNode, 0); xmlNode *childrenNode = nodeToDelete->children;
if (childrenNode == NULL) {
printf("warn: childrenNode is null\n");
}
//xmlUnlinkNode(nodeToDelete->children); xmlNodePtr nextChildNode = NULL; while (childrenNode != NULL) {
printf("debug: childrenNode: %s\n", childrenNode->name);
nextChildNode = childrenNode->next;
xmlUnlinkNode(childrenNode); if (siblingNode != NULL) {
printf("debug: addPreSibling: %s, sibling is %s\n", childrenNode->name, siblingNode->name);
xmlAddPrevSibling(siblingNode, nextChildNode);
} else {
printf("debug: addChild: %s, parent is %s\n", childrenNode->name, parentNode->name);
printTree(childrenNode, 0);
xmlAddChild(parentNode, childrenNode);
} childrenNode = nextChildNode;
} xmlUnlinkNode(nodeToDelete);
xmlFreeNode(nodeToDelete);
} // 用一个父节点包装子节点
void wrapWithNode(xmlNode *parentNode, xmlNode *node, xmlNode *newNode) {
xmlUnlinkNode(node);
xmlAddChild(newNode, node);
xmlAddChild(parentNode, newNode);
} // 增加一个新节点
void appendNewChildNode(xmlNode *parentNode, xmlNode *newNode) {
xmlAddChild(parentNode, newNode);
} int test_removeNode(const char* filepath) {
xmlDoc *doc = NULL;
xmlNode *root_element = NULL;
xmlNode *parentNode = NULL;
xmlNode *curNode = NULL; /*parse the file and get the DOM */
doc = xmlReadFile(filepath, NULL, 0); if (doc == NULL) {
printf("error: could not parse file %s\n", filepath);
} /*Get the root element node */
root_element = xmlDocGetRootElement(doc); // 删除节点,但是保留子节点
curNode = getNode(root_element, "p", &parentNode);
if (curNode == NULL) {
printf("error: p node is not found");
return -1;
}
if (parentNode == NULL) {
// 根节点只能有一个子节点,这里就不处理了
printf("error: This is root node, should treat specially. root node should have only one node");
return -1;
}
removeNode(parentNode, curNode); // 重新获取跟节点,应该是main了
root_element = xmlDocGetRootElement(doc); print_element_names(root_element, "after delete"); /*free the document */
xmlFreeDoc(doc);
return 0;
} int test_wrapWithNode(const char* filepath) {
xmlDoc *doc = NULL;
xmlNode *root_element = NULL;
xmlNode *newNode = NULL; /*parse the file and get the DOM */
doc = xmlReadFile(filepath, NULL, 0); if (doc == NULL) {
printf("error: could not parse file %s\n", filepath);
} /*Get the root element node */
root_element = xmlDocGetRootElement(doc); // 增加一个父节点,根节点需要特殊处理
xmlUnlinkNode(root_element);
newNode = xmlNewNode(NULL, BAD_CAST "main");
xmlAddChild(newNode, root_element);
xmlDocSetRootElement(doc, newNode);
// 重新获取跟节点,应该是main了
root_element = xmlDocGetRootElement(doc); print_element_names(root_element, "after wrap"); /*free the document */
xmlFreeDoc(doc); return 0;
}

示例使用的是如下XML文件:

<parent>
<p>
<a1>
<one>1</one>
<two>2</two>
<three>3</three>
</a1>
<a2>
<one>1</one>
<two>2</two>
<three>3</three>
</a2>
</p>
</parent>

结果:

print: after delete **删除节点p后的节点树**
node type: Element, name: parent
node type: Element, name: a1
node type: Element, name: one
node type: Element, name: two
node type: Element, name: three
node type: Element, name: a2
node type: Element, name: one
node type: Element, name: two
node type: Element, name: three test: wrapWithNode **增加一个main节点后的节点树**
print: after wrap
node type: Element, name: main
node type: Element, name: parent
node type: Element, name: p
node type: Element, name: a1
node type: Element, name: one
node type: Element, name: two
node type: Element, name: three
node type: Element, name: a2
node type: Element, name: one
node type: Element, name: two
node type: Element, name: three

代码还没仔细看,略看下,写的非常不错,真乃大神,看来得细细的啃一下libxml2的源码才行….
想要完成全部的XML处理还需自己多多的了解。

C语言两个libxml2库使用的问题的更多相关文章

  1. C++的XML编程经验――LIBXML2库使用指南[转]

    C++的XML编程经验――LIBXML2库使用指南 写这篇文章的原因有如下几点:1)C++标准库中没有操作XML的方法,用C++操作XML文件必须熟悉一种函数库,LIBXML2是其中一种很优秀的XML ...

  2. C++的XML编程经验――LIBXML2库使用指南

    C++的XML编程经验――LIBXML2库使用指南 写这篇文章的原因有如下几点:1)C++标准库中没有操作XML的方法,用C++操作XML文件必须熟悉一种函数库,LIBXML2是其中一种很优秀的XML ...

  3. 分享:写了一个 java 调用 C语言 开发的动态库的范例

    分享:写了一个 java 调用 C语言 开发的动态库的范例 cfunction.h   代码#pragma once#ifdef __cplusplusextern "C" {#e ...

  4. c++ 11开始语言本身和标准库支持并发编程

    c++ 11开始语言本身和标准库支持并发编程,意味着真正要到编译器从语言和标准库层面开始稳定,估计得到17标准出来.14稳定之后的事情了,根据历史经验,新特性的引入到稳定被广泛采用至少要一个大版本的跨 ...

  5. 【Go语言绘图】gg 库的基本使用

    最近接了个比较大的需求,需要做很多图片处理的事情,比如图片的旋转裁截拼接,各种渐变处理,文字排列,一开始光是想想就头疼.但没有办法,既然已经需求已经到手上了,那就得把它做好才行,于是便开始被迫营业,无 ...

  6. iOS解决两个静态库的冲突 duplicate symbol

    http://blog.163.com/023_dns/blog/static/118727366201391544630380/ 场景: 解决TencentOpenAPI.framework与Zba ...

  7. Python3.x(windows系统)安装libxml2库

    Python3.x(windows系统)安装libxml2库 cmd安装命令: pip install lxml 执行结果: 再执行命令: pip install virtualenv 执行结果:

  8. R语言两种方式求指定日期所在月的天数

                 R语言两种方式求指定日期所在月的天数 days_monthday<-function(date){ m<-format(date,format="%m& ...

  9. Go语言的标准net库使用

    Go语言的标准net库使用 与大多数语言一样,Go的标准库是很全的,因为Go的出现本来就是为了网络通信的高并发实现,所以其相关的网络库封装得很简洁,也更加的易读.这里对使用到的api进行记录. net ...

随机推荐

  1. MEF初体验之五:Lazy Exports

    在一个部件组合中,导入将触发一个部件或者多个部件的实例化,这些部件暴露了所需原请求部件的必要的导入.对于一些应用程序来说,延迟实例化-防止图结构下的递归组合-可能对于将创建一个长久复杂的开销很大而不必 ...

  2. springmvc 接收对象 滴灌摘要

    js 对象 该阵列看起来像 我明白http://blog.csdn.net/baicp3/article/details/12752255本文 我们指示样品棒 data3一个js对象.遗嘱java当代 ...

  3. 设计模式16:迭代模式(Iterator)

    迭代模式: 它提供了一种方法没有对象的顺序访问聚合对象的暴漏底层的细节. Provide a way to access the elements of an aggregate object seq ...

  4. 前端是Sencha Touch+ Cordova(转)

    从13年初开始,我的关注点一直在两个点上,一个是股票,一个是移动前端和大数据技术,互联网金融的发展会让互联网证券越来越火热,当然,我也希望将这两个关注点结合到一起,做一些事情.   现在,我的APP和 ...

  5. POJ 1113 || HDU 1348: wall(凸包问题)

    传送门: POJ:点击打开链接 HDU:点击打开链接 以下是POJ上的题: Wall Time Limit: 1000MS   Memory Limit: 10000K Total Submissio ...

  6. JMeter怎么使用代理服务器

    一.JMeter有一个内置的代理服务器,主要用户使用浏览器录制脚本,在左侧的WorkBench中添加HTTP Proxy Server即可, 其中port表示代理服务器段口号, URL Pattern ...

  7. nyoj 517 最小公倍数 【java睑板】

    我写了一个gcd TL该.然后调用math内gcd,AC该... 思维:它是采取n前面的最小公倍数和n求 1~n的最小公倍数 代码: import java.util.Scanner; import ...

  8. 为代码减负之&lt;二&gt;存储过程(SQL)

    在上篇博客中介绍到了触发器的使用,而且当中也提到了触发器是个特殊的存储过程,那么什么是存储过程呢?他们 两个又究竟有什么差别呢? 事实上最基本的差别就是,触发器是当满足条件时系统自己主动运行的,而存储 ...

  9. 优秀团队建设--美国式团队(ppt)

    美国式团队 一.团队精神 团队精神反映一个人的素养.一个人的能力,一个人与别人合作的精神和能力.一个团队是个有机的总体,作为个人,仅仅有全然融入到这个有机总体之中,才干最大限度地体现自己的价值. 团队 ...

  10. crawler_基础之_java.net.HttpURLConnection 访问网络资源

    java访问网络资源 由底层到封装  为  scoket==> java.net.HttpURLConnection==>HttpClient 这次阐述先 java.net.HttpURL ...