解压
tar -xf mxml-2.10.tar
安装
BUILDING Mini-XML
Mini-XML comes with an autoconf-based configure script; just type the
following command to get things going:
./configure
The default install prefix is /usr/local, which can be overridden using the
--prefix option:
./configure --prefix=/foo
Other configure options can be found using the --help option:
./configure --help
Once you have configured the software, type "make" to do the build and run
the test program to verify that things are working, as follows:
make
If you are using Mini-XML under Microsoft Windows with Visual C++, use the
included project files in the "vcnet" subdirectory to build the library
instead. Note: The static library on Windows is NOT thread-safe.
可以看出,与一般安装一样。
先./configure
。
$ ./configure
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 for shared library support... yes
configure: creating ./config.status
config.status: creating Makefile
config.status: creating mxml.list
config.status: creating mxml.pc
config.status: creating config.h
$
再执行make
,最后执行sudo make install
,安装成功。
默认就是安装在/usr/local
的,如果安装在其它位置,要在/etc/ld.so.conf.d/libc.conf
文件配置上路径。
# libc default configuration
/usr/local/lib
注意 ,生成Makefile时,也可以关闭线程支持./configure --enable-threads=no
。
使用的话,加上#include <mxml.h>
头文件就行。
#include <stdio.h>
#include <mxml.h>
int main(int argc, const char* argv[])
{
// 创建xml文件头节点
mxml_node_t *xml = mxmlNewXML("1.0");
// 创建xml根节点 - china
mxml_node_t* china = mxmlNewElement(xml, "China");
// 创建城市节点
mxml_node_t* city = mxmlNewElement(china, "City");
// 添加子节点
// name
mxml_node_t* name = mxmlNewElement(city, "Name");
// 设置标签值
mxmlNewText(name, 0, "北京");
mxmlElementSetAttr(name, "isbig", "true");
// 面积
mxml_node_t* area = mxmlNewElement(city, "Area");
mxmlNewText(area, 0, "1.641万平方千米");
// 人口
mxml_node_t* popu = mxmlNewElement(city, "Population");
mxmlNewText(popu, 0, "2200万");
// 第二个城市节点
city = mxmlNewElement(china, "City");
// name
name = mxmlNewElement(city, "Name");
mxmlNewText(name, 0, "石家庄");
mxmlElementSetAttr(name, "isbig", "false");
area = mxmlNewElement(city, "Area");
mxmlNewText(area, 0, "15848平方千米");
popu = mxmlNewElement(city, "Population");
mxmlNewText(popu, 0, "107万");
// 将xml内容保存到磁盘
FILE* fp = fopen("china.xml", "w");
mxmlSaveFile(xml, fp, MXML_NO_CALLBACK);
fclose(fp);
mxmlDelete(xml);
return 0;
}
$ gcc create_xml.c -o create
/usr/bin/ld: /tmp/ccMJhREG.o: in function `main':
create_xml.c:(.text+0x15): undefined reference to `mxmlNewXML'
/usr/bin/ld: create_xml.c:(.text+0x2a): undefined reference to `mxmlNewElement'
/usr/bin/ld: create_xml.c:(.text+0x3f): undefined reference to `mxmlNewElement'
/usr/bin/ld: create_xml.c:(.text+0x54): undefined reference to `mxmlNewElement'
/usr/bin/ld: create_xml.c:(.text+0x6e): undefined reference to `mxmlNewText'
/usr/bin/ld: create_xml.c:(.text+0x84): undefined reference to `mxmlElementSetAttr'
/usr/bin/ld: create_xml.c:(.text+0x95): undefined reference to `mxmlNewElement'
/usr/bin/ld: create_xml.c:(.text+0xaf): undefined reference to `mxmlNewText'
运行时发现报错了,库没引用对,gcc create_xml.c -lmxml -o create
$ gcc create_xml.c -lmxml -o create
/usr/bin/ld: //usr/local/lib/libmxml.so: undefined reference to `pthread_getspecific'
/usr/bin/ld: //usr/local/lib/libmxml.so: undefined reference to `pthread_key_create'
/usr/bin/ld: //usr/local/lib/libmxml.so: undefined reference to `pthread_once'
/usr/bin/ld: //usr/local/lib/libmxml.so: undefined reference to `pthread_setspecific'
/usr/bin/ld: //usr/local/lib/libmxml.so: undefined reference to `pthread_key_delete'
collect2: error: ld returned 1 exit status
缺线程库 ,加上-lpthread
。所以完整的命令是gcc create_xml.c -lmxml -lpthread -o create
编译成功。
如果报找不到库,更新一下缓存sudo ldconfig
。
$ ./create
./create: error while loading shared libraries: libmxml.so.1: cannot open shared object file: No such file or directory
$ sudo ldconfig
$ ./create
$ ls
china.xml create create_xml.c
查看china.xml
<?xml version="1.0" encoding="utf-8"?>
<China>
<City>
<Name isbig="true">北京</Name>
<Area>1.641万平方千米</Area>
<Population>2200万</Population>
</City>
<City>
<Name isbig="false">石家庄</Name>
<Area>15848平方千米</Area>
<Population>107万</Population>
</City>
</China>
#include <stdio.h>
#include <mxml.h>
int main(int argc, const char* argv[])
{
// 从磁盘加载xml文件
FILE* fp = fopen("china.xml", "r");
if(fp == NULL)
{
printf("fopen error\n");
return 0;
}
// root 节点指向xml文件头
mxml_node_t* root = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK);
// 遍历 - 取出各个节点的值
// 找到第一个城市节点
mxml_node_t* city = mxmlFindElement(root, root, "City", NULL, NULL, MXML_DESCEND);
if(city == NULL)
{
printf("xml node not found\n");
return 0;
}
while( city )
{
printf("==================\n");
// 向下走一个节点
mxml_node_t* node = mxmlWalkNext(city, root, MXML_DESCEND_FIRST);
printf("city: \n");
printf(" name = %s\n", mxmlGetText(node, NULL));
//
node = mxmlWalkNext(node, root, MXML_NO_DESCEND);
printf(" area = %s\n", mxmlGetText(node, NULL));
//
node = mxmlWalkNext(node, root, MXML_NO_DESCEND);
printf(" population = %s\n", mxmlGetText(node, NULL));
// 搜索下一个城市节点
city = mxmlFindElement(city, root, "City", NULL, NULL, MXML_DESCEND);
}
fclose(fp);
mxmlDelete(root);
return 0;
}
xml简单介绍及libmxml编程
解决error while loading shared libraries: libXXX.so.X: cannot open shared object file: No such file