参考文章: 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. Hadoop_10_shuffle02_详解Shuffle过程【来源网络】推荐更为详细

    网址:http://www.cnblogs.com/felixzh/p/4680808.html Shuffle过程,也称Copy阶段.reduce task从各个map task上远程拷贝一片数据, ...

  2. 转:.NET获取当前方法名或调用此方法的方法名

    Introduction Before .NET, we were always looking for a way to log current method name in a log file ...

  3. 【转】包管理器Bower详细讲解

      包管理器Bower   今天自己用Angular写东西的时候,下载了Angular-seed项目,发现需要用到bower,之前也使用过,没有仔细了解,今天趁机了解到一些. bower的官网地址:  ...

  4. cocos3 singleton

    class TestSingleton : public CCLayer { public: static TestSingleton* getInstance();//创建一个全局访问点,例如我们常 ...

  5. BZOJ4303 : 数列

    将每个点看成二维坐标点$(i,a_i)$,那么每次操作的范围都是一个矩形. 于是建立KD-Tree,通过打标记支持操作即可. 时间复杂度$O(m\sqrt{n})$. #include<cstd ...

  6. TYVJ P1002 谁拿了最多奖学金 Label:模拟 水

    背景 NOIP2005复赛提高组第一题 描述 某校的惯例是在每学期的期末考试之后发放奖学金.发放的奖学金共有五种,获取的条件各自不同:1)     院士奖学金,每人8000元,期末平均成绩高于80分( ...

  7. C#时间格式化(Datetime)用法详解

    Datetime.ToString(String, IFormatProvider) 参数format格式详细用法: 格式字符 关联属性/说明 d ShortDatePattern D LongDat ...

  8. 关于EnumerateObjectsUsingBlock和for-in之间的较量

      遍历一个数组看谁快 参赛选手 ForLoop, For - in, enumerateObjectsUsingBlock这个三个方法: NSMutableArray *test = [NSMuta ...

  9. 关于Stock Quant与生产内容的理解

    Stock Quant对象是8.0中新增加的内容,主要用于核算库存,与Stock Move相关联,没个Stock Move动作都会关联一个或者多个Stock Quant对象.库存移动的过程实际上就是将 ...

  10. uva729

    /*题目一大堆,其实意思就是长度为n个二进制数,里面有h个1,将这个二进制数进行全排列,然后输出*/ #include"iostream" #include"algori ...