linux 之xml 解析读取

原博文:https://www.cnblogs.com/catgatp/p/6505451.html

下载地址:ftp://xmlsoft.org/libxml2/

下载libxml2文件后,解压改文件,然后进入解压文件进行编译安装,编译安装命令如下;

./configure
make
make install

如果在编译时遇到python 库的缺失,用下面命令安装再编译即可:

sudo  yum -y install python-devel

编译安装完成后就可以看到对应库生成在 :  /usr/local/lib 

zdg@localhost xmldemo]$ cd /usr/local/lib
[zdg@localhost lib]$ 
[zdg@localhost lib]$ 
[zdg@localhost lib]$ ls
cmake       libhttpd.la  libhttpd.so.0      libxml2.a   libxml2.so    libxml2.so.2.9.8  xml2Conf.sh
libhttpd.a  libhttpd.so  libhttpd.so.0.0.0  libxml2.la  libxml2.so.2  pkgconfig
[zdg@localhost lib]$ 

头文件生成在: /usr/local/include/libxml2/

[zdg@localhost include]$ cd /usr/local/include/libxml2/
[zdg@localhost libxml2]$ ls
libxml

测试xml 文件

<?xml version="1.0" encoding="ISO-8859-1"?>
 
<bookstore>
 
<book>
<title lang="eng">Harry Potter</title>
<price>29.99</price>
</book>
 
<book>
<title lang="eng">Learning XML</title>
<price>39.95</price>
</book>
 
</bookstore>
 

源码:

直接使用libxml2接口解析XML文档


#include <stdio.h>
#include <stdlib.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
 
 
int main (int argc , char **argv)
{
xmlDocPtr pdoc = NULL;
xmlNodePtr proot = NULL, pcur = NULL;
/*****************打开xml文档********************/
xmlKeepBlanksDefault(0);//必须加上,防止程序把元素前后的空白文本符号当作一个node
pdoc = xmlReadFile ("test.xml", "UTF-8", XML_PARSE_RECOVER);//libxml只能解析UTF-8格式数据
 
if (pdoc == NULL)
{
printf ("error:can't open file!\n");
exit (1);
}
 
/*****************获取xml文档对象的根节对象********************/
proot = xmlDocGetRootElement (pdoc);
 
if (proot == NULL)
{
printf("error: file is empty!\n");
exit (1);
}
 
/*****************查找书店中所有书籍的名称********************/
pcur = proot->xmlChildrenNode;
 
while (pcur != NULL)
{
//如同标准C中的char类型一样,xmlChar也有动态内存分配,字符串操作等 相关函数。例如xmlMalloc是动态分配内存的函数;xmlFree是配套的释放内存函数;xmlStrcmp是字符串比较函数等。
//对于char* ch="book", xmlChar* xch=BAD_CAST(ch)或者xmlChar* xch=(const xmlChar *)(ch)
//对于xmlChar* xch=BAD_CAST("book"),char* ch=(char *)(xch)
if (!xmlStrcmp(pcur->name, BAD_CAST("book")))
{
xmlNodePtr nptr=pcur->xmlChildrenNode;
while (pcur != NULL)
{
if (!xmlStrcmp(nptr->name, BAD_CAST("title")))
{
printf("title: %s\n",((char*)XML_GET_CONTENT(nptr->xmlChildrenNode)));
break;
}
}
 
}
pcur = pcur->next;
}
 
/*****************释放资源********************/
xmlFreeDoc (pdoc);
xmlCleanupParser ();
xmlMemoryDump ();
return 0;
}

使用XPath语言解析XML文档

#include <stdio.h>
#include <stdlib.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
#include <libxml/xmlmemory.h>
#include <libxml/xpointer.h>
 
xmlXPathObjectPtr getNodeset(xmlDocPtr pdoc,const xmlChar *xpath)
{
xmlXPathContextPtr context=NULL;//XPath上下文指针
xmlXPathObjectPtr result=NULL; //XPath结果指针
context = xmlXPathNewContext(pdoc);
 
if(pdoc==NULL){
printf("pdoc is NULL\n");
return NULL;
}
 
if(xpath){
if (context == NULL) {
printf("context is NULL\n");
return NULL;
}
 
result = xmlXPathEvalExpression(xpath, context);
xmlXPathFreeContext(context); //释放上下文指针
if (result == NULL) {
printf("xmlXPathEvalExpression return NULL\n");
return NULL;
}
 
if (xmlXPathNodeSetIsEmpty(result->nodesetval)) {
xmlXPathFreeObject(result);
printf("nodeset is empty\n");
return NULL;
}
}
 
return result;
}
 
int main (int argc , char **argv){
xmlDocPtr pdoc = NULL;
xmlNodePtr proot = NULL;
 
/*****************打开xml文档********************/
xmlKeepBlanksDefault(0);//必须加上,防止程序把元素前后的空白文本符号当作一个node
pdoc = xmlReadFile ("test.xml", "UTF-8", XML_PARSE_RECOVER);//libxml只能解析UTF-8格式数据
 
if (pdoc == NULL)
{
printf ("error:can't open file!\n");
exit (1);
}
 
/*****************获取xml文档对象的根节对象********************/
proot = xmlDocGetRootElement (pdoc);
 
if (proot == NULL)
{
printf("error: file is empty!\n");
exit (1);
}
 
/*****************查找书店中所有书籍的名称********************/
xmlChar *xpath = BAD_CAST("//book"); //xpath语句
xmlXPathObjectPtr result = getNodeset(pdoc, xpath); //查询XPath表达式,得到一个查询结果
if (result == NULL)
{
printf("result is NULL\n");
exit (1);
}
 
if(result)
{
xmlNodeSetPtr nodeset = result->nodesetval; //获取查询到的节点指针集合
xmlNodePtr cur;
 
//nodeset->nodeNr是集合元素总数
for (int i=0; i < nodeset->nodeNr; i++)
{
cur = nodeset->nodeTab[i];
cur = cur->xmlChildrenNode;
 
while (cur != NULL)
{
//如同标准C中的char类型一样,xmlChar也有动态内存分配,字符串操作等 相关函数。例如xmlMalloc是动态分配内存的函数;xmlFree是配套的释放内存函数;xmlStrcmp是字符串比较函数等。
//对于char* ch="book", xmlChar* xch=BAD_CAST(ch)或者xmlChar* xch=(const xmlChar *)(ch)
//对于xmlChar* xch=BAD_CAST("book"),char* ch=(char *)(xch)
if (!xmlStrcmp(cur->name, BAD_CAST("title"))) {
printf("title: %s\n",((char*)XML_GET_CONTENT(cur->xmlChildrenNode)));
break;
}
 
cur = cur->next;
}
}
 
xmlXPathFreeObject(result);//释放结果指针
}
 
/*****************释放资源********************/
xmlFreeDoc (pdoc);
xmlCleanupParser ();
xmlMemoryDump ();
 
return 0;
}

在我demo 里文件和编译命令如下:

测试文件:

[zdg@localhost xmldemo]$ ls
test.xml  xml.c
[zdg@localhost xmldemo]$ 

gcc -o xml xml.c  -I /usr/local/include/libxml2/ -L /usr/local/lib -lxml2

执行程序结果:./xml

zdg@localhost xmldemo]$ 
[zdg@localhost xmldemo]$ ./xml
title:Harry Potter
title:Learning XML
title:save youself
[zdg@localhost xmldemo]$ 


  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值