使用libxml2进行xml开发(一)
(一)Windows下使用MinGW和Code::Blocks环境配置libxml2
笔者此次是在windows 7下使用MinGW和Code::Blocks开发C程式的,手上的一个项目需要使用socket通讯接收远端主机发来的xml报文,使用C程式解析,所以需要配置libxml2。
首先先到http://xmlsoft.org/sources/win32/下载好libxml2、iconv和zlib的包,并将其对应的bin、include、lib中的内容复制到MinGW对应的bin、include、lib目录中去(libxml2是基于iconv和zlib的
在CB中建立好一个工程后,在settings->compiler->linker settings中添加上述包中的lib(已经添加到MinGW\lib下了),我们可以用libxml2官方的一个例子来测试我们的配置:
将gjobs.xml放在工程文件夹中,将其设置为输入 Project->set programs' arguments
笔者在编译成功后,运行的时候遇到过如下的一个错误
“无法定位程序输入点gzdirect于动态链接库zlib1.dll”
解决办法是使用http://www.zlib.net/下载的最新的zlib1.dll替换WINDOWS/system32文件夹下的zlib1.dll (window下优先使用system32下的这个dll,而在安装某些软件时会将它们的zlib1.dll放到system32中,造成版本过低)
运行结果:
附gjobread.c和gjobs.xml源码
/*
* gjobread.c : a small test program for gnome jobs XML format
*
* See Copyright for the status of this software.
*
* Daniel.Veillard@w3.org
*/ #include <stdio.h>
#include <string.h>
#include <stdlib.h> /*
* This example should compile and run indifferently with libxml-1.8.8 +
* and libxml2-2.1.0 +
* Check the COMPAT comments below
*/ /*
* COMPAT using xml-config --cflags to get the include path this will
* work with both
*/
#include <libxml/xmlmemory.h>
#include <libxml/parser.h> #define DEBUG(x) printf(x) /*
* A person record
* an xmlChar * is really an UTF8 encoded char string (0 terminated)
*/
typedef struct person {
xmlChar *name;
xmlChar *email;
xmlChar *company;
xmlChar *organisation;
xmlChar *smail;
xmlChar *webPage;
xmlChar *phone;
} person, *personPtr; /*
* And the code needed to parse it
*/
static personPtr
parsePerson(xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur) {
personPtr ret = NULL; DEBUG("parsePerson\n");
/*
* allocate the struct
*/
ret = (personPtr) malloc(sizeof(person));
if (ret == NULL) {
fprintf(stderr,"out of memory\n");
return(NULL);
}
memset(ret, , sizeof(person)); /* We don't care what the top level element name is */
/* COMPAT xmlChildrenNode is a macro unifying libxml1 and libxml2 names */
cur = cur->xmlChildrenNode;
while (cur != NULL) {
if ((!xmlStrcmp(cur->name, (const xmlChar *)"Person")) &&
(cur->ns == ns))
ret->name = xmlNodeListGetString(doc, cur->xmlChildrenNode, );
if ((!xmlStrcmp(cur->name, (const xmlChar *)"Email")) &&
(cur->ns == ns))
ret->email = xmlNodeListGetString(doc, cur->xmlChildrenNode, );
cur = cur->next;
} return(ret);
} /*
* and to print it
*/
static void
printPerson(personPtr cur) {
if (cur == NULL) return;
printf("------ Person\n");
if (cur->name) printf(" name: %s\n", cur->name);
if (cur->email) printf(" email: %s\n", cur->email);
if (cur->company) printf(" company: %s\n", cur->company);
if (cur->organisation) printf(" organisation: %s\n", cur->organisation);
if (cur->smail) printf(" smail: %s\n", cur->smail);
if (cur->webPage) printf(" Web: %s\n", cur->webPage);
if (cur->phone) printf(" phone: %s\n", cur->phone);
printf("------\n");
} /*
* a Description for a Job
*/
typedef struct job {
xmlChar *projectID;
xmlChar *application;
xmlChar *category;
personPtr contact;
int nbDevelopers;
personPtr developers[]; /* using dynamic alloc is left as an exercise */
} job, *jobPtr; /*
* And the code needed to parse it
*/
static jobPtr
parseJob(xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur) {
jobPtr ret = NULL; DEBUG("parseJob\n");
/*
* allocate the struct
*/
ret = (jobPtr) malloc(sizeof(job));
if (ret == NULL) {
fprintf(stderr,"out of memory\n");
return(NULL);
}
memset(ret, , sizeof(job)); /* We don't care what the top level element name is */
cur = cur->xmlChildrenNode;
while (cur != NULL) { if ((!xmlStrcmp(cur->name, (const xmlChar *) "Project")) &&
(cur->ns == ns)) {
ret->projectID = xmlGetProp(cur, (const xmlChar *) "ID");
if (ret->projectID == NULL) {
fprintf(stderr, "Project has no ID\n");
}
}
if ((!xmlStrcmp(cur->name, (const xmlChar *) "Application")) &&
(cur->ns == ns))
ret->application =
xmlNodeListGetString(doc, cur->xmlChildrenNode, );
if ((!xmlStrcmp(cur->name, (const xmlChar *) "Category")) &&
(cur->ns == ns))
ret->category =
xmlNodeListGetString(doc, cur->xmlChildrenNode, );
if ((!xmlStrcmp(cur->name, (const xmlChar *) "Contact")) &&
(cur->ns == ns))
ret->contact = parsePerson(doc, ns, cur);
cur = cur->next;
} return(ret);
} /*
* and to print it
*/
static void
printJob(jobPtr cur) {
int i; if (cur == NULL) return;
printf("======= Job\n");
if (cur->projectID != NULL) printf("projectID: %s\n", cur->projectID);
if (cur->application != NULL) printf("application: %s\n", cur->application);
if (cur->category != NULL) printf("category: %s\n", cur->category);
if (cur->contact != NULL) printPerson(cur->contact);
printf("%d developers\n", cur->nbDevelopers); for (i = ;i < cur->nbDevelopers;i++) printPerson(cur->developers[i]);
printf("======= \n");
} /*
* A pool of Gnome Jobs
*/
typedef struct gjob {
int nbJobs;
jobPtr jobs[]; /* using dynamic alloc is left as an exercise */
} gJob, *gJobPtr; static gJobPtr
parseGjobFile(char *filename) {
xmlDocPtr doc;
gJobPtr ret;
jobPtr curjob;
xmlNsPtr ns;
xmlNodePtr cur; /*
* build an XML tree from a the file;
*/
doc = xmlParseFile(filename);
if (doc == NULL) return(NULL); /*
* Check the document is of the right kind
*/ cur = xmlDocGetRootElement(doc);
if (cur == NULL) {
fprintf(stderr,"empty document\n");
xmlFreeDoc(doc);
return(NULL);
}
ns = xmlSearchNsByHref(doc, cur,
(const xmlChar *) "http://www.gnome.org/some-location");
if (ns == NULL) {
fprintf(stderr,
"document of the wrong type, GJob Namespace not found\n");
xmlFreeDoc(doc);
return(NULL);
}
if (xmlStrcmp(cur->name, (const xmlChar *) "Helping")) {
fprintf(stderr,"document of the wrong type, root node != Helping");
xmlFreeDoc(doc);
return(NULL);
} /*
* Allocate the structure to be returned.
*/
ret = (gJobPtr) malloc(sizeof(gJob));
if (ret == NULL) {
fprintf(stderr,"out of memory\n");
xmlFreeDoc(doc);
return(NULL);
}
memset(ret, , sizeof(gJob)); /*
* Now, walk the tree.
*/
/* First level we expect just Jobs */
cur = cur->xmlChildrenNode;
while ( cur && xmlIsBlankNode ( cur ) )
{
cur = cur -> next;
}
if ( cur == )
return ( NULL );
if ((xmlStrcmp(cur->name, (const xmlChar *) "Jobs")) || (cur->ns != ns)) {
fprintf(stderr,"document of the wrong type, was '%s', Jobs expected",
cur->name);
fprintf(stderr,"xmlDocDump follows\n");
xmlDocDump ( stderr, doc );
fprintf(stderr,"xmlDocDump finished\n");
xmlFreeDoc(doc);
free(ret);
return(NULL);
} /* Second level is a list of Job, but be laxist */
cur = cur->xmlChildrenNode;
while (cur != NULL) {
if ((!xmlStrcmp(cur->name, (const xmlChar *) "Job")) &&
(cur->ns == ns)) {
curjob = parseJob(doc, ns, cur);
if (curjob != NULL)
ret->jobs[ret->nbJobs++] = curjob;
if (ret->nbJobs >= ) break;
}
cur = cur->next;
} return(ret);
} static void
handleGjob(gJobPtr cur) {
int i; /*
* Do whatever you want and free the structure.
*/
printf("%d Jobs registered\n", cur->nbJobs);
for (i = ; i < cur->nbJobs; i++) printJob(cur->jobs[i]);
} int main(int argc, char **argv) {
int i;
gJobPtr cur; /* COMPAT: Do not genrate nodes for formatting spaces */
LIBXML_TEST_VERSION
xmlKeepBlanksDefault(); for (i = ; i < argc ; i++) {
cur = parseGjobFile(argv[i]);
if ( cur )
handleGjob(cur);
else
fprintf( stderr, "Error parsing file '%s'\n", argv[i]); } /* Clean up everything else before quitting. */
xmlCleanupParser(); return();
}
gjobread.c
<?xml version="1.0"?>
<gjob:Helping xmlns:gjob="http://www.gnome.org/some-location">
<gjob:Jobs> <gjob:Job>
<gjob:Project ID="3"/>
<gjob:Application>GBackup</gjob:Application>
<gjob:Category>Development</gjob:Category> <gjob:Update>
<gjob:Status>Open</gjob:Status>
<gjob:Modified>Mon, 07 Jun 1999 20:27:45 -0400 MET DST</gjob:Modified>
<gjob:Salary>USD 0.00</gjob:Salary>
</gjob:Update> <gjob:Developers>
<gjob:Developer>
</gjob:Developer>
</gjob:Developers> <gjob:Contact>
<gjob:Person>Nathan Clemons</gjob:Person>
<gjob:Email>nathan@windsofstorm.net</gjob:Email>
<gjob:Company>
</gjob:Company>
<gjob:Organisation>
</gjob:Organisation>
<gjob:Webpage>
</gjob:Webpage>
<gjob:Snailmail>
</gjob:Snailmail>
<gjob:Phone>
</gjob:Phone>
</gjob:Contact> <gjob:Requirements>
The program should be released as free software, under the GPL.
</gjob:Requirements> <gjob:Skills>
</gjob:Skills> <gjob:Details>
A GNOME based system that will allow a superuser to configure
compressed and uncompressed files and/or file systems to be backed
up with a supported media in the system. This should be able to
perform via find commands generating a list of files that are passed
to tar, dd, cpio, cp, gzip, etc., to be directed to the tape machine
or via operations performed on the filesystem itself. Email
notification and GUI status display very important.
</gjob:Details> </gjob:Job> </gjob:Jobs>
</gjob:Helping>
gjobs.xml
使用libxml2进行xml开发(一)的更多相关文章
- Spring_IoC注解开发和AOP的XML开发(学习笔记2)
一:IoC注解开发 1,在applicationContext.xml中需要引入context约束 <beans xmlns="http://www.springframework.o ...
- TZ_05_Spring_事物的xml开发和annotation开发
1.Spring_事物的xml开发 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=& ...
- 使用Kotlin&Anko, 扔掉XML开发Android应用
尝鲜使用Kotlin写了一段时间Android.说大幅度的减少了Java代码一点不夸张.用Java的时候动不动就new一个OnClickListener()匿名类,动不动就类型转换的地方都可以省下很多 ...
- C#序列化xml,开发常用
序列化操作对于开发人员来说最熟悉不过了. 序列化分为:序列化和反序列化. 序列化名词解释:序列化是将对象状态转换为可保持或传输的格式的过程. 与序列化相对的是反序列化,它将流转换为对象.这两个过程结合 ...
- [libxml2]_[XML处理]_[使用libxml2的xpath特性修改xml文件内容]
场景: 1.在软件需要保存一些配置项时,使用数据库的话比较复杂,查看内容也不容易.纯文本文件对utf8字符支持也不好. 2.这时候使用xml是最佳选择,使用跨平台库libxml2. 3.基于xpath ...
- Spring中AOP的基于xml开发和配置
pom文件: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http ...
- android开发中eclipse里xml开发的自动提示和使用帮助快捷键提示
Eclipse Android 代码自动提示功能 Eclipse for android 设置代码提示功能 打 开 Eclipse 依次选择 Window > Preferences > ...
- MFC使用自带的MSXML6.dll解析xml(开发环境vc2010)
程序是win32控制台程序 // msxml.cpp : 定义控制台应用程序的入口点. #include "stdafx.h" #include <iostream> ...
- TZ_03_mybatis的xml开发
1.通过Student.xml编写sql来操作数据库 1>insert语句插入后返回主键 加入标签useGeneratedKeys=“true” keyProperty=“oid” 中 keyP ...
随机推荐
- 【转】Hadoop HDFS分布式环境搭建
原文地址 http://blog.sina.com.cn/s/blog_7060fb5a0101cson.html Hadoop HDFS分布式环境搭建 最近选择给大家介绍Hadoop HDFS系统 ...
- 骗分大法之-----分块||迷之线段树例题a
什么是分块呢? 就是一种可以帮你骗到不少分的神奇的算法. 分块的写法有几种,我所知道的有①预处理②不预处理 不预处理的代码我看得一脸懵逼 所以我在这里就谈一下预处理的版本www 首先看一道题: 给定一 ...
- Loadrunner脚本回放无法准确定位欲删除元素
Loadrunner脚本回放无法准确定位欲删除元素 问题: loadrunner脚本回放无法准确定位欲删除元素 详细: 我司ocrm系统,我的工作台菜单->我的综合工作台子页面下,工作日志页面删 ...
- OAuth 2.0 学习
OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛应用,目前的版本是2.0版. 本文对OAuth 2.0的设计思路和运行流程,做一个简明通俗的解释,主要参考材料为R ...
- jQuery 操作 html5 data-* 属性
Html 部分: <a class="nav-item" href="javascript: void(0)" data-id="{{$item ...
- Executor(一)ExecutorService 线程池
Executor(一)ExecutorService 线程池 本篇主要涉及到的是 java.util.concurrent 包中的 ExecutorService.ExecutorService 就是 ...
- 慢工出细活,Facebook点赞按钮设计中的门道
一年前,Facebook点赞按钮发布更新.一年后的今天,Facebook小小的点赞按钮因为Ted刚发布的一段演讲掀起波澜.设计一个像FB点赞按钮那么小的东西很难么?Ted中Margaret Gould ...
- 2014.1.14 struts 的default.properties 配置文件详述
转自 http://justsee.iteye.com/blog/723993 Struts 2框架有两个核心配置文件:struts.xml和struts.properties 其中struts.x ...
- android textview settext卡顿深层次原因
最近在公司项目里面发现listview里面的textview在调用settext函数的时候非常耗时,当时都有点不敢相信,这是因为如果你把textview设置成wrap_content,则每次调用set ...
- Unsorted, maximum ==> sorted
https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Moc ...