libxml2使用指南
来源:互联网 发布:python有指针吗 编辑:程序博客网 时间:2024/06/08 10:10
一、Libxml2介绍:
Libxml2 是一个xml的c语言版的解析器,本来是为Gnome项目开发的工具,是一个基于MIT License的免费开源软件。它除了支持c语言版以外,还支持c++、PHP、Pascal、Ruby、Tcl等语言的绑定,能在Windows、Linux、Solaris、MacOsX等平台上运行。功能还是相当强大的,相信满足一般用户需求没有任何问题。
二、Libxml2安装:
一般如果在安装系统的时候选中了所有开发库和开发工具的话(Fedora Core系列下),应该不用安装,下面介绍一下手动安装:
1) 从xmlsoft站点或ftp(ftp.xmlsoft.org)站点下载libxml压缩包(libxml2-xxxx.tar.gz)
2) 对压缩包进行解压缩:tar xvzf libxml2-xxxx.tar.gz
3) 进入解压缩后的文件夹中运行:A)./configure B)make C)make install
4)编译:$gcc test.c -o test -I /usr/local/include/libxml2/ -L /usr/local/lib -lxml2
安装完成后就可以使用简单的代码解析XML文件,包括本地和远程的文件,但是在编码上有一些问题。Libxml默认只支持 UTF-8的编码,无论输入输出都是UTF-8,所以如果你解析完一个XML得到的结果都是UTF-8的,如果需要输出GB2312或者其它编码,需要 ICONV来做转码(生成UTF-8编码的文件也可以用它做),如果系统中没有安装iconv的话,需要安装libiconv。
1) 下载libiconv压缩包(例如libiconv-1.11.tar.gz)
2) 对压缩包进行解压缩:tar xvzf libiconv-1.11.tar.gz
3) 进入解压缩后的文件夹中运行:A)./configure B)make C)make install
三、关于XML:
在开始研究 Libxml2库之前,先了解一下XML的相关基础。XML是一种基于文本的格式,它可用来创建能够通过各种语言和平台访问的结构化数据。它包括一系列类似 HTML的标记,并以树型结构来对这些标记进行排列。
例如,可参见清单 1中介绍的简单文档。为了更清楚地显示 XML的一般概念,下面是一个简化的XML文件。
清单 1. 一个简单的 XML 文件
<?xml version="1.0" encoding="UTF-8"?>
<files>
<owner>root</owner>
<action>delete</action>
<age units="days">10</age>
</files>
清单 1 中的第一行是 XML 声明,它告诉负责处理 XML的应用程序,即解析器,将要处理的 XML的版本。大部分的文件使用版本 1.0编写,但也有少量的版本 1.1的文件。它还定义了所使用的编码。大部分文件使用 UTF-8,但是,XML设计用来集成各种语言中的数据,包括那些不使用英语字母的语言。
接下来出现的是元素。一个元素以开始标记开始(如 <files>),并以结束标记结束(如 </files>),其中使用斜线 (/)来区别于开始标记。元素是 Node的一种类型。XML 文档对象模型 (DOM) 定义了几种不同的 Nodes类型,包括:
Elements(如 files或者 age)
Attributes(如 units)
Text(如 root或者 10)
元素可以具有子节点。例如,age元素有一个子元素,即文本节点 10。
XML 解析器可以利用这种父子结构来遍历文档,甚至修改文档的结构或内容。LibXML2是这样的解析器中的其中一种,并且文中的示例应用程序正是使用这种结构来实现该目的。对于各种不同的环境,有许多不同的解析器和库。LibXML2是用于 UNIX 环境的解析器和库中最好的一种,并且经过扩展,它提供了对几种脚本语言的支持,如 Perl和 Python。
三、使用Libxml2
项目中要实现一个管理XML文件的后台程序,需要对XML文件进行创建,解析,修改,查找等操作,下面介绍如何利用libxml2提供的库来实现上述功能。
1、创建XML文档:
我们使用xmlNewDoc()来创建XML文档,然后使用
xmlNewNode(),xmlNewChild(),xmlNewProp(),xmlNewText()等函数向XML文件中添加节点及子节点,设置元素和属性,创建完毕后用xmlSaveFormatFileEnc()来保存XML文件到磁盘(该函数可以设置保存XML文件时的编码格式)。
示例1:
#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
int main(int argc, char **argv)
{
xmlDocPtr doc = NULL;
xmlNodePtr root_node = NULL, node = NULL, node1 = NULL;
// 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);
xmlFreeDoc(doc);
xmlCleanupParser();
xmlMemoryDump();//debug memory for regression tests
return(0);
}
2、解析XML文档
解析文档时仅仅需要文件名并只调用一个函数,并有错误检查,常用的相关函数有 xmlParseFile(),xmlParseDoc(),获取文档指针后,就可以使用xmlDocGetRootElement()来获取根元素节点指针,利用该指针就可以在DOM树里漫游了,结束后要调用xmlFreeDoc()释放。
例2:
xmlDocPtr doc; //定义解析文档指针
xmlNodePtr cur; //定义结点指针(你需要它为了在各个结点间移动)
xmlChar *key;
doc = xmlReadFile(url, MY_ENCODING, 256); //解析文件
if (doc == NULL ) {
fprintf(stderr,"Document not parsed successfully. \n");
return;
}
cur = xmlDocGetRootElement(doc); //确定文档根元素
if (cur == NULL) {
fprintf(stderr,"empty document\n");
xmlFreeDoc(doc);
return;
}
if (xmlStrcmp(cur->name, (const xmlChar *) "root")) {
fprintf(stderr,"document of the wrong type, root node != root");
xmlFreeDoc(doc);
return;
}
cur = cur->xmlChildrenNode;
while(cur!=NULL) {
if ((!xmlStrcmp(cur->name, (const xmlChar *)"keyword"))) {
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
printf("keyword: %s\n", key);
xmlFree(key);
}
cur = cur->next;
}
xmlFreeDoc(doc);
3、修改XML元素及属性等信息
要修改XML文档里的元素及属性等信息,先需要解析XML文档,获得一个节点指针(xmlNodePtr node),利用该节点指针漫游DOM树,就可以在XML文档中获取,修改,添加相关信息。
示例3:
得到一个节点的内容:
xmlChar *value = xmlNodeGetContent(node);
返回值value应该使用xmlFree(value)释放内存
得到一个节点的某属性值:
xmlChar *value = xmlGetProp(node, (const xmlChar *)"prop1");
返回值需要xmlFree(value)释放内存
设置一个节点的内容:
xmlNodeSetContent(node, (const xmlChar *)"test");
设置一个节点的某属性值:
xmlSetProp(node, (const xmlChar *)"prop1", (const xmlChar *)"v1");
添加一个节点元素:
xmlNewTextChild(node, NULL, (const xmlChar *)"keyword", (const xmlChar *)"test Element");
添加一个节点属性:
xmlNewProp(node, (const xmlChar *)"prop1", (const xmlChar *)"test Prop");
4、查找XML节点
有时候对一个XML文档我们可能只关心其中某一个或某几个特定的Element的值或其属性,如果漫游DOM树将是很痛苦也很无聊的事,利用XPath可以非常方便地得到你想的Element。下面是一个自定义函数:
示例4:
xmlXPathObjectPtr get_nodeset(xmlDocPtr doc, const xmlChar *xpath) {
xmlXPathContextPtr context;
xmlXPathObjectPtr result;
context = xmlXPathNewContext(doc);
if (context == NULL) {
printf("context is NULL\n");
return NULL;
}
result = xmlXPathexpression_r(xpath, context);
xmlXPathFreeContext(context);
if (result == NULL) {
printf("xmlXPathExpression return NULL\n");
return NULL;
}
if (xmlXPathNodeSetIsEmpty(result->nodesetval)) {
xmlXPathFreeObject(result);
printf("nodeset is empty\n");
return NULL;
}
return result;
}
在doc指向的XML文档中查询满足xpath表达式条件的节点,返回满足这一条件的节点集合查询条件xpath的写法参见xpath相关资料。在查询完毕获取结果集后,就可以通过返回的 xmlXPathObjectPtr 结构访问该节点:
示例5:
xmlChar *xpath = ("/root/node/[@key='keyword']");
xmlXPathObjectPtr app_result = get_nodeset(doc,xpath);
if (app_result == NULL) {
printf("app_result is NULL\n");
return;
}
int i = 0;
xmlChar *value;
if(app_result) {
xmlNodeSetPtr nodeset = app_result->nodesetval;
for (i=0; i < nodeset->nodeNr; i++) {
cur = nodeset->nodeTab[i];
cur = cur->xmlChildrenNode;
while(cur!=NULL) {
value = xmlGetProp(cur,(const xmlChar *)"key");
if (value != NULL) {
printf("value: %s\n\n", d_ConvertCharset("utf-8", "GBK", (char *)value));
xmlFree(value);
}
value = xmlNodeGetContent(cur);
if (value != NULL) {
printf("value: %s\n\n", d_ConvertCharset("utf-8", "GBK", (char *)value));
xmlFree(value);
}}}
xmlXPathFreeObject (app_result);
}
通过get_nodeset()返回的结果集,我们可以获取该节点的元素及属性,也可以修改该节点的值。示例中在获取值打印的时候用到 d_ConvertCharset()函数来改变编码格式为GBK,以方便正确读取可能的中文字符。
5、编码问题
由于Libxml一般以UTF-8格式保存和操纵数据,如果你的程序使用其它的数据格式,比如中文字符(GB2312,GBK编码),就必须使用Libxml函数转换到UTF-8。如果你想你的程序以除UTF-8外的其它编码方式输出也必须做转换。
下面的示例程序提供几个函数来实现对数据编码格式的转换,其中有的要用到Libiconv,因此为了确保他们能正常工作,先检查以下系统中是否已经安装libiconv库。
示例6:
xmlChar *ConvertInput(const char *in, const char *encoding) {
unsigned char *out;
int ret;
int size;
int out_size;
int temp;
xmlCharEncodingHandlerPtr handler;
if (in == 0)
return 0;
handler = xmlFindCharEncodingHandler(encoding);
if (!handler) {
printf("ConvertInput: no encoding handler found for '%s'\n", encoding ? encoding : "");
return 0;
}
size = (int) strlen(in) + 1;
out_size = size * 2 - 1;
out = (unsigned char *) xmlMalloc((size_t) out_size);
if (out != 0) {
temp = size - 1;
ret = handler->input(out, &out_size, (const unsigned char *) in, &temp);
if ((ret < 0) || (temp - size + 1)) {
if (ret < 0) {
printf("ConvertInput: conversion wasn't successful.\n");
} else {
printf("ConvertInput:conversion wasn't successful. converted: %i octets.\n", temp);
}
xmlFree(out);
out = 0;
} else {
out = (unsigned char *) xmlRealloc(out, out_size + 1);
out[out_size] = 0;
}
} else {printf("ConvertInput: no mem\n");}
return out;
}
示例7:
char * Convert( char *encFrom, char *encTo, const char * in) {
static char bufin[1024], bufout[1024], *sin, *sout;
int mode, lenin, lenout, ret, nline;
iconv_t c_pt;
if ((c_pt = iconv_open(encTo, encFrom)) == (iconv_t)-1) {
printf("iconv_open false: %s ==> %s\n", encFrom, encTo);
return NULL;
}
iconv(c_pt, NULL, NULL, NULL, NULL);
lenin = strlen(in) + 1;
lenout = 1024;
sin = (char *)in;
sout = bufout;
ret = iconv(c_pt, &sin, (size_t *)&lenin, &sout, (size_t *)&lenout);
if (ret == -1) {
return NULL;
}
iconv_close(c_pt);
return bufout;
}
示例8:
char *d_ConvertCharset(char *cpEncodeFrom, char *cpEncodeTo, const char *cpInput) {
static char s_strBufOut[1024], *sin, *cpOut;
size_t iInputLen, iOutLen, iReturn;
iconv_t c_pt;
if ((c_pt = iconv_open(cpEncodeTo, cpEncodeFrom)) == (iconv_t)-1) {
printf("iconv_open failed!\n");
return NULL;
}
iconv(c_pt, NULL, NULL, NULL, NULL);
iInputLen = strlen(cpInput) + 1;
iOutLen = 1024;
sin = (char *)cpInput;
cpOut = s_strBufOut;
iReturn = iconv(c_pt, &sin, &iInputLen, &cpOut, &iOutLen);
if (iReturn == -1) {
return NULL;
}
iconv_close(c_pt);
return s_strBufOut;
}
<?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</nod4>
</root>)
遍历程序代码
#include <iostream>
#include <libxml/parser.h>
#include <libxml/tree.h>
using namespace std;
int main(int argc,char** argv)
{
xmlDocPtr doc=NULL;
xmlNodePtr cur=NULL;
char* name=NULL;
char* value=NULL;
xmlKeepBlanksDefault (0);
if(argc<2)
{
cout<<"argc must be 2 or above."<<endl;
return -1;
}
doc=xmlParseFile(argv[1]);//创建Dom树
if(doc==NULL)
{
cout<<"Loading xml file failed."<<endl;
exit(1);
}
cur=xmlDocGetRootElement(doc);//获取根节点
if(cur==NULL)
{
cout<<"empty file"<<endl;
xmlFreeDoc(doc);
exit(2);
}
//walk the tree
cur=cur->xmlChildrenNode;//get sub node
while(cur !=NULL)
{
name=(char*)(cur->name);
value=(char*)xmlNodeGetContent(cur);
cout<<"name is: "<<name<<", value is: "<<value<<endl;
xmlFree(value);
cur=cur->next;
}
xmlFreeDoc(doc);//释放xml解析库所用资源
xmlCleanupParser();
return 0;
}
说明:
1. 当使用dom树来解析xml文档时,由于默认的方式是把节点间的空白当作第一个子节点,所以
为了能和常说的第一个子节点相符,需调用xmlKeepBlanksDefault (0)函数来忽略这种空白。
2. 对于使用xmlChar* xmlNodeGetContent(xmlNodePtr cur)函数获取节点内容后,必须调用xmlFree()来对所分配的内存进行释放。
使用Xpath获取特定结点的内容(使用的xml文档见上面):
#include <iostream>
#include <string>
using namespace std;
#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
int main(int argc,char** argv)
{
xmlDocPtr doc;
xmlXPathContextPtr xpathCtx;
xmlXPathObjectPtr xpathObj;
xmlNodeSetPtr nodeset;
string xpathExpr;
char* val=NULL;
int size,i;
if(argc<2)
{
cout<<"argc must be 2 or above."<<endl;
return -1;
}
/* Load XML document */
doc = xmlParseFile(argv[1]);
if (doc == NULL)
{
cout<<"Error: unable to parse file: "<<argv[1]<<endl;
return -1;
}
/* Create xpath evaluation context */
xpathCtx = xmlXPathNewContext(doc);
if(xpathCtx == NULL)
{
cout<<"Error: unable to create new XPath context"<<endl;
xmlXPathFreeContext(xpathCtx);
xmlFreeDoc(doc);
return -2;
}
xpathExpr="/root/node3";
/* Evaluate xpath expression */
xpathObj = xmlXPathEvalExpression((const xmlChar*)(xpathExpr.c_str()), xpathCtx);
if(xpathObj == NULL)
{
cout<<"Error: unable to evaluate xpath expression"<<xpathExpr<<endl;
xmlXPathFreeContext(xpathCtx);
xmlFreeDoc(doc);
return -3;
}
/* get values of the selected nodes */
nodeset=xpathObj->nodesetval;
if(xmlXPathNodeSetIsEmpty(nodeset))
{
cout<<"No such nodes."<<endl;
xmlXPathFreeObject(xpathObj);
xmlXPathFreeContext(xpathCtx);
xmlFreeDoc(doc);
return -4;
}
//get the value
size = (nodeset) ? nodeset->nodeNr : 0;
for(i = 0; i <size; i++)
{
val=(char*)xmlNodeListGetString(doc,nodeset->nodeTab[i]->xmlChildrenNode,1);
cout<<"the results are: "<<val<<endl;
xmlFree(val);
}
//Cleanup of XPath data
xmlXPathFreeObject(xpathObj);
xmlXPathFreeContext(xpathCtx);
/* free the document */
xmlFreeDoc(doc);
xmlCleanupParser();
return 0;
}
- Libxml2使用指南
- Libxml2使用指南
- libxml2使用指南
- Libxml2使用指南
- LIBXML2库使用指南
- LIBXML2库使用指南2
- LIBXML2库使用指南
- C++ LIBXML2库使用指南
- LIBXML2库使用指南
- LIBXML2库使用指南
- LIBXML2库使用指南
- LIBXML2库使用指南
- libxml2 中文使用指南
- LIBXML2库使用指南2
- LIBXML2库使用指南2
- LIBXML2库windows使用指南
- XML---(2)LIBXML2库Linux使用指南
- C++的XML编程经验――LIBXML2库使用指南
- Substring spoj8222
- 时间轴控件
- Android中多线程下载
- 简单经典算法笔记(未完)
- 互联网教育:玩法与困境
- libxml2使用指南
- PHP数组根据值获取Key
- MFC文件操作
- 项目经理的管理感悟
- windows实战Git环境配备msysGit+TortoiseGit
- MFC程序编译成功后,运行还要编译一遍
- mysql 上的小技巧
- IIS6和IIS7做301永久重定向(附图)
- linux下 MYSQL安装运行的简单方法