C程序使用xml
项目用到的 改编简单实例
1.使用libxml2
tar xjvf libxml2-2.6.24.tar.bz2
cd libxml2-2.6.24
./configure --prefix=/arm/usr --host=arm-linux
make
make install
2.编写rxml.xml
<?xml version="1.0"?>
<qvbstr>
<storyinfo>
<author>qvb3d</author>
<datewritten>March 2, 2012</datewritten>
<keyword>example keyword</keyword>
</storyinfo>
<body>
<headline>This is the headline</headline>
<para>This is the body text.</para>
</body>
</qvbstr>
3.编写C程序
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#define TESTXML_FILE "rxml.xml"
int main(int argc,char *argv[])
{
char docname[]=TESTXML_FILE;
xmlDocPtr doc;
xmlNodePtr cur;
doc=xmlParseFile(docname);
if (doc==NULL)
{
printf("Document %s error!\n",docname);
return 0;
}
cur=xmlDocGetRootElement(doc);
if (cur==NULL)
{
printf("empty Document %s error!\n",docname);
return 0;
}
if(xmlStrcmp(cur->name,(const xmlChar *)"qvbstr"))
{
printf("Document of the wrong type,root node!=qvbstr\n");
}
if(!(cur->name==NULL))
{
printf("root node=%s\n",cur->name);
}
xmlFreeDoc(doc);
return 0;
}
4.写Makefile
all:
gcc rxml.c -o rxml -lxml2 -lz -lpthread -licucore -lm \
-I/arm/include/libxml2 -L/arm/lib
clean:
rm -rf rxml
保存
也可以手工使用
xml2-config --libs --cflags
查看要链接的库位置和链接标志
前提你编译时,安装到系统上一个libxml2
./configure --prefix=/usr