Chinaunix首页 | 论坛 | 博客
  • 博客访问: 463833
  • 博文数量: 65
  • 博客积分: 573
  • 博客等级: 中士
  • 技术积分: 693
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-09 17:16
文章分类

全部博文(65)

文章存档

2015年(12)

2014年(9)

2013年(22)

2012年(7)

2011年(15)

分类: LINUX

2013-09-26 00:16:42

参考文章地址: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
  1. /*  create_xml.c */

  2. #include <stdio.h>
  3. #include <libxml/parser.h>
  4. #include <libxml/tree.h>

  5. int main(int argc, char **argv)
  6. {
  7.     xmlDocPtr doc = NULL; /* document pointer */
  8.     xmlNodePtr root_node = NULL, node = NULL, node1 = NULL;/* node pointers */

  9.     // Creates a new document, a node and set it as a root node
  10.     doc = xmlNewDoc(BAD_CAST "1.0");
  11.     root_node = xmlNewNode(NULL, BAD_CAST "root");
  12.     xmlDocSetRootElement(doc, root_node);

  13.     //creates a new node, which is "attached" as child node of root_node node.
  14.     xmlNewChild(root_node, NULL, BAD_CAST "node1",BAD_CAST "content of node1");

  15.     // xmlNewProp() creates attributes, which is "attached" to an node.
  16.     node=xmlNewChild(root_node, NULL, BAD_CAST "node3", BAD_CAST"node has attributes");
  17.     xmlNewProp(node, BAD_CAST "attribute", BAD_CAST "yes");

  18.     //Here goes another way to create nodes.
  19.     node = xmlNewNode(NULL, BAD_CAST "node4");
  20.     node1 = xmlNewText(BAD_CAST"other way to create content");
  21.     xmlAddChild(node, node1);
  22.     xmlAddChild(root_node, node);

  23.     //Dumping document to stdio or file
  24.     xmlSaveFormatFileEnc(argc > 1 ? argv[1] : "-", doc, "UTF-8", 1);

  25.     /*free the document */
  26.     xmlFreeDoc(doc);
  27.     xmlCleanupParser();
  28.     xmlMemoryDump();//debug memory for regression tests

  29.     return 0;
  30. }
运行./creat_xml   hello.xml , hello.xml文件生成的结果如下:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <root>
  3.   <node1>content of node1</node1>
  4.   <node3 attribute="yes">node has attributes</node3>
  5.   <node4>other way to create content</node4>
  6. </root>

下面给出解析的函数  parse_xml.c  ,编译方法如上
  1. #include <stdio.h>
  2. #include <libxml/parser.h>
  3. #include <libxml/tree.h>

  4. int main(int argc, char** argv)
  5. {
  6.     xmlDocPtr doc=NULL;
  7.     xmlNodePtr cur=NULL;
  8.     char* name=NULL;
  9.     char* value=NULL;

  10.     xmlKeepBlanksDefault (0);

  11.     if(argc<2)
  12.     {
  13.      printf("ERROR: argc must be 2 or above.\n");
  14.      return -1;
  15.     }

  16.     //create Dom tree
  17.     doc=xmlParseFile(argv[1]);
  18.     if(doc==NULL)
  19.     {
  20.      printf("ERROR: Loading xml file failed.\n");
  21.      exit(1);
  22.     }

  23.     // get root node
  24.     cur=xmlDocGetRootElement(doc);
  25.     if(cur==NULL)
  26.     {
  27.      printf("ERROR: empty file\n");
  28.      xmlFreeDoc(doc);
  29.      exit(2);
  30.     }

  31.     //walk the tree
  32.     cur=cur->xmlChildrenNode; //get sub node
  33.     while(cur !=NULL)
  34.     {
  35.      name=(char*)(cur->name);
  36.      value=(char*)xmlNodeGetContent(cur);
  37.      printf("DEBUG: name is: %s, value is: %s\n", name, value);
  38.      xmlFree(value);
  39.      xmlChar* szAttr = xmlGetProp(cur,BAD_CAST "attribute");
  40.      printf("DEBUG: attribute is: %s\n", (char *)szAttr );
  41.      xmlFree(szAttr);
  42.      cur=cur->next;
  43.     }

  44.     // release resource of xml parser in libxml2
  45.     xmlFreeDoc(doc);
  46.     xmlCleanupParser();

  47.     return 0;
  48. }
修改xml文件change_xml.c
  1. #include <stdio.h>
  2. #include <libxml/parser.h>
  3. #include <libxml/tree.h>

  4. int main(int argc, char** argv)
  5. {
  6.     xmlDocPtr doc=NULL;
  7.     xmlNodePtr cur=NULL;
  8.     xmlNodePtr ptr=NULL;
  9.     char* name=NULL;
  10.     char* value=NULL;

  11.     xmlKeepBlanksDefault (0); //去除空行

  12.     if(argc<2)
  13.     {
  14.      printf("ERROR: argc must be 2 or above.\n");
  15.      return -1;
  16.     }

  17.     //create Dom tree
  18.     doc=xmlParseFile(argv[1]);
  19.     if(doc==NULL)
  20.     {
  21.      printf("ERROR: Loading xml file failed.\n");
  22.      exit(1);
  23.     }

  24.     // get root node
  25.     cur=xmlDocGetRootElement(doc);
  26.     if(cur==NULL)
  27.     {
  28.      printf("ERROR: empty file\n");
  29.      xmlFreeDoc(doc);
  30.      exit(2);
  31.     }

  32.     //walk the tree
  33.     cur=cur->xmlChildrenNode; //get sub node
  34.     ptr=cur->next;
  35.     xmlUnlinkNode(cur);
  36.     xmlFreeNode(cur);
  37.     cur=ptr;
  38.     
  39.     cur=cur->next;
  40.     //修改节点的属性值
  41.     xmlSetProp(cur,BAD_CAST "attribute", BAD_CAST "no");
  42.   
  43.     //修改节点的内容
  44.     xmlNodeSetContent(cur, BAD_CAST "content changed");
  45.   

  46.     xmlSaveFormatFileEnc(argc > 1 ? argv[1] : "-", doc, "UTF-8", 1);
  47.         
  48.     // release resource of xml parser in libxml2
  49.     xmlFreeDoc(doc);
  50.     xmlCleanupParser();

  51.     return 0;
  52. }
编译之后运行 ./change_xml  hello.xml之后,hello.xml的内容变为
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <root>
  3.   <node3 attribute="yes">node has attributes</node3>
  4.   <node4 attribute="no">content changed</node4>
  5. </root>


下面两篇写的很详细的博文贴出:lixbxml2库使用指南
     http://blog.sina.com.cn/s/blog_6a1837e90100ns2q.html
     http://www.blogjava.net/wxb_nudt/archive/2007/11/18/161340.html
  



阅读(3419) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~