// XMLT01.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include "tinyxml2.h"

using namespace std;
using namespace TinyXml2;

void ReadTest01XML()
{
 XMLDocument doc;
 doc.LoadFile("Test01.xml");
 const char * content = doc.FirstChildElement("test")->GetText();
 printf("%s\n",content);
}

void Printfln(const char * content, const char * name = "",const int n = 0, const char * notEqual1 = "", const char * notEqual2 = NULL)
{
 for(int i = 0; i < n; i++)
 {
  printf("    ");
 }
 if(content != notEqual1 && content != notEqual2)
 {
  printf("%s: %s\n",name,content);
 }
 else
 {
  printf("%s: \n",name);
 }
}

void ReadXML(const XMLElement *root)
{
 if(NULL == root)
 {
  return;
 }

static int flag = 0;

const char * rootName = NULL;
 const char * rootContent = NULL;
 const XMLAttribute  * rootAttribute = NULL;
 const char * rootAttributeName = NULL;
 const char * rootAttributeValue = NULL;
 rootName = root->Name();
 rootContent = root->GetText();
 rootAttribute = root->FirstAttribute();
 if(NULL != rootAttribute)
 {
  rootAttributeName = rootAttribute->Name();
  rootAttributeValue = rootAttribute->Value();
 }
 Printfln(rootContent,rootName,flag);
 Printfln(rootAttributeValue,rootAttributeName,flag);

const XMLElement *child = root->FirstChildElement();
 if(NULL != child)
 {
  flag++;
  ReadXML(child);
 }

const XMLElement * nextSibling = root->NextSiblingElement();
 if(NULL != nextSibling)
 {
  ReadXML(nextSibling);
 }
 else
 {
  flag--;
  return;
 }
}

void ReadTest02XML()
{
 XMLDocument doc;
 doc.LoadFile("Test02.xml");
 const XMLElement *root = doc.RootElement();

ReadXML(root);

system("pause");
}

int _tmain(int argc, _TCHAR* argv[])
{
 ReadTest01XML();
 ReadTest02XML();
 return 0;
}

C++:借助tinyxml2读取XML文件的更多相关文章

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

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

  2. C#中常用的几种读取XML文件的方法

    1.C#中常用的几种读取XML文件的方法:http://blog.csdn.net/tiemufeng1122/article/details/6723764/

  3. 读取xml文件报错:Invalid byte 2 of 2-byte UTF-8 sequence。

    程序读取xml文件后,系统报“Invalid byte 2 of 2-byte UTF-8 sequence”错误,如何解决呢? 1.程序解析xml的时候,出现Invalid byte 2 of 2- ...

  4. C#读取XML文件的基类实现

    刚到新单位,学习他们的源代码,代码里读写系统配置文件的XML代码比较老套,直接写在一个系统配置类里,没有进行类的拆分,造成类很庞大,同时,操作XML的读写操作都是使用SetAttribute和node ...

  5. C#读取XML文件的方法

    先写一个xml文件: <?xml version="1.0" encoding="utf-8" ?> <bookste> <!-- ...

  6. Spring相关:jdom学习:读取xml文件

    云课堂马士兵的spring2.5课程中提到的 用JDOM读取XML文件需先用org.jdom.input.SAXBuilder对象的build()方法创建Document对象,然后用Document类 ...

  7. 在C#中创建和读取XML文件

    1.创建简单的XML文件 为了便于测试,我们首先创建控制台应用程序,项目命名为CreateXml,Program.cs代码如下: 这样会在C盘根目录下创建data2.xml文件,文件内容为 using ...

  8. Java获取路径方法&相对路径读取xml文件方法

    (1).request.getRealPath("/");//不推荐使用获取工程的根路径 (2).request.getRealPath(request.getRequestURI ...

  9. python读取xml文件

    关于python读取xml文章很多,但大多文章都是贴一个xml文件,然后再贴个处理文件的代码.这样并不利于初学者的学习,希望这篇文章可以更通俗易懂的教如何使用python 来读取xml 文件. 什么是 ...

随机推荐

  1. 第九章 LinkedBlockingQueue源码解析

    1.对于LinkedBlockingQueue需要掌握以下几点 创建 入队(添加元素) 出队(删除元素) 2.创建 Node节点内部类与LinkedBlockingQueue的一些属性 static ...

  2. [转]VS2010快捷键

    VS2010的快捷键,后附VS2008的快捷键 Ctrl+E,D ----格式化全部代码 Ctrl+E,F ----格式化选中的代码 CTRL + SHIFT + B生成解决方案 CTRL + F7 ...

  3. WebSettings 文档 API 翻译 常用设置

    . setDefaultFontSize(int size)  Sets the default font size. The default is 16. setDefaultTextEncodin ...

  4. 不要再使用JS框架了

    停止编写Javascript框架吧. Javascript框架就好像死亡和税收一样:终究不可避免它的存在.我确信如果我是那面墙上的一只苍蝇,每次有人开始一个新的网页项目时,第一个问题肯定是我们用的是哪 ...

  5. C# 音频操作系统项目总结

    此项目需求是针对.wav格式音频进行操作,转换成相应的.mp3格式的音频文件,对音频进行切割,最后以需求的形式输出,此篇会回顾运用到的一些知识点. 1.MDI子窗口的建立: 首先一个窗体能够创建多个M ...

  6. C语言学习笔记:14_内部函数和外部函数

    /* * 14_内部函数和外部函数.c * * Created on: 2015年7月5日 * Author: zhong */ #include <stdio.h> #include & ...

  7. Nuget出现错误怎么办?

        Go to the packages folder in the Windows Explorer and delete it. Open Visual Studio and Go to To ...

  8. 用vs2013开发node.js的addon.

        下载node.js的源代码. https://github.com/joyent/node 如果用svn下载,后面加上/trunk,以免把用不着的branches也下载下来,浪费时间. 安装V ...

  9. C++的基本类型

  10. ZOJ 2319 Beautiful People

    LIS.先按S降序升序再按B降序排序(如果B不按降序排序的话就会覆盖掉正解),然后再对B用O(nlog(n))的LIS求解就可以了.用d数组标记每个元素在上升序列中的位置,然后根据d倒着找id就可以了 ...