Tinyxml的简单应用
参考文章: 1-> http://www.cnblogs.com/phinecos/archive/2008/03/11/1100912.html
2-> http://blog.csdn.net/clever101/article/details/5334369
在日常开发过程中,常常需要用到xml文件的读写,tinyxml是一款轻量级的xml开源库,对于诸如程序配置,账单记录等常见的xml文件读写,tinyxml完全可以胜任.
1->代码下载:http://sourceforge.net/projects/tinyxml/
2->编译
解压下载包,用VS2010打开 tinyxml.sln 解决方案,里面共有四个工程,编译第一个工程 tinyxml ,生成 tinyxml.lib 静态库文件,我们所需要用到的就是 tinyxml.h 和 tinyxml.lib .
3->测试
新建工程,添加tinyxml.h头文件和tinyxml.lib静态库,我们就可以对xml文件进行都写了,以下是测试代码.
#include <iostream>
#include <string>
#include "tinystr.h"
#include "tinyxml.h"
using namespace std; #define ASSERT_P(v,s,r) do{if(v==s)return r;} while(0); bool CreateFile(const char* szFileName)
{
// 创建文档对象
TiXmlDocument *pDocument = new TiXmlDocument();
ASSERT_P(pDocument,NULL,false);
// 创建根元素,连接到pDocument
TiXmlElement *pConfigElement = new TiXmlElement("config");
ASSERT_P(pConfigElement,NULL,false);
pDocument->LinkEndChild(pConfigElement);
// 创建一个common元素,连接到config下
TiXmlElement *pCommonElement = new TiXmlElement("common");
ASSERT_P(pCommonElement,NULL,false);
// 为common元素设置属性(key,value)
pCommonElement->SetAttribute("zoneid","");
pCommonElement->SetAttribute("platid","");
pConfigElement->LinkEndChild(pCommonElement);
// 添加zone元素
int nZoneCnt = ;
TiXmlElement *pZoneListElement = new TiXmlElement("zonelist");
ASSERT_P(pZoneListElement,NULL,false);
pZoneListElement->SetAttribute("zonecount",);
pConfigElement->LinkEndChild(pZoneListElement);
for(int i=;i<;i++)
{
char szName[];
TiXmlElement *pZoneElement = new TiXmlElement("zone");
sprintf(szName,"zone_%02d",i+);
pZoneElement->SetAttribute("name",szName);
pZoneListElement->LinkEndChild(pZoneElement);
}
return pDocument->SaveFile(szFileName);
} bool ReadFile(const char *szFileName)
{
// 加载文件
TiXmlDocument *pDocument = new TiXmlDocument(szFileName);
ASSERT_P(pDocument,NULL,false);
pDocument->LoadFile();
// 获得,并输出根元素
TiXmlElement *pConfigElement = pDocument->RootElement();
cout<<pConfigElement->Value()<<endl;
// 获取config的第一个子元素
TiXmlElement *pCommonElement = pConfigElement->FirstChildElement();
// or TiXmlElement *pCommonElement = pConfigElement->FirstChildElement("common");
ASSERT_P(pCommonElement,NULL,false);
cout<<"\tzoneid="<<pCommonElement->Attribute("zoneid")<<",platid="<<pCommonElement->Attribute("platid")<<endl;
// 第二个子元素
TiXmlElement *pZoneListElement = pConfigElement->FirstChildElement("zonelist");
ASSERT_P(pZoneListElement,NULL,false);
int nZoneCount = atoi(pZoneListElement->Attribute("zonecount"));
TiXmlElement *pZoneElement = pZoneListElement->FirstChildElement();
for(int i=;i<nZoneCount;i++)
{
ASSERT_P(pZoneElement,NULL,false);
cout<<"\t\tname="<<pZoneElement->Attribute("name")<<endl;
pZoneElement = pZoneElement->NextSiblingElement();
}
return true;
} int main()
{
const char *szFileName = "./config.xml";
CreateFile(szFileName);
ReadFile(szFileName);
system("pause");
return ;
}
Tinyxml的简单应用的更多相关文章
- 开源TinyXML 最简单的新手教程
TinyXML它是基于一个非常受欢迎的现在DOM型号XML解析器,简单易用且小巧玲珑,很适合存储简单数据.配置文件. 该项目属于开源项目,在sourceforge上边的链接是:http://sourc ...
- TinyXML 的简单介绍以及使用
先说几句重点: 1,tinyxml 生成或解析XML非常好用 2,tinyxml 利用DOM(文档对象模型)操作XML,根节点与各个子节点相当于形成一棵树 3,只要你了解tinyxml的用法,可以只n ...
- TinyXML用法小结2
参考:http://www.cnblogs.com/hgwang/p/5833638.html TinyXML用法小结 1. 介绍 Tinyxml的官方网址:http://www.grinn ...
- 值得推荐的C/C++框架和库
值得推荐的C/C++框架和库 [本文系外部转贴,原文地址:http://coolshell.info/c/c++/2014/12/13/c-open-project.htm]留作存档 下次造轮子前先看 ...
- [转]C/C++ 程序员必须收藏的资源大全
from: https://github.com/jobbole/awesome-cpp-cn C++ 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome – XXX 系列 ...
- [转载]C/C++框架和库
C/C++框架和库 装载自:http://blog.csdn.net/xiaoxiaoyeyaya/article/details/42541419 值得学习的C语言开源项目 Webbench Web ...
- C++ 资源大全
http://www.uml.org.cn/c++/201411145.asp http://ezlippi.com/blog/2014/12/c-open-project.html <C++ ...
- 最全面的 C++ 资源、框架大全
转载自 http://www.codeceo.com/article/cpp-resource-framework.html#0-tsina-1-99850-397232819ff9a47a7b7 ...
- 1.值得推荐的C/C++框架和库 (转)
值得学习的C语言开源项目 - 1. Webbench Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的 ...
随机推荐
- Wireshark分析器分析数据流过程
Wireshark分析器分析数据流过程 分析包是Wireshark最强大的功能之一.分析数据流过程就是将数据转换为可以理解的请求.应答.拒绝和重发等.帧包括了从捕获引擎或监听库到核心引擎的信息.Wir ...
- LightOJ1105 Fi Binary Number(数位DP)
题目要求第k个没有连续两个1的二进制数. 这算数位DP吧,只不过以前遇到的是统计区间的数字情况,而这题是求第几个数字,差不多是反过来的. 本来我想用状态dp[i][0/1]表示长度i末尾0或1的二进制 ...
- 导出一个EXCEL,多个SHEET
Infragistics.Excel. Workbook work = new Infragistics.Excel.Workbook(); Infra ...
- Html5的DeviceOrientation特性
设备定位API 引用W3C中的设备定位API的规范描述可知,该API“……定义了多种新型DOM事件,旨在提供与主机设备相关的物理朝向与运动状态信息.”由API提供的数据产生自多种来源,其中包括设备上的 ...
- ibatis插入数据返回ID的方法
ibatis插入数据返回ID的方法 主要就是利用seelctkey来获取这个ID值,但是oracle和mysql的区别还是很大的 oracle的用法 <insert id="inser ...
- juniper防火墙 L2TP VPN配置
juniper防火墙 L2TP VPN配置 建立L2TP_POOL 创建连接的用户: 创建用户组: 更改L2TP的连接池: 更改L2TP的隧道: 设置防火墙的策略: Win7连接:
- asp.net 页面过程
- Java泛型总结
1. 什么是泛型?泛型(Generic type 或者 generics)是对 Java 语言的类型系统的一种扩展,以支持创建可以按类型进行参数化的类.可以把类型参数看作是使用参数化类型时指定的类型的 ...
- Oracle 10g安装64位图解流程
1. 安装准备阶段 1.1 安装Oracle环境 本例使用X-Manager来实现与Linux系统的连接,本例使用的所有命令和操作都是在X-Manager下进行.X-Manager安装完成后的配置方法 ...
- slf4j提示Class path contains multiple SLF4J bindings
报错: SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding .jar!/org/slf4j/impl/St ...