[库][c++]tinyxml2使用小结
参考:http://blog.csdn.net/educast/article/details/12908455
1.配置TinyXML2
去这里把项目弄下来,然后解压,我们之需要里面的tinyxml2.h和tinyxml2.cpp,将他们拷到工程目录里面。
2.HelloWorld
在项目中创建test.xml,内容如下:
- <?xml version="1.0"?>
- <Hello>World</Hello>
创建main.cpp
- #include <iostream>
- #include"tinyxml2.h"
- using namespace std;
- using namespace tinyxml2;
- void example1()
- {
- XMLDocument doc;
- doc.LoadFile("test.xml");
- const char* content= doc.FirstChildElement( "Hello" )->GetText();
- printf( "Hello,%s", content );
- }
- int main()
- {
- example1();
- return 0;
- }
编译运行:

3.稍微复杂一些的例子
下面这个例子的场景更可能在工程中遇到,就是在XML中存储一些数据,然后由程序来调用。
- <?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"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()
- {
- example1();
- return 0;
- }
运行结果

解释一下几个函数:
FirstChildElement(const char* value=0):获取第一个值为value的子节点,value默认值为空,则返回第一个子节点。
RootElement():获取根节点,相当于FirstChildElement的空参数版本;
const XMLAttribute* FirstAttribute() const:获取第一个属性值;
XMLHandle NextSiblingElement( const char* _value=0 ) :获得下一个节点。
[库][c++]tinyxml2使用小结的更多相关文章
- [原][库][c++]tinyxml使用小结
参考:http://blog.csdn.net/L_Andy/article/details/40615517 tinyxml官网: http://www.grinninglizard.com/tin ...
- C语言库在不同系统下的后缀
C语言的静态库与动态库对比分析,各有长短 库: 指由标准常用函数编译而成的文件,旨在提高常用函数的可重用性,减轻开发人员负担.常用的sdtio.h,math.h等 库 ...
- 055 Python第三方库安装
目录 一.概述 二.看见更大的Python世界 2.1 Python社区 2.1.1 PyPI 2.1.2 实例:开发与区块链相关的程序 2.2 安装Python第三方库 三.第三方库的pip安装方法 ...
- C++标准库的初探
1,操作符 << 的原生意义是按位左移,例: 1 << 2; 其底层的意义是将整数 1 按位左移 2 位,即: 0000 0001 ==> 0000 0100: 2,重 ...
- linux专题一之文件管理(目录结构、创建、查看、删除、移动)
在linux系统中一切都是文件./ 在linux中为根目录,是一切文件的根目录.本文将通过linux系统的目录结构和与linux文件操作有关的相关命令(touch.mkdir.cp.mv.mv.les ...
- C++自定义修饰键,实现如<Capslock+J>等组合键的按键映射
前:所谓修饰键,就是Ctrl,Alt,Shift,Win这些按键. Update: 我使用AHK写了一个功能更丰富的脚本:https://github.com/h46incon/ModifierCus ...
- libevent源码深度剖析
原文地址: http://blog.csdn.net/sparkliang/article/details/4957667 第一章 1,前言 Libevent是一个轻量级的开源高性能网络库,使用者众多 ...
- 《C程序设计语言》【PDF】下载链接:
<C程序设计语言>[PDF]下载 https://u253469.pipipan.com/fs/253469-230382180 内容简介 在计算机发展的历史上,没有哪一种程序设计语言像C ...
- 详解C程序编译、链接与存储空间布局
被隐藏了的过程 现如今在流行的集成开发环境下我们很少需要关注编译和链接的过程,而隐藏在程序运行期间的细节过程可不简单,即使使用命令行来编译一个源代码文件,简单的一句"gcc hello.c& ...
随机推荐
- mysql数据库环境配置中部分问题解决办法
注:原文地址:https://www.cnblogs.com/hezhuoheng/p/9366630.html 其中最重要的,是三个原则:命令按顺序输入.删除了ini(这个不是原则,是我解决问题的一 ...
- 第一章SpringBoot入门
一.简介 SpringBoot来简化Spring应用的开发,约定大于配置,去繁从简,just run就能创建一个独立的产品级别的应用. 背景: j2EE笨重的开发方法,繁多的配置,低下的开发效率,复杂 ...
- 209. Minimum Size Subarray Sum(双指针)
Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...
- lnmp搭建环境易错误点
1.虚拟主机使用桥接网络 2.nginx配置server 3.先ping通,service iptables stop 4.php-fpm开启,使之能正确解析php文件,nginx开启 5.mysql ...
- 怎么查看CI,codeigniter的版本信息?想看某个项目中使用的CI具体是哪个版本,怎么查看?
怎么查看CI的版本信息?想看某个项目中使用的CI具体是哪个版本,怎么查看?system\core\codeigniter.php中可以查看版本常量/** * CodeIgniter Version * ...
- tomcat 9.0.4 性能调优
参考了网上的一些优化参数,但是在启动中发现 有2个报错: 11-Feb-2018 15:57:23.293 警告 [main] org.apache.catalina.startup.SetAllPr ...
- python之路----验证客户端合法性
验证客户端链接的合法性 import os import hmac import socket secret_key = b'egg' sk = socket.socket() sk.bind(('1 ...
- 解析分布式锁之Zookeeper实现(一)
实现分布式锁目前有三种流行方案,分别为基于数据库.Redis.Zookeeper的方案,本文主要阐述基于Zookeeper的分布式锁,其他两种会在后文中一起探讨.现在我们来看下使用Zookeeper如 ...
- 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 ...
- bzoj1704 / P2882 [USACO07MAR]面对正确的方式Face The Right Way
P2882 [USACO07MAR]面对正确的方式Face The Right Way $n<=5000$?枚举翻转长度,顺序模拟就ok了 对于每次翻转,我们可以利用差分的思想,再搞搞前缀和. ...