-----------------------------------------------------------------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. String类的源码分析

    之前面试的时候被问到有没有看过String类的源码,楼主当时就慌了,回来赶紧补一课. 1.构造器(构造方法) String类提供了很多不同的构造器,分别对应了不同的字符串初始化方法,此处从源码中摘录如 ...

  2. DeepLearning.ai学习笔记(二)改善深层神经网络:超参数调试、正则化以及优化--Week2优化算法

    1. Mini-batch梯度下降法 介绍 假设我们的数据量非常多,达到了500万以上,那么此时如果按照传统的梯度下降算法,那么训练模型所花费的时间将非常巨大,所以我们对数据做如下处理: 如图所示,我 ...

  3. POJ3069(贪心+巧用优先队列)

    题目传送门:http://poj.org/problem?id=3069 题目大意:一个直线上有N个点.点i的距离是Xi.从这些点中选取若干个加上标记.要求:对于每个点,与其距离为R的范围内必有做标记 ...

  4. BZOJ-1012-[JSOI2008]最大数maxnumber(线段树)

    Description 现在请求你维护一个数列,要求提供以下两种操作:1. 查询操作.语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度.2. 插 ...

  5. yum软件管理器,及yum源配置

    说到yum源就必须说到linux系统中特有的依赖关系问题,yum就是为了解决依赖关系而存在的.yum源就相当是一个目录项,当我们使用yum机制安装软件时,若需要安装依赖软件,则yum机制就会根据在yu ...

  6. 均值滤波去除图像噪声的matlab程序

    所谓均值滤波实际上就是用均值替代原图像中的各个像素值. 均值滤波的方法是:对待处理的当前像素,选择一个模板,该模板为其近邻的若干像素组成,用模板中的像素的均值来替代原像素. 优点:算法简单,计算速度快 ...

  7. Greatest Common Increasing Subsequence hdu1423

    Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536 ...

  8. libsvn_subr-1.so.0: undefined symbol: apr_atomic_xchgptr 故障解决

    源码编译安装完成之后,查看svn的安装版本会报以下错误 svn: symbol lookup error: /usr/local/subversion/lib/libsvn_subr-.so.: un ...

  9. webpack2使用ch10-处理图片(png jpg svg 等) 限制图片 压缩图片

    1 目录展示 安装依赖 "file-loader": "^0.11.1", "image-webpack-loader": "^3 ...

  10. 通用table样式

    <html> <head> <title>通用table样式</title> <style type="text/css"&g ...