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

1.配置TinyXML2

这里把项目弄下来,然后解压,我们之需要里面的tinyxml2.h和tinyxml2.cpp,将他们拷到工程目录里面。

2.HelloWorld

在项目中创建test.xml,内容如下:

  1. <?xml version="1.0"?>
  2. <Hello>World</Hello>

创建main.cpp

  1. #include <iostream>
  2. #include"tinyxml2.h"
  3. using namespace std;
  4. using namespace tinyxml2;
  5. void example1()
  6. {
  7. XMLDocument doc;
  8. doc.LoadFile("test.xml");
  9. const char* content= doc.FirstChildElement( "Hello" )->GetText();
  10. printf( "Hello,%s", content );
  11. }
  12. int main()
  13. {
  14. example1();
  15. return 0;
  16. }

编译运行:

3.稍微复杂一些的例子

下面这个例子的场景更可能在工程中遇到,就是在XML中存储一些数据,然后由程序来调用。

  1. <?xml version="1.0"?>
  2. <scene name="Depth">
  3. <node type="camera">
  4. <eye>0 10 10</eye>
  5. <front>0 0 -1</front>
  6. <refUp>0 1 0</refUp>
  7. <fov>90</fov>
  8. </node>
  9. <node type="Sphere">
  10. <center>0 10 -10</center>
  11. <radius>10</radius>
  12. </node>
  13. <node type="Plane">
  14. <direction>0 10 -10</direction>
  15. <distance>10</distance>
  16. </node>
  17. </scene>
  1. #include <iostream>
  2. #include"tinyxml2.h"
  3. using namespace std;
  4. using namespace tinyxml2;
  5. void example2()
  6. {
  7. XMLDocument doc;
  8. doc.LoadFile("test.xml");
  9. XMLElement *scene=doc.RootElement();
  10. XMLElement *surface=scene->FirstChildElement("node");
  11. while (surface)
  12. {
  13. XMLElement *surfaceChild=surface->FirstChildElement();
  14. const char* content;
  15. const XMLAttribute *attributeOfSurface = surface->FirstAttribute();
  16. cout<< attributeOfSurface->Name() << ":" << attributeOfSurface->Value() << endl;
  17. while(surfaceChild)
  18. {
  19. content=surfaceChild->GetText();
  20. surfaceChild=surfaceChild->NextSiblingElement();
  21. cout<<content<<endl;
  22. }
  23. surface=surface->NextSiblingElement();
  24. }
  25. }
  26. int main()
  27. {
  28. example1();
  29. return 0;
  30. }

运行结果

解释一下几个函数:

FirstChildElement(const char* value=0):获取第一个值为value的子节点,value默认值为空,则返回第一个子节点。

RootElement():获取根节点,相当于FirstChildElement的空参数版本;

const XMLAttribute* FirstAttribute() const:获取第一个属性值;

XMLHandle NextSiblingElement( const char* _value=0 ) :获得下一个节点。

[库][c++]tinyxml2使用小结的更多相关文章

  1. [原][库][c++]tinyxml使用小结

    参考:http://blog.csdn.net/L_Andy/article/details/40615517 tinyxml官网: http://www.grinninglizard.com/tin ...

  2. C语言库在不同系统下的后缀

    C语言的静态库与动态库对比分析,各有长短 库:  指由标准常用函数编译而成的文件,旨在提高常用函数的可重用性,减轻开发人员负担.常用的sdtio.h,math.h等                 库 ...

  3. 055 Python第三方库安装

    目录 一.概述 二.看见更大的Python世界 2.1 Python社区 2.1.1 PyPI 2.1.2 实例:开发与区块链相关的程序 2.2 安装Python第三方库 三.第三方库的pip安装方法 ...

  4. C++标准库的初探

    1,操作符 << 的原生意义是按位左移,例: 1 << 2; 其底层的意义是将整数 1 按位左移 2 位,即: 0000 0001  ==> 0000 0100: 2,重 ...

  5. linux专题一之文件管理(目录结构、创建、查看、删除、移动)

    在linux系统中一切都是文件./ 在linux中为根目录,是一切文件的根目录.本文将通过linux系统的目录结构和与linux文件操作有关的相关命令(touch.mkdir.cp.mv.mv.les ...

  6. C++自定义修饰键,实现如<Capslock+J>等组合键的按键映射

    前:所谓修饰键,就是Ctrl,Alt,Shift,Win这些按键. Update: 我使用AHK写了一个功能更丰富的脚本:https://github.com/h46incon/ModifierCus ...

  7. libevent源码深度剖析

    原文地址: http://blog.csdn.net/sparkliang/article/details/4957667 第一章 1,前言 Libevent是一个轻量级的开源高性能网络库,使用者众多 ...

  8. 《C程序设计语言》【PDF】下载链接:

    <C程序设计语言>[PDF]下载 https://u253469.pipipan.com/fs/253469-230382180 内容简介 在计算机发展的历史上,没有哪一种程序设计语言像C ...

  9. 详解C程序编译、链接与存储空间布局

    被隐藏了的过程 现如今在流行的集成开发环境下我们很少需要关注编译和链接的过程,而隐藏在程序运行期间的细节过程可不简单,即使使用命令行来编译一个源代码文件,简单的一句"gcc hello.c& ...

随机推荐

  1. unity3d-准备工作

    1.软件下载 想进行unity3d游戏开发,首先unity3d官网下载软件:unity3d下载 2.运行游戏界面 3.安装Visual Studio Tools for Unity unity3d自带 ...

  2. 7.10 Models -- Handling Metadata(处理元数据)

    1. 随着从store中返回的records,你可能需要处理一些元数据.Metadata是伴随着特定model或者type的一种数据,而不是record. 2. 分页是使用元数据的一个常见的例子.想象 ...

  3. Summary: Difference between null and empty String

    String s1 = ""; means that the empty String is assigned to s1. In this case, s1.length() i ...

  4. 查看mysql主外键信息

    SELECT  *FROMinformation_schema.key_column_usage tWHERE t.constraint_schema = '库名称'AND t.constraint_ ...

  5. MFC工具栏的创建、设计与使用实例

    本文通过实例说明MFC工具栏的创建.设计和使用方法,包括三个demo.       demo1:创建一个工具栏 C++代码 //摘抄自MSDN demo1 (创建一个工具栏) 1.Create a t ...

  6. Excel脱拽或者下拉公式时, 保持公式里单元格数字不变

    绝对引用 可以选中B1 用F4快捷键自己就给加绝对引用符号了 然后回车 复制或者拖拽

  7. python 线程、多线程

    复习进程知识: python:主进程,至少有一个主线程 启动一个新的子进程:Process,pool 给每一个进程设定一下执行的任务:传一个函数+函数的参数 如果是进程池:map函数:传入一个任务函数 ...

  8. SNMP学习笔记之SNMP TRAP简介、流程以及使用Python实现接受Trap信息

    0x00 SNMP TRAP简介 SNMP(Simple Network Management Protocol) trap是一种很有用,但是也容易让人难以理解的协议. 虽然名字叫做简单网络管理协议, ...

  9. c++标准库多线程入门

    从c++ 11开始,语言核心和标准库开始引入了对多线程的原生支持.如下所示: int doSth(char c) { default_random_engine dre(c); uniform_int ...

  10. c++获取时间戳

    vc获取时间戳的代码如下: SYSTEMTIME st; }; GetLocalTime(&st); // vc专用 _snprintf_s(ts, sizeof(ts), "%4d ...