下载地址http://xmlsoft.org/downloads.html,可以git clone下来,也可以下载压缩包。
在arm平台下运行。
cd libxml2-2.9.1
./autogen.sh
./configure CROSS_COMPILE=arm-hisiv400-linux- --host=arm-hisiv400-linux --prefix=/opt/libxml2 --enable-shared --with-python=no
make
make install
arm-hisiv400-linux-gcc test.c -o test -I /opt/libxml2/include/libxml2 -L /opt/libxml2/lib -lxml2
#include
#include
static const char *TAG_RECTNODE = "RECTNODE";
static const char *TAG_INFO = "INFO";
static const char *TAG_ATTR_WIDTH = "Width";
static const char *TAG_ATTR_HEIGHT = "Height";
int main(){
xmlDocPtr doc;
xmlNodePtr cur;
doc = xmlParseFile("./test.xml"); //打开解析的xml文件
if (doc == NULL) {
printf( "open file fail!\r\n");
return false;
}
cur = xmlDocGetRootElement(doc); //拿到文件根节点
if (cur == NULL) {
printf( "empty document!\r\n");
xmlFreeDoc(doc);
return false;
}
cur = cur->xmlChildrenNode;
while (cur != NULL) {
if (!xmlStrcmp(cur->name, (const xmlChar *)TAG_INFO)) {
//遍历根节点底下的子节点
char *strWallWidth = (char *)xmlGetProp(cur,(const xmlChar *)TAG_ATTR_WIDTH);
}
cur = cur->next;
}
xmlFreeDoc(doc);
xmlCleanupParser();
return 0;
}
test.xml
<?xml version="1.0" encoding="UTF-8"?>
<RECTNODE> #根节点
<INFO #子节点
Width="7680"
Height="2880"
/>
</RECTNODE>
这样我们可以快速的搭建配置脚本环境,将需要的配置项写入子节点或者新建更多的节点来构建简易配置文件。该例子快速入门并且迅速上手,希望有所帮助。