(一)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

http://www.ualberta.ca/dept/aict/uts/software/solaris9/sparc/patches/solaris/9_Recommended/114014-18/SUNWlxmlS/reloc/usr/share/src/libxml2/example/gjobread.c

http://www.ualberta.ca/dept/aict/uts/software/solaris9/sparc/patches/solaris/9_Recommended/114014-18/SUNWlxmlS/reloc/usr/share/src/libxml2/example/gjobs.xml

笔者在编译成功后,运行的时候遇到过如下的一个错误

“无法定位程序输入点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开发(一)的更多相关文章

  1. Spring_IoC注解开发和AOP的XML开发(学习笔记2)

    一:IoC注解开发 1,在applicationContext.xml中需要引入context约束 <beans xmlns="http://www.springframework.o ...

  2. TZ_05_Spring_事物的xml开发和annotation开发

    1.Spring_事物的xml开发 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=& ...

  3. 使用Kotlin&Anko, 扔掉XML开发Android应用

    尝鲜使用Kotlin写了一段时间Android.说大幅度的减少了Java代码一点不夸张.用Java的时候动不动就new一个OnClickListener()匿名类,动不动就类型转换的地方都可以省下很多 ...

  4. C#序列化xml,开发常用

    序列化操作对于开发人员来说最熟悉不过了. 序列化分为:序列化和反序列化. 序列化名词解释:序列化是将对象状态转换为可保持或传输的格式的过程. 与序列化相对的是反序列化,它将流转换为对象.这两个过程结合 ...

  5. [libxml2]_[XML处理]_[使用libxml2的xpath特性修改xml文件内容]

    场景: 1.在软件需要保存一些配置项时,使用数据库的话比较复杂,查看内容也不容易.纯文本文件对utf8字符支持也不好. 2.这时候使用xml是最佳选择,使用跨平台库libxml2. 3.基于xpath ...

  6. Spring中AOP的基于xml开发和配置

    pom文件: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http ...

  7. android开发中eclipse里xml开发的自动提示和使用帮助快捷键提示

    Eclipse Android 代码自动提示功能 Eclipse for android 设置代码提示功能 打 开 Eclipse 依次选择 Window > Preferences > ...

  8. MFC使用自带的MSXML6.dll解析xml(开发环境vc2010)

    程序是win32控制台程序 // msxml.cpp : 定义控制台应用程序的入口点. #include "stdafx.h" #include <iostream> ...

  9. TZ_03_mybatis的xml开发

    1.通过Student.xml编写sql来操作数据库 1>insert语句插入后返回主键 加入标签useGeneratedKeys=“true” keyProperty=“oid” 中 keyP ...

随机推荐

  1. 什么是Shell、Shell脚本

    Shell本身是一个用C语言编写的程序,它是用户使用Linux的桥梁.Shell既是一种命令语言,又是一种程序设计语言.作为命令语言,它交互式地解释和执行用户输入的命令:作为程序设计语言,它定义了各种 ...

  2. struts2框架之自定义拦截器和配置

    struts框架中也存在拦截器,只不过系统自动调用.框架自带的拦截器的配置文件所在的位置为: java Resources--->Libraries--->struts2-core-2.3 ...

  3. OSGi 系列(十三)之 Configuration Admin Service

    OSGi 系列(十三)之 Configuration Admin Service OSGi 的 CM 就是 Configuration Admin Service,是用于管理 Bundle 属性.并在 ...

  4. Spring.NET学习笔记6——依赖注入(应用篇)

    1. 谈到高级语言编程,我们就会联想到设计模式:谈到设计模式,我们就会说道怎么样解耦合.而Spring.NET的IoC容器其中的一种用途就是解耦合,其最经典的应用就是:依赖注入(Dependeny I ...

  5. PetaPoco与MySQL

    随便写写的,PetaPoco与MySQL一起使用,在一个工控项目中充分使用节省不少开发时间,经历大半年的努力的项目接近完成,客户不认帐,开始需求合同就是个败笔,技术还是仅能解决技术问题而已! 上图上代 ...

  6. 2018秋季c语言基础课第一次作业

    1)大学和高中最大的不同是没有人天天看着你,请看大学理想的师生关系是?有何感想? 答:邹欣老师提到了很多种关系,不外呼就是两种:平等或者不平等.平等的师生关系与陌生人无异,而自古以来尊师重道却被世人所 ...

  7. Jigloo 下载 安装 GUI

    这个需要授权,一直不能解决!! 网上找了很多,都觉不能访问,这个可以用Eclipse直接更新的 http://www.cloudgardensoftware.com/jigloo/update-sit ...

  8. kafka系列 -- 多线程消费者实现

    看了一下kafka,然后写了消费Kafka数据的代码.感觉自己功力还是不够. 不能随心所欲地操作数据,数据结构没学好,spark的RDD操作没学好. 不能很好地组织代码结构,设计模式没学好,面向对象思 ...

  9. yii2 HTML组手

    1.样式和脚本 1.1 Yii 提供两个方法用于生成包含内联样式和脚本代码的标签. <?= Html::style('.danger { color: #f00; }') ?> Gives ...

  10. EXT combobox 二级连动 清空store缓存数据

    项目中有这样的一个需求,做一个连动操作,如图: 所属行业中的combobox中下拉框中的值会根据前一个选择框中的值动态去变化,这个其实非常好做,但不是我现在讨论的主要问题,主要问题是,当第二次选择了& ...