Linux 下C/C++解析XML文件
2015-11-27 16:11
477 查看
在工程项目中我们的项目需要根据不同的环境配置不同的程序参数,而常用的两种文件分别是ini文件和XML文件,接下来我来分析下在Linux下解析XML文件过程。
我们首先使用linux自带的libxml2来解析XML文件。
在libxml2中比较重要的数据结构是xmlNodePtr,它在libxml/tree.h中定义为
解析代码如下:
使用的XML文件如下
我们首先使用linux自带的libxml2来解析XML文件。
在libxml2中比较重要的数据结构是xmlNodePtr,它在libxml/tree.h中定义为
/** * xmlNode: * * A node in an XML tree. */ typedef struct _xmlNode xmlNode; typedef xmlNode *xmlNodePtr; struct _xmlNode { void *_private; /* application data */ xmlElementType type; /* type number, must be second ! */ const xmlChar *name; /* the name of the node, or the entity */ struct _xmlNode *children; /* parent->childs link */ struct _xmlNode *last; /* last child link */ struct _xmlNode *parent; /* child->parent link */ struct _xmlNode *next; /* next sibling link */ struct _xmlNode *prev; /* previous sibling link */ struct _xmlDoc *doc; /* the containing document */ /* End of common part */ xmlNs *ns; /* pointer to the associated namespace */ xmlChar *content; /* the content */ struct _xmlAttr *properties;/* properties list */ xmlNs *nsDef; /* namespace definitions on this node */ void *psvi; /* for type/PSVI informations */ unsigned short line; /* line number */ unsigned short extra; /* extra data for XPath/XSLT */ };
解析代码如下:
使用的XML文件如下
<?xml version="1.0" encoding="UTF-8"?> <root> <monitor> <name>ftp</name> <!--监控的程序名称--> <path>/usr/bin/</path> <!--程序绝对路径--> <interval>30</interval> <!--间隔多久监控一次--> <restartwait>30</restartwait> <!--程序被重启后,等待多久再进行监控--> <ip>127.0.0.1</ip> <!--程序IP--> <port>9878</port> <!--程序端口--> <request>1;monitor</request> <!--发送给程序的报文--> <response>The ftp is working ok.</response> <!--程序给监控的反馈报文--> <log> <folderpath>log/</folderpath> <!--程序日志保存目录--> <savedays>2</savedays> <!--程序日志保存时间--> </log> </monitor> </root>
相关文章推荐
- Linux 文件删除 注意事项
- Linux查看系统信息的一些命令及查看已安装软件包的命令(转)
- LINUX 安全运维 (三)
- Linux kernel Panic后自动重启机器的设置
- Linux下修改网卡IP、DNS和网关
- 初学Linux,自己做笔记
- 主机Linux的交叉编译链生成、安装、配置
- Linux下iptables 禁止端口和开放端口(仅供参考,里面含有iptables如何指定允许外网访问的端口号范围,以及对局域网的ip全部开放端口)
- vs2010开发linux平台C程序 开发环境配置
- Makefile的obj-y和obj-m语句
- linux驱动程序调试常用方法
- 有关Linux邮件的基础知识
- uboot源码及启动过程分析
- Centos 6.5创建本地YUM仓库
- 详解Linux安装GCC方法
- linux 查看库依赖关系
- s3c2440开发板Linux系统下eeprom驱动制作以及读写程序
- linux 查看库依赖关系
- linux网站推荐
- windows/linux/esxi查看硬件序列号