-----------------------------------------------------------------2015年7月21日16:37:53------------------------------------------------

今天在使用tinyxml2时,遇到一个问题,就是#include "tinyxml2.h"这一句一定要写在文件最开始的地方,不然会有错误,我也不知道是为什么。

----------------------------------------------------------------------------------分割线--------------------------------------------------

最近项目上需要用到XML,然后简单的学习了一下XML,在此简单描述XML中的元素解析过程,学习例子来自于

http://blog.csdn.net/educast/article/details/12908455

这里获取XML解析器的文件,我们只需要tinyxml2.h和tinyxml2.cpp,将他们拷到工程目录里面。

---------------------------------------------------------------------------分割线2015年10月8日14:42:49--------------------------------------------------------------

打开文件:

tinyxml2::XMLDocument *doc = new tinyxml2::XMLDocument();
tinyxml2::XMLError eRet = doc->LoadFile(m_Path.c_str());
if (tinyxml2::XML_NO_ERROR != eRet)
{
cout <<"XML File Error\n";
}

1.XML元素内容的获取

创建一个简单的xml文件

 <?xml version="1.0"?>
<Hello>
World
</Hello>

然后编写程序获取xml元素内容。

 #include <iostream>
#include <fstream>
#include "tinyxml2.h"
using namespace tinyxml2;
using namespace std; void example1()
{
XMLDocument doc;
doc.LoadFile("test.xml"); const char* content= doc.FirstChildElement( "Hello" )->GetText();
cout << content <<endl;
} int main()
{
example1(); return ;
}

注意:XML文件中不同的书写格式会输出不同的元素内容格式,比如如下所示:

2.复杂一点的例子

 <?xml version="1.0"?>
<scene name="Depth">
<node type="camera">
<eye>0 10 10</eye>
<front>0 0 -1</front>
<refUp>0 1 0</refUp>
<fov>90</fov>
</node>
<node type="Sphere">
<center>0 10 -10</center>
<radius>10</radius>
</node>
<node type="Plane">
<direction>0 10 -10</direction>
<distance>10</distance>
</node>
</scene>
 #include <iostream>
#include <fstream>
#include "tinyxml2.h"
using namespace tinyxml2;
using namespace std; #include <iostream>
#include"tinyxml2.h"
using namespace std;
using namespace tinyxml2;
void example2()
{
XMLDocument doc;
doc.LoadFile("test.xml");
XMLElement *scene=doc.RootElement();
XMLElement *surface=scene->FirstChildElement("node");
while (surface)
{
XMLElement *surfaceChild=surface->FirstChildElement();
const char* content;
const XMLAttribute *attributeOfSurface = surface->FirstAttribute();
cout<< attributeOfSurface->Name() << ":" << attributeOfSurface->Value() << endl;
while(surfaceChild)
{
content=surfaceChild->GetText();
surfaceChild=surfaceChild->NextSiblingElement();
cout<<content<<endl;
}
surface=surface->NextSiblingElement();
}
}
int main()
{
example2();
return ;
}

--------------------------------------------------分割线 2015年6月4日10:54:02--------------------------------------------------------------------------

现有如下xml的内容,需要将其中的maxvalue存放在一个map maxValue中,minvalue存放在一个map minValue中,实现该功能的c++代码如下:

xml:

<maxvalue>
<item name="age1" value = "100"></item>
<item name="age3" value = "80"></item>
<item name="age5" value = "70"></item>
</maxvalue> <minvalue>
<item name="age1" value = "20"></item>
<item name="age2" value = "20"></item>
<item name="age3" value = "20"></item>
<item name="age5" value = "20"></item>
</minvalue>

c++:

     tinyxml2::XMLElement* t_myEle = root->FirstChildElement("maxvalue");//直接读取root节点的子节点中叫maxvalue的节点
t_myEle=t_myEle->FirstChildElement(); string t_first;
double t_second;
const char* name;
while (t_myEle)
{ name = t_myEle->Attribute("name");
t_first.assign(name,strlen(name));
t_second=t_myEle->DoubleAttribute("value");
maxValue.insert(pair<string,double>(t_first,t_second));
t_myEle=t_myEle->NextSiblingElement();
} t_myEle = root->FirstChildElement("minvalue");
t_myEle=t_myEle->FirstChildElement(); while (t_myEle)
{ name = t_myEle->Attribute("name");
t_first.assign(name,strlen(name));
t_second=t_myEle->DoubleAttribute("value");
minValue.insert(pair<string,double>(t_first,t_second));
t_myEle=t_myEle->NextSiblingElement();
}

TinyXML2的使用的更多相关文章

  1. TinyXML2读取和创建XML文件 分类: C/C++ 2015-03-14 13:29 94人阅读 评论(0) 收藏

    TinyXML2是simple.small.efficient C++ XML文件解析库!方便易于使用,是对TinyXML的升级改写!源码见本人上传到CSDN的TinyXML2.rar资源:http: ...

  2. tinyxml2简单使用

    引入头文件 <span style="font-size:18px;">#include "HelloWorldScene.h" #include ...

  3. cocos2d-x使用tinyxml2存储解析xml

    我用的是2.1.4的cocos2d-x,里面自带有tinyxml2库. 导入头文件:#include "support/tinyxml2/tinyxml2.h" using nam ...

  4. TinyXml和tinyxml2

    C++操作xml没有标准库的支持,TinyXml是个不错的xml操作库,以前总是使用TinyXml读写xml,但是最近对大量xml进行读写时,速度真的是有点慢,特别是在调试时,每次启动读xml就要好长 ...

  5. 7.数据本地化CCString,CCArray,CCDictionary,tinyxml2,写入UserDefault.xml文件,操作xml,解析xml

     数据本地化 A CCUserDefault 系统会在默认路径cocos2d-x-2.2.3\projects\Hello\proj.win32\Debug.win32下生成一个名为UserDef ...

  6. 用TinyXml2读取XML文件的一个简单Demo

    废话少说直接上代码,需要的人自然一看便懂,对于第一次接触TinyXml2的人来说还是有帮助的. <?xml version="1.0"?> <Table name ...

  7. tinyxml2库的使用--MFC工程

    在编写应用程序的时候,经常需要动态加载某些数据,这种情况下微软的ini文件是蛮好的选择,但是平台的通用性比较差,使用xml的话就比较强一点,但是解析比较复杂,型号有牛人已经开发出了直接读写xml的库, ...

  8. 推荐一个优秀的c++源代码,TinyXml2

    项目主页:http://grinninglizard.com/tinyxml2docs/index.html tinyxml2.h /* Original code by Lee Thomason ( ...

  9. Windows10 VS2017 C++ xml解析(tinyxml2库)

    首先下载tinyxml2 7.0.1库: https://github.com/leethomason/tinyxml2/releases 打开tinyxml2,然后升级sdk,解决方案->重定 ...

随机推荐

  1. 研磨SpringCloud系列(一)第一个Spring Boot应用

    在此之前,给大家推荐几个东西. STS,Spring官方基于eclipse做的扩展ide.Spring官方背书. 第二个,lombok,注解生成get/set,构造以及基本方法的插件,"隐藏 ...

  2. 深入理解计算机系统chapter1

    ---恢复内容开始--- 预处理器+编译器+汇编器+链接器=编译系统 运行hello程序 操作系统: 无论是在单核还是多核系统中,一个CPU看上去都在并发的执行多个进程,这是通过处理器在进程间切换来实 ...

  3. EF 6.0

    最近又开始研究EF框架了 哎 搞的东西太杂了 网上的参考了一篇博客 但是他是基于EF 4.0之前做的 所以自己基于他的博客来构造EF 6.0的使用基础 命名空间不同: 旧版本:using System ...

  4. Python数据分析(二): Numpy技巧 (4/4)

    numpy.pandas.matplotlib(+seaborn)是python数据分析/机器学习的基本工具. numpy的内容特别丰富,我这里只能介绍一下比较常见的方法和属性.   第一部分: ht ...

  5. 自测-5 Shuffling Machine

    Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techn ...

  6. Zabbix(一) : 简介以及Server端安装

    一.什么是Zabbix? zabbix由AlexeiVladishev首先开发,目前在维护的是Zabbix SIA.ZABBIX是一个企业级的开源分布式监控解决方案. zabbix为监控网络和服务器的 ...

  7. 获取报告 Stream转string,利用字符串分割转换成DataTable

    protected void Button1_Click(object sender, EventArgs e) { MemoryStream stream = new MemoryStream(); ...

  8. Linux基础命令讲解(二)

    Linux命令基本格式: 命令 [参数] [路径文件] 方括号内容可省略 查看命令帮助手段: 1 man 命令名 (man 还可以获取配置文件,函数的帮助) 2 命令 --help 3 help 命令 ...

  9. win10 uwp Window.Current.Dispatcher中Current为null

    本文说的是进行网络中异步界面出现的错误,可能带有一定的主观性和局限性,说的东西可能不对或者不符合每个人的预期.如果觉得我有讲的不对的,就多多包含,或者直接关掉这篇文章,但是请勿生气或者发怒吐槽,可以在 ...

  10. Ubuntu软件中心卡在正在应用更改的解决办法

    http://forum.ubuntu.org.cn/viewtopic.php?t=374037 http://forum.ubuntu.org.cn/viewtopic.php?p=2743994 ...