参考: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. mysql数据库环境配置中部分问题解决办法

    注:原文地址:https://www.cnblogs.com/hezhuoheng/p/9366630.html 其中最重要的,是三个原则:命令按顺序输入.删除了ini(这个不是原则,是我解决问题的一 ...

  2. 第一章SpringBoot入门

    一.简介 SpringBoot来简化Spring应用的开发,约定大于配置,去繁从简,just run就能创建一个独立的产品级别的应用. 背景: j2EE笨重的开发方法,繁多的配置,低下的开发效率,复杂 ...

  3. 209. Minimum Size Subarray Sum(双指针)

    Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...

  4. lnmp搭建环境易错误点

    1.虚拟主机使用桥接网络 2.nginx配置server 3.先ping通,service iptables stop 4.php-fpm开启,使之能正确解析php文件,nginx开启 5.mysql ...

  5. 怎么查看CI,codeigniter的版本信息?想看某个项目中使用的CI具体是哪个版本,怎么查看?

    怎么查看CI的版本信息?想看某个项目中使用的CI具体是哪个版本,怎么查看?system\core\codeigniter.php中可以查看版本常量/** * CodeIgniter Version * ...

  6. tomcat 9.0.4 性能调优

    参考了网上的一些优化参数,但是在启动中发现 有2个报错: 11-Feb-2018 15:57:23.293 警告 [main] org.apache.catalina.startup.SetAllPr ...

  7. python之路----验证客户端合法性

    验证客户端链接的合法性 import os import hmac import socket secret_key = b'egg' sk = socket.socket() sk.bind(('1 ...

  8. 解析分布式锁之Zookeeper实现(一)

    实现分布式锁目前有三种流行方案,分别为基于数据库.Redis.Zookeeper的方案,本文主要阐述基于Zookeeper的分布式锁,其他两种会在后文中一起探讨.现在我们来看下使用Zookeeper如 ...

  9. MySQL Crash Course #04# Chapter 7. 8 AND. OR. IN. NOT. LIKE

    索引 AND. OR 运算顺序 IN Operator VS. OR NOT 在 MySQL 中的表现 LIKE 之注意事项 运用通配符的技巧 Understanding Order of Evalu ...

  10. bzoj1704 / P2882 [USACO07MAR]面对正确的方式Face The Right Way

    P2882 [USACO07MAR]面对正确的方式Face The Right Way $n<=5000$?枚举翻转长度,顺序模拟就ok了 对于每次翻转,我们可以利用差分的思想,再搞搞前缀和. ...