参考文章: 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的简单应用的更多相关文章

  1. 开源TinyXML 最简单的新手教程

    TinyXML它是基于一个非常受欢迎的现在DOM型号XML解析器,简单易用且小巧玲珑,很适合存储简单数据.配置文件. 该项目属于开源项目,在sourceforge上边的链接是:http://sourc ...

  2. TinyXML 的简单介绍以及使用

    先说几句重点: 1,tinyxml 生成或解析XML非常好用 2,tinyxml 利用DOM(文档对象模型)操作XML,根节点与各个子节点相当于形成一棵树 3,只要你了解tinyxml的用法,可以只n ...

  3. TinyXML用法小结2

    参考:http://www.cnblogs.com/hgwang/p/5833638.html TinyXML用法小结 1.      介绍 Tinyxml的官方网址:http://www.grinn ...

  4. 值得推荐的C/C++框架和库

    值得推荐的C/C++框架和库 [本文系外部转贴,原文地址:http://coolshell.info/c/c++/2014/12/13/c-open-project.htm]留作存档 下次造轮子前先看 ...

  5. [转]C/C++ 程序员必须收藏的资源大全

    from: https://github.com/jobbole/awesome-cpp-cn C++ 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome – XXX 系列 ...

  6. [转载]C/C++框架和库

    C/C++框架和库 装载自:http://blog.csdn.net/xiaoxiaoyeyaya/article/details/42541419 值得学习的C语言开源项目 Webbench Web ...

  7. C++ 资源大全

    http://www.uml.org.cn/c++/201411145.asp http://ezlippi.com/blog/2014/12/c-open-project.html <C++ ...

  8. 最全面的 C++ 资源、框架大全

    转载自   http://www.codeceo.com/article/cpp-resource-framework.html#0-tsina-1-99850-397232819ff9a47a7b7 ...

  9. 1.值得推荐的C/C++框架和库 (转)

    值得学习的C语言开源项目 - 1. Webbench Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的 ...

随机推荐

  1. HDU2296 Ring(AC自动机+DP)

    题目是给几个带有价值的单词.而一个字符串的价值是 各单词在它里面出现次数*单词价值 的和,问长度不超过n的最大价值的字符串是什么? 依然是入门的AC自动机+DP题..不一样的是这题要输出具体方案,加个 ...

  2. Origami

    Origami 是一个来自 Facebook 设计团队的作品,是 Quartz Composer 的免费工具包,可在无需编程的情况下轻松实现与设计原型进行交互.

  3. JAVA发送邮件工具类

    import java.util.Date;import java.util.Properties; import javax.mail.BodyPart;import javax.mail.Mess ...

  4. 利用Oracle的row_number() over函数消除重复的记录

    .select d.id,d.outer_code from dict_depts_source d order by outer_code(查看重复数据) .select d.id,d.outer_ ...

  5. PCL 1.6.0 VS2010 Configuration

    Open VS2010, create a new project, then open Property Manager, double-click Microsoft.Cpp.win32.user ...

  6. 八、job管理

    查看用法: [root@super65 ~]# salt-run -d|grep jobs'jobs.active:' #查看当前执行的job Return a report on all activ ...

  7. 什么情况下include_path不起作用?

    include_path是怎么起作用的? 如果有多个include_path顺序是怎么样的? 什么情况下include_path不起作用? 今天, 我就全面的介绍下这个问题, 先从一个例子开始吧. 如 ...

  8. [转].net 缩略图方法

    本文转自:http://www.cnblogs.com/promic/archive/2010/04/21/1717190.html private static string strConnect ...

  9. 分布式架构高可用架构篇_08_MyCat在MySQL主从复制基础上实现读写分离

    参考: 龙果学院http://www.roncoo.com/share.html?hamc=hLPG8QsaaWVOl2Z76wpJHp3JBbZZF%2Bywm5vEfPp9LbLkAjAnB%2B ...

  10. HDU 1069 Monkey and Banana(二维偏序LIS的应用)

    ---恢复内容开始--- Monkey and Banana Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...