Expat是一个用C语言开发的、用来解析XML文档的开发库,它最初是开源的、Mozilla 项目下的一个XML解析器。
下载源码 进入解压目录执行 ./configure
root@ubuntu:/home/wl/service/dependency/expat/expat-2.2.0# ./configure
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking how to print strings... printf
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for a sed that does not truncate output... /bin/sed
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for fgrep... /bin/grep -F
checking for ld used by gcc... /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld... yes
checking for BSD- or MS-compatible name lister (nm)... /usr/bin/nm -B
checking the name lister (/usr/bin/nm -B) interface... BSD nm
checking whether ln -s works... yes
checking the maximum length of command line arguments... 1572864
checking how to convert x86_64-pc-linux-gnu file names to x86_64-pc-linux-gnu format... func_convert_file_noop
checking how to convert x86_64-pc-linux-gnu file names to toolchain format... func_convert_file_noop
checking for /usr/bin/ld option to reload object files... -r
checking for objdump... objdump
checking how to recognize dependent libraries... pass_all
checking for dlltool... dlltool
checking how to associate runtime and link libraries... printf %s\n
checking for ar... ar
checking for archiver @FILE support... @
checking for strip... strip
checking for ranlib... ranlib
checking for gawk... no
checking for mawk... mawk
checking command to parse /usr/bin/nm -B output from gcc object... ok
checking for sysroot... no
checking for a working dd... /bin/dd
checking how to truncate binary pipes... /bin/dd bs=4096 count=1
checking for mt... mt
checking if mt is a manifest tool... no
checking how to run the C preprocessor... gcc -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking for dlfcn.h... yes
checking for objdir... .libs
checking if gcc supports -fno-rtti -fno-exceptions... no
checking for gcc option to produce PIC... -fPIC -DPIC
checking if gcc PIC flag -fPIC -DPIC works... yes
checking if gcc static flag -static works... yes
checking if gcc supports -c -o file.o... yes
checking if gcc supports -c -o file.o... (cached) yes
checking whether the gcc linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
checking whether -lc should be explicitly linked in... no
checking dynamic linker characteristics... GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... yes
checking for gcc... (cached) gcc
checking whether we are using the GNU C compiler... (cached) yes
checking whether gcc accepts -g... (cached) yes
checking for gcc option to accept ISO C89... (cached) none needed
checking for g++... g++
checking whether we are using the GNU C++ compiler... yes
checking whether g++ accepts -g... yes
checking how to run the C++ preprocessor... g++ -E
checking for ld used by g++... /usr/bin/ld -m elf_x86_64
checking if the linker (/usr/bin/ld -m elf_x86_64) is GNU ld... yes
checking whether the g++ linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
checking for g++ option to produce PIC... -fPIC -DPIC
checking if g++ PIC flag -fPIC -DPIC works... yes
checking if g++ static flag -static works... yes
checking if g++ supports -c -o file.o... yes
checking if g++ supports -c -o file.o... (cached) yes
checking whether the g++ linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
checking dynamic linker characteristics... (cached) GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking for a BSD-compatible install... /usr/bin/install -c
checking whether gcc accepts -fexceptions... yes
checking for ANSI C header files... (cached) yes
checking whether byte ordering is bigendian... no
checking for an ANSI C-conforming const... yes
checking for size_t... yes
checking for memmove... yes
checking for bcopy... yes
checking fcntl.h usability... yes
checking fcntl.h presence... yes
checking for fcntl.h... yes
checking for unistd.h... (cached) yes
checking for off_t... yes
checking for stdlib.h... (cached) yes
checking for unistd.h... (cached) yes
checking for sys/param.h... yes
checking for getpagesize... yes
checking for working mmap... yes
checking for gcc option to accept ISO C99... -std=gnu99
checking for gcc -std=gnu99 option to accept ISO Standard C... (cached) -std=gnu99
checking for an ANSI C99-conforming __func__... yes
configure: creating ./config.status
config.status: creating Makefile
config.status: WARNING: 'Makefile.in' seems to ignore the --datarootdir setting
config.status: creating expat.pc
config.status: creating expat_config.h
config.status: executing libtool commands
root@ubuntu:/home/wl/service/dependency/expat/expat-2.2.0
执行make
root@ubuntu:/home/wl/service/dependency/expat/expat-2.2.0# make
/bin/bash ./libtool --verbose --mode=compile gcc -std=gnu99 -I./lib -I. -g -O2 -Wall -Wmissing-prototypes -Wstrict-prototypes -fexceptions -DHAVE_EXPAT_CONFIG_H -o lib/xmlparse.lo -c lib/xmlparse.c
libtool: compile: gcc -std=gnu99 -I./lib -I. -g -O2 -Wall -Wmissing-prototypes -Wstrict-prototypes -fexceptions -DHAVE_EXPAT_CONFIG_H -c lib/xmlparse.c -fPIC -DPIC -o lib/.libs/xmlparse.o
libtool: compile: gcc -std=gnu99 -I./lib -I. -g -O2 -Wall -Wmissing-prototypes -Wstrict-prototypes -fexceptions -DHAVE_EXPAT_CONFIG_H -c lib/xmlparse.c -o lib/xmlparse.o >/dev/null 2>&1
/bin/bash ./libtool --verbose --mode=compile gcc -std=gnu99 -I./lib -I. -g -O2 -Wall -Wmissing-prototypes -Wstrict-prototypes -fexceptions -DHAVE_EXPAT_CONFIG_H -o lib/xmltok.lo -c lib/xmltok.c
libtool: compile: gcc -std=gnu99 -I./lib -I. -g -O2 -Wall -Wmissing-prototypes -Wstrict-prototypes -fexceptions -DHAVE_EXPAT_CONFIG_H -c lib/xmltok.c -fPIC -DPIC -o lib/.libs/xmltok.o
libtool: compile: gcc -std=gnu99 -I./lib -I. -g -O2 -Wall -Wmissing-prototypes -Wstrict-prototypes -fexceptions -DHAVE_EXPAT_CONFIG_H -c lib/xmltok.c -o lib/xmltok.o >/dev/null 2>&1
/bin/bash ./libtool --verbose --mode=compile gcc -std=gnu99 -I./lib -I. -g -O2 -Wall -Wmissing-prototypes -Wstrict-prototypes -fexceptions -DHAVE_EXPAT_CONFIG_H -o lib/xmlrole.lo -c lib/xmlrole.c
libtool: compile: gcc -std=gnu99 -I./lib -I. -g -O2 -Wall -Wmissing-prototypes -Wstrict-prototypes -fexceptions -DHAVE_EXPAT_CONFIG_H -c lib/xmlrole.c -fPIC -DPIC -o lib/.libs/xmlrole.o
libtool: compile: gcc -std=gnu99 -I./lib -I. -g -O2 -Wall -Wmissing-prototypes -Wstrict-prototypes -fexceptions -DHAVE_EXPAT_CONFIG_H -c lib/xmlrole.c -o lib/xmlrole.o >/dev/null 2>&1
/bin/bash ./libtool --verbose --mode=link gcc -std=gnu99 -I./lib -I. -g -O2 -Wall -Wmissing-prototypes -Wstrict-prototypes -fexceptions -DHAVE_EXPAT_CONFIG_H -no-undefined -version-info 7:2:6 -rpath /usr/local/lib -o libexpat.la lib/xmlparse.lo lib/xmltok.lo lib/xmlrole.lo
libtool: link: gcc -shared -fPIC -DPIC lib/.libs/xmlparse.o lib/.libs/xmltok.o lib/.libs/xmlrole.o -g -O2 -Wl,-soname -Wl,libexpat.so.1 -o .libs/libexpat.so.1.6.2
libtool: link: (cd ".libs" && rm -f "libexpat.so.1" && ln -s "libexpat.so.1.6.2" "libexpat.so.1")
libtool: link: (cd ".libs" && rm -f "libexpat.so" && ln -s "libexpat.so.1.6.2" "libexpat.so")
libtool: link: ar cru .libs/libexpat.a lib/xmlparse.o lib/xmltok.o lib/xmlrole.o
libtool: link: ranlib .libs/libexpat.a
libtool: link: creating libexpat.la
libtool: link: ( cd ".libs" && rm -f "libexpat.la" && ln -s "../libexpat.la" "libexpat.la" )
gcc -std=gnu99 -I./lib -I. -g -O2 -Wall -Wmissing-prototypes -Wstrict-prototypes -fexceptions -DHAVE_EXPAT_CONFIG_H -o xmlwf/xmlwf.o -c xmlwf/xmlwf.c
gcc -std=gnu99 -I./lib -I. -g -O2 -Wall -Wmissing-prototypes -Wstrict-prototypes -fexceptions -DHAVE_EXPAT_CONFIG_H -o xmlwf/xmlfile.o -c xmlwf/xmlfile.c
gcc -std=gnu99 -I./lib -I. -g -O2 -Wall -Wmissing-prototypes -Wstrict-prototypes -fexceptions -DHAVE_EXPAT_CONFIG_H -o xmlwf/codepage.o -c xmlwf/codepage.c
gcc -std=gnu99 -I./lib -I. -g -O2 -Wall -Wmissing-prototypes -Wstrict-prototypes -fexceptions -DHAVE_EXPAT_CONFIG_H -o xmlwf/unixfilemap.o -c xmlwf/unixfilemap.c
/bin/bash ./libtool --verbose --mode=link gcc -std=gnu99 -I./lib -I. -g -O2 -Wall -Wmissing-prototypes -Wstrict-prototypes -fexceptions -DHAVE_EXPAT_CONFIG_H -o xmlwf/xmlwf xmlwf/xmlwf.o xmlwf/xmlfile.o xmlwf/codepage.o xmlwf/unixfilemap.o libexpat.la
libtool: link: gcc -std=gnu99 -I./lib -I. -g -O2 -Wall -Wmissing-prototypes -Wstrict-prototypes -fexceptions -DHAVE_EXPAT_CONFIG_H -o xmlwf/.libs/xmlwf xmlwf/xmlwf.o xmlwf/xmlfile.o xmlwf/codepage.o xmlwf/unixfilemap.o ./.libs/libexpat.so
libtool: link: creating xmlwf/xmlwf
root@ubuntu:/home/wl/service/dependency/expat/expat-2.2.0#
make install
root@ubuntu:/home/wl/service/dependency/expat/expat-2.2.0# make install
/bin/bash ./conftools/mkinstalldirs /usr/local/lib /usr/local/include /usr/local/lib/pkgconfig
/bin/bash ./libtool --mode=install /usr/bin/install -c libexpat.la /usr/local/lib/libexpat.la
libtool: install: /usr/bin/install -c .libs/libexpat.so.1.6.2 /usr/local/lib/libexpat.so.1.6.2
libtool: install: (cd /usr/local/lib && { ln -s -f libexpat.so.1.6.2 libexpat.so.1 || { rm -f libexpat.so.1 && ln -s libexpat.so.1.6.2 libexpat.so.1; }; })
libtool: install: (cd /usr/local/lib && { ln -s -f libexpat.so.1.6.2 libexpat.so || { rm -f libexpat.so && ln -s libexpat.so.1.6.2 libexpat.so; }; })
libtool: install: /usr/bin/install -c .libs/libexpat.lai /usr/local/lib/libexpat.la
libtool: install: /usr/bin/install -c .libs/libexpat.a /usr/local/lib/libexpat.a
libtool: install: chmod 644 /usr/local/lib/libexpat.a
libtool: install: ranlib /usr/local/lib/libexpat.a
libtool: finish: PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/sbin" ldconfig -n /usr/local/lib
----------------------------------------------------------------------
Libraries have been installed in:
/usr/local/lib
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the '-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the 'LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the 'LD_RUN_PATH' environment variable
during linking
- use the '-Wl,-rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to '/etc/ld.so.conf'
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
for FN in ./lib/expat.h ./lib/expat_external.h expat_config.h ; do /usr/bin/install -c -m 644 $FN /usr/local/include ; done
/usr/bin/install -c -m 644 expat.pc /usr/local/lib/pkgconfig/expat.pc
/bin/bash ./conftools/mkinstalldirs /usr/local/bin /usr/local/share/man/man1
/bin/bash ./libtool --mode=install /usr/bin/install -c xmlwf/xmlwf /usr/local/bin/xmlwf
libtool: install: /usr/bin/install -c xmlwf/.libs/xmlwf /usr/local/bin/xmlwf
/usr/bin/install -c -m 644 ./doc/xmlwf.1 /usr/local/share/man/man1
root@ubuntu:/home/wl/service/dependency/expat/expat-2.2.0#
完成
expat的简单使用
参考expat源码目录下doc目录的reference.html和example目录下的几个示例代码
编译方法
g++ test.c -L /usr/local/include -lexpat 或 gcc test.c -L /usr/local/include -lexpat
expat安装后头文件默认在/usr/local/include目录下,需要使用-L参数指定头文件目录否则会报错
test.c:(.text+0x2af): undefined reference to `XML_ParserCreate'
test.c:(.text+0x2c9): undefined reference to `XML_SetUserData'
test.c:(.text+0x2df): undefined reference to `XML_SetElementHandler'
test.c:(.text+0x2f0): undefined reference to `XML_SetCharacterDataHandler'
test.c:(.text+0x31c): undefined reference to `XML_Parse'
test.c:(.text+0x3d8): undefined reference to `XML_ParserFree'
下面是我从网上拷贝的一个c++例子,原文链接用expat解析xml文件c++,代码如下
#include
#include "expat.h"
#include
#include
#include
#include
using namespace std;
char * elempt = "apple"; //userData
string buf = "" //XML格式的数据
"apple<\/red>"
"<\/blue>"
"tree<\/green>"
"hello kitty<\/pink>"
"<\/name>";
//如果是windows环境,<\/name>是对的
//如果是unix环境,则为
typedef struct{
string red;
string blue;
string green;
string pink;
}fruit;
fruit table_temp;
vector mytable;
int flag = 0;
void StartElementHandler(void *userData, const XML_Char *name, const XML_Char **atts) //遇到起始标记的回调函数
{
if(!strcmp(name,"name"))
{
//cout << name << "\tstart_of_name" << endl;
}
if(!strcmp(name,"red")) //当遇到时,将flag置1
{
//cout << name << "\tstart_of_red" << endl;;
//cout << atts[0] << "\t" << atts[1]<< "\t" << atts[2]<< "\t" << atts[3]<< endl;
flag = 1;
}
if(!strcmp(name,"blue")) //当遇到时,将flag置2
{
//cout << name << "\tstart_of_blue" << endl;
flag = 2;
}
if(!strcmp(name,"green")) //当遇到时,将flag置3
{
//cout << name << "\tstart_of_green" << endl;
flag = 3;
}
if(!strcmp(name,"pink")) //当遇到时,将flag置4
{
//cout << name << "\tstart_of_pink" << endl;
flag = 4;
}
}
void EndElementHandler(void *userData, const XML_Char *name)
{
if(!strcmp(name,"pink")) //当遇到pink结束标签时,表示4个数据都得到了,将结构体放入vecotr中
{
//cout << name << "\tend_of_name" << endl;
mytable.push_back(table_temp);
}
if(!strcmp(name,"red"))
{
//cout << name << "\tend_of_red" << endl;
}
if(!strcmp(name,"blue"))
{
//cout << name << "\tend_of_blue" << endl;
}
if(!strcmp(name,"green"))
{
//cout << name << "\tend_of_green" << endl;
}
if(!strcmp(name,"pink"))
{
//cout << name << "\tend_of_pink" << endl;
}
}
void CharacterDataHandler(void *userData, const XML_Char *s, int len)
{
string str2 = s;
string str1;
//cout << str2 << endl;
for(int i=0;i
将该代码放入test.c中并编译
root@ubuntu:/home/wl/service/dependency/expat/expat-2.2.0/examples# g++ test.c -L /usr/local/include -lexpat
test.c:8:17: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
char * elempt = "apple"; //userData
^
test.c:14:10: warning: unknown escape sequence: '\/' [enabled by default]
"<\/name>";
^
test.c:14:10: warning: unknown escape sequence: '\/' [enabled by default]
test.c:14:10: warning: unknown escape sequence: '\/' [enabled by default]
test.c:14:10: warning: unknown escape sequence: '\/' [enabled by default]
test.c:14:10: warning: unknown escape sequence: '\/' [enabled by default]
root@ubuntu:/home/wl/service/dependency/expat/expat-2.2.0/examples#
执行
./a.out
root@ubuntu:/home/wl/service/dependency/expat/expat-2.2.0/examples# ./a.out
apple
tree
hello kitty
root@ubuntu:/home/wl/service/dependency/expat/expat-2.2.0/examples#
更多用法参考https://www.xml.com/pub/a/1999/09/expat/index.html