tinyxml的用法和实例

现在很多协议都用xml来设计,方便扩展,也具有兼容性。现在流行的json和gson就不说了,应用比较多的tinyxml也是很不错的。下面介绍下tinyxml的用法和实例。

使用tinyxml,需要在工程中包含其源码,并在头文件建立引用关系。下面是一个简单的例子,按照层次关系打印出xml文件。

代码如下:

#include "stdafx.h"
#include "targetver.h"
#include "tinystr.h"
#include "SystemCommon.h"
#include "tinyxml.h"  

void ParseXML(TiXmlElement *Element); 

int _tmain(int argc, _TCHAR* argv[])
{ 

    char c[2048] =  "<!-- This is a comment -->"
                    "<?xml version=\"1.0\"  encoding=\"utf-8\"?>"
                    "<Class name=\"计算机软件班\">计算机"
                    "<Students>"
                    "<student name=\"张三\" studentNo=\"13031001\" sex=\"男\" age=\"22\">"
                    "<phone>88208888</phone>"
                    "<ET name=\"de\" ground = \"ground\">on the ground</ET>"
                    "<et name = \"et name\" ball = \"ball\"/>"
                    "<address>西安市太白南路二号</address>"
                    "</student>"
                    "<student name=\"李四\" studentNo=\"13031002\" sex=\"男\" age=\"20\">"
                    "<phone>88206666</phone>"
                    "<address>西安市光华路</address>"
                    "</student>"
                    "</Students>"
                    "</Class>";
    TiXmlDocument *myDocument = new TiXmlDocument(); 

     //假设文件名是xml.xml
    myDocument->LoadFile("xml.xml",TIXML_ENCODING_UTF8);
    //myDocument->Parse(c, 0,TIXML_ENCODING_UNKNOWN);
    TiXmlElement *rootElement = myDocument->RootElement(); 

    while(rootElement)
    {
        ParseXML(rootElement);
        rootElement = rootElement->NextSiblingElement();
    } 

    delete myDocument; 

    cout<<"----------------END-----------------"<<endl;
    return 0;
} 

void PrintTree(int c)
{
    if(c <= 0)
        return ;
    while(c)
    {
        cout<<" ";
        --c;
    }
    return;
} 

//调用 tinyxml 解析 xml
void ParseXML(TiXmlElement *pElement)
{
    static int i= 0;
    PrintTree(i);
    cout<<pElement->Value()<<" ";
    const char * str = NULL;
    if(str = pElement->GetText())
        cout<<" "<<str<<endl;
    else
        cout<<endl; 

    TiXmlAttribute* attributeOfStudent = pElement->FirstAttribute();
    while(attributeOfStudent)
    {
        PrintTree(i);
        std::cout << attributeOfStudent->Name() << " : " << attributeOfStudent->Value()<<std::endl;
        attributeOfStudent = attributeOfStudent->Next();
    }
    TiXmlElement* ChildElement = pElement->FirstChildElement();
    while(NULL != ChildElement)
    {
        i++;
        ParseXML(ChildElement);
        i--;
        ChildElement = ChildElement->NextSiblingElement();
    }
}

例如要解析的文件内容是

<?xml version="1.0" encoding="utf-8"?>
<!-- This is a comment -->
<Class name="Class of SoftWare">
    <Students>
      <student name="LiLei" studentNo="13031001" sex="Man" age="22">
        <phone>88208888</phone>
        <ET name="de" ground = "ground">on the ground
           <et name = "et name" ball = "ball"/>
        </ET>
        <address>Road1</address>
      </student>
      <student name="LiSi" studentNo="13031002" sex="Man" age="20">
        <phone>88206666</phone>
        <address>Road2</address>
      </student>
    </Students>
    <Teatcher>few teatchers
    </Teatcher>
</Class>

转载请注明原创链接:http://blog.csdn.net/wujunokay/article/details/38310691

tinyxml的用法和实例

时间: 2024-08-08 17:51:19

tinyxml的用法和实例的相关文章

c# Task.FromResult 的基本用法和实例

c#  Task.FromResult 的基本用法和实例 This method is useful when you perform an asynchronous operation that returns a Task object, and the result of that Task object is already computed There are two common use cases I've found: When you're implementing an in

Linux(centos8)下的yum的基本用法和实例

原文:Linux(centos8)下的yum的基本用法和实例 yum 命令 Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器.基于RPM包管理,能够从指定的服务器自动下载RPM包并且安装,可以自动处理依赖性关系,并且一次安装所有依赖的软件包,无须繁琐地一次次下载.安装. yum 语法 ? 1 yum [options] [command] [package ...] options:可选,选

PHP foreach()用法和实例

原网址:http://blog.csdn.net/china_skag/article/details/6444727 PHP 4 引入了 foreach 结构,和 Perl 以及其他语言很像.这只是一种遍历数组简便方法.foreach 仅能用于数组,当试图将其用于其它数据类型或者一个未初始化的变量时会产生错误.有两种语法,第二种比较次要但却是第一种的有用的扩展. foreach (array_expression as $value)    statement foreach (array_e

php foreach用法和实例

PHP 4 引入了 foreach 结构,和 Perl 以及其他语言很像.这只是一种遍历数组简便方法.foreach 仅能用于数组,当试图将其用于其它数据类型或者一个未初始化的变量时会产生错误.有两种语法,第二种比较次要但却是第一种的有用的扩展.foreach (array_expression as $value)statement foreach (array_expression as $key => $value)statement第一种格式遍历给定的 array_expression

Global.asax的用法和实例(转)

Global.asax 文件,有时候叫做 ASP.NET 应用程序文件,提供了一种在一个中心位置响应应用程序级或模块级事件的方法.你可以使用这个文件实现应用程序安全性以及其它一些任务.下面让我们详细看一下如何在应用程序开发工作中使用这个文件. 概述 Global.asax 位于应用程序根目录下.虽然 Visual Studio .NET 会自动插入这个文件到所有的 ASP.NET 项目中,但是它实际上是一个可选文件.删除它不会出问题——当然是在你没有使用它的情况下..asax 文件扩展名指出它是

Matlab 之meshgrid, interp, griddata 用法和实例(转)

http://blog.sina.com.cn/s/blog_67f37e760101bu4e.html 实例结果http://wenku.baidu.com/link?url=SiGsFZIxuS1E1VZWtixqXdjG5Y9SY4tu1W8TXgk147HDOLLCgpffjX8ywDMIH1PYkQSi5rp8gkmnMPsLhH-IUaGjMl8hsRhZQssTPmnBjdy 关于MATLAB 插值(Interpolation)http://blog.sina.com.cn/s/b

List 用法和实例(转载)

写在粘贴复制前:英文的感觉也可以,也能看的懂,多看看英文资料没坏处的 Problem. You have questions about the List collection in the .NET Framework, which is located in the System.Collections.Generic namespace. You want to see examples of using List and also explore some of the many use

C语言::模拟实现strlen函数

编写一个C语言程序模拟实现strlen函数. 算法 strlen函数功能是计算字符串中字符的个数.(除\0外) 而字符串本身就是一个字符数组,只不过末尾以\0结束. 因此,我们只需遍历除\0之外的所有字符即可. 有三种方法可以解决这个问题. 算法总结 方法一:设置一个整型计数器,遍历字符串. 方法二:通过不断函数自身的递归. 方法三:与方法一类似,设置一个char*变量标记字符串尾部,通过指针相减得到字符长度. 核心代码 //方法一:通过设置整型计数器,模拟实现strlen函数. int my_

C语言::模拟实现strcmp函数

题目要求 编写一个C语言程序模拟实现strcmp函数. (我们依然先模拟实现strcmp函数,然后再对照string.h库中strcmp函数的实现,对比与大师之间的差距.) 算法分析 通过上一篇文章:C语言::strcmp函数功能.原型.用法及实例我们获得了strcmp函数的如下信息: strcmp原型:int strcmp( const char *s1, const char *s2 ); strcmp功能:将两个字符串自左向右逐个字符进行相比(根据ASCII值大小),直到出现不同的字符或遇