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,测试网站在压力下工作的 ...
随机推荐
- 递推DP URAL 1353 Milliard Vasya's Function
题目传送门 /* 题意:1~1e9的数字里,各个位数数字相加和为s的个数 递推DP:dp[i][j] 表示i位数字,当前数字和为j的个数 状态转移方程:dp[i][j] += dp[i-1][j-k] ...
- 【BZOJ】1503: [NOI2004]郁闷的出纳员(Splay)
http://www.lydsy.com/JudgeOnline/problem.php?id=1503 这题没有看题解就1a了-好开心,, 其实后面去看题解发现他们的都很麻烦,其实有种很简单的做法: ...
- ASP.NET C# 日期 时间 年 月 日 时 分 秒 格式及转换(转自happymagic的专栏)
在平时编码中,经常要把日期转换成各种各样的形式输出或保持,今天专门做了个测试,发现DateTime的ToString()方法居然有这么多的表现形式,和大家一起分享. DateTime time=Dat ...
- 关于HTML条件注释你可能不知道的一些事儿
最近经常看到类似这样的HTML代码片段,很多前端开发人员应该都熟悉: 1 <!--[if lt IE 7]> <html class="ie6"> ...
- php 经典分页(推荐和laypage配合)
学习地址:http://www.imooc.com/video/2463 <?php //(ps:推荐使用laypage整站式跳转来渲染分页按钮样式比较舒服http://laypage.layu ...
- CSS3:渐变大全
渐变大全 声明 最后的老写法镜像渐变可能不太准确.其余都完全正确 <!DOCTYPE html> <html> <head> <meta http-equiv ...
- jQuery EasyUI DataGrid Checkbox 数据设定与取值
纯粹做个记录,以免日后忘记该怎么设定. 这一篇将会说明两种使用 jQuery EasyUI DataGrid 的 Checkbox 设定方式,以及在既有数据下将 checked 为 true 的该笔数 ...
- The content of element type "configuration" must match "(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?...
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC & ...
- avira更新时候安装了launcher
用不上红伞其他的软件,一个登录选择器就显得多余.可能是长时间没用电脑,更新给装上了启动器. 本文以时间顺序而记录 ------------------------------------------- ...
- PHP 错误与异常 笔记与总结(15 )使用观察者模式处理异常信息
使异常处理变得更灵活.可观察,可以使用设计模式中的观察者模式. 文件 ① 定义观察者的接口 ExceptionObserver.php: <?php /* 给观察者定义的规范 */ interf ...