libxml2 生成、解析xml

由于项目的需要,前一段时间负责找一个用c or cpp语言实现的xml parser,由于还要加上开源的条件,确实选择不是很多。因为目前开源的东西还是java的比较多阿!比较apache的xerces和libxml2后决定选择libxml2,下面将对使用Libxml2一个多月来的感受作一简介吧。个人意见仅供大家参考。
         首先说说Libxml2吧,Libxml2 是一个xml c语言版的解析器,本来是为Gnome项目开发的工具,是一个基于MIT License的免费开源软件。它除了支持c语言版以外,还支持c++、PHP、Pascal、Ruby、Tcl等语言的绑定,能在Windows、Linux、Solaris、MacOsX等平台上运行。功能还是相当强大的,相信满足一般用户需求没有任何问题。
具体的介绍就参考  http://xmlsoft.org/
          Libxml2主要的优点有:1.    安装、使用比较简单,容易入门;2.    支持的编码格式较多,能很好的解决中文问题(使用一个很简单的编码转换函数);3.    支持Xpath解析(这点对于任意定位xml文档中的节点还是很有用的哦);4.支持Well-formed 和valid验证,具体而言支持DTD验证,Schema验证功能正在完善中(目前多数解析器都还不完全支持shema验证功能);5.    支持目前通用的Dom、Sax方式解析等等。
     
         不足之处也是有的:1.   指针太多,使用不当时就会出现错误,在Linux系统中表现为常见的段错误,同样管理不当易造成内存泄漏;2.个人认为内面有些函数的功能设计的不是很好(比如获取Xpath函数,它不获取节点属性,这样子有些情况会定位不准)
    Libxml2的在linux下使用
           安装
                      1)       从xmlsoft站点或ftp(ftp.xmlsoft.org)站点下载libxml压缩包(libxml2-xxxx.tar.gz)
                      2)       对压缩包进行解压缩
                                  tar xvzf libxml2-xxxx.tar.gz
                       3)       进入解压缩后的文件夹中运行
                                 (1)
                                            ./configure --prefix  /home/user/myxml/xmlinst(此处为待安装的路径)
                                  (2)
                                             make
                                  (3)
                                              make install
                      4)       添加路径
                export PATH= /home/user/myxml/xmlinst/bin:$PATH
                        说明:为了结构清晰,最好将libxml2不安装在解压目录中。
                 使用Libxml2生成xml
                        #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);
                     }
                      编译运行
                             
               g++ xmlCreator.cpp -o xmlCreator -I /home/xb/libxml2/xmlinst/include/libxml2/   
          -L /home/xb/libxml2/xmlinst/lib/ -lxml2         绿色文字为 libxml2 安装路径
     生成的xml
               <?xml version="1.0" encoding="UTF-8"?>
                          <root>
                                     <node1>content of node 1</node1>
                                     <node3 attribute="yes">node has attributes</node3>
                                     <node4>other way to create content</node4>
                           </root>
              呵呵,现就这些吧,好像很简单,算是入门级。
体验新版博客

你可能感兴趣的:(libxml2)