参考文章地址:http://blog.sina.com.cn/s/blog_6a1837e90100ns2q.html
http://www.blogjava.net/wxb_nudt/archive/2007/11/18/161340.html
环境: ubuntu 10.04 32位
http://blog.sina.com.cn/s/blog_6a1837e90100ns2q.html
1、下载与安装LIBXML2
Libxml2是一个C语言的XML程序库,可以简单方便的提供对XML文档的各种操作,并且支持XPATH查询,以及部分的支持XSLT转换等功能。Libxml2的下载地址是,完全版的库是开源的,并且带有例子程序和说明文档。最好将这个库先下载下来,因为这样可以查看其中的文档和例子。
由于我是在linux下用C语言进行开发的,所以我下载的是libxml2-2.6.20.tar.gz版本的源码包。
具体安装步骤:
1、解压:$tar zxvf libxml2-2.6.20.tar.gz
2、进入解压后的安装目录:$cd libxml2-2.6.20
3、安装三部曲:1)$./configure
2)$make
3)$make install
安装完毕。
2.先给出两个函数,一个创建xml文件,一个解析xml文件
首先给出create_xml.c 编译方法:gcc create_xml.c -o create_xml -I /usr/local/include/libxml2 -lxml2
-
/* create_xml.c */
-
-
#include <stdio.h>
-
#include <libxml/parser.h>
-
#include <libxml/tree.h>
-
-
int main(int argc, char **argv)
-
{
-
xmlDocPtr doc = NULL; /* document pointer */
-
xmlNodePtr root_node = NULL, node = NULL, node1 = NULL;/* node pointers */
-
-
// Creates a new document, a node and set it as a root node
-
doc = xmlNewDoc(BAD_CAST "1.0");
-
root_node = xmlNewNode(NULL, BAD_CAST "root");
-
xmlDocSetRootElement(doc, root_node);
-
-
//creates a new node, which is "attached" as child node of root_node node.
-
xmlNewChild(root_node, NULL, BAD_CAST "node1",BAD_CAST "content of node1");
-
-
// xmlNewProp() creates attributes, which is "attached" to an node.
-
node=xmlNewChild(root_node, NULL, BAD_CAST "node3", BAD_CAST"node has attributes");
-
xmlNewProp(node, BAD_CAST "attribute", BAD_CAST "yes");
-
-
//Here goes another way to create nodes.
-
node = xmlNewNode(NULL, BAD_CAST "node4");
-
node1 = xmlNewText(BAD_CAST"other way to create content");
-
xmlAddChild(node, node1);
-
xmlAddChild(root_node, node);
-
-
//Dumping document to stdio or file
-
xmlSaveFormatFileEnc(argc > 1 ? argv[1] : "-", doc, "UTF-8", 1);
-
-
/*free the document */
-
xmlFreeDoc(doc);
-
xmlCleanupParser();
-
xmlMemoryDump();//debug memory for regression tests
-
-
return 0;
-
}
运行./creat_xml hello.xml , hello.xml文件生成的结果如下:
-
<?xml version="1.0" encoding="UTF-8"?>
-
<root>
-
<node1>content of node1</node1>
-
<node3 attribute="yes">node has attributes</node3>
-
<node4>other way to create content</node4>
-
</root>
下面给出解析的函数 parse_xml.c ,编译方法如上
-
#include <stdio.h>
-
#include <libxml/parser.h>
-
#include <libxml/tree.h>
-
-
int main(int argc, char** argv)
-
{
-
xmlDocPtr doc=NULL;
-
xmlNodePtr cur=NULL;
-
char* name=NULL;
-
char* value=NULL;
-
-
xmlKeepBlanksDefault (0);
-
-
if(argc<2)
-
{
-
printf("ERROR: argc must be 2 or above.\n");
-
return -1;
-
}
-
-
//create Dom tree
-
doc=xmlParseFile(argv[1]);
-
if(doc==NULL)
-
{
-
printf("ERROR: Loading xml file failed.\n");
-
exit(1);
-
}
-
-
// get root node
-
cur=xmlDocGetRootElement(doc);
-
if(cur==NULL)
-
{
-
printf("ERROR: empty file\n");
-
xmlFreeDoc(doc);
-
exit(2);
-
}
-
-
//walk the tree
-
cur=cur->xmlChildrenNode; //get sub node
-
while(cur !=NULL)
-
{
-
name=(char*)(cur->name);
-
value=(char*)xmlNodeGetContent(cur);
-
printf("DEBUG: name is: %s, value is: %s\n", name, value);
-
xmlFree(value);
-
xmlChar* szAttr = xmlGetProp(cur,BAD_CAST "attribute");
-
printf("DEBUG: attribute is: %s\n", (char *)szAttr );
-
xmlFree(szAttr);
-
cur=cur->next;
-
}
-
-
// release resource of xml parser in libxml2
-
xmlFreeDoc(doc);
-
xmlCleanupParser();
-
-
return 0;
-
}
修改xml文件change_xml.c
-
#include <stdio.h>
-
#include <libxml/parser.h>
-
#include <libxml/tree.h>
-
-
int main(int argc, char** argv)
-
{
-
xmlDocPtr doc=NULL;
-
xmlNodePtr cur=NULL;
-
xmlNodePtr ptr=NULL;
-
char* name=NULL;
-
char* value=NULL;
-
-
xmlKeepBlanksDefault (0); //去除空行
-
-
if(argc<2)
-
{
-
printf("ERROR: argc must be 2 or above.\n");
-
return -1;
-
}
-
-
//create Dom tree
-
doc=xmlParseFile(argv[1]);
-
if(doc==NULL)
-
{
-
printf("ERROR: Loading xml file failed.\n");
-
exit(1);
-
}
-
-
// get root node
-
cur=xmlDocGetRootElement(doc);
-
if(cur==NULL)
-
{
-
printf("ERROR: empty file\n");
-
xmlFreeDoc(doc);
-
exit(2);
-
}
-
-
//walk the tree
-
cur=cur->xmlChildrenNode; //get sub node
-
ptr=cur->next;
-
xmlUnlinkNode(cur);
-
xmlFreeNode(cur);
-
cur=ptr;
-
-
cur=cur->next;
-
//修改节点的属性值
-
xmlSetProp(cur,BAD_CAST "attribute", BAD_CAST "no");
-
-
//修改节点的内容
-
xmlNodeSetContent(cur, BAD_CAST "content changed");
-
-
-
xmlSaveFormatFileEnc(argc > 1 ? argv[1] : "-", doc, "UTF-8", 1);
-
-
// release resource of xml parser in libxml2
-
xmlFreeDoc(doc);
-
xmlCleanupParser();
-
-
return 0;
-
}
编译之后运行 ./change_xml hello.xml之后,hello.xml的内容变为
-
<?xml version="1.0" encoding="UTF-8"?>
-
<root>
-
<node3 attribute="yes">node has attributes</node3>
-
<node4 attribute="no">content changed</node4>
-
</root>
下面两篇写的很详细的博文贴出:lixbxml2库使用指南
http://blog.sina.com.cn/s/blog_6a1837e90100ns2q.html
http://www.blogjava.net/wxb_nudt/archive/2007/11/18/161340.html
阅读(3484) | 评论(0) | 转发(1) |