Tinyxml解析xml文件

下载地址 http://sourceforge.net/projects/tinyxml/ 官网有详细的文档以及API介绍 感觉像javaXML解析文件一样好用。

读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好。

TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。

DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。

 在TinyXML中,根据XML的各种元素来定义了一些类:

TiXmlBase:整个TinyXML模型的基类。

TiXmlAttribute:对应于XML中的元素的属性。

TiXmlNode:对应于DOM结构中的节点。

TiXmlComment:对应于XML中的注释

TiXmlDeclaration:对应于XML中的申明部分,即<?versiong="1.0" ?>。

TiXmlDocument:对应于XML的整个文档。

TiXmlElement:对应于XML的元素。

TiXmlText:对应于XML的文字部分

TiXmlUnknown:对应于XML的未知部分。 

TiXmlHandler:定义了针对XML的一些操作。

TinyXML是个解析库,主要由DOM模型类(TiXmlBase、TiXmlNode、TiXmlAttribute、TiXmlComment、TiXmlDeclaration、TiXmlElement、TiXmlText、TiXmlUnknown)和操作类(TiXmlHandler)构成。它由两个头文件(.h文件)和四个CPP文件(.cpp文件)构成,用的时候,只要将(tinyxml.h、tinystr.h、tinystr.cpp、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp)导入工程就可以用它的东西了。如果需要,可以将它做成自己的DLL来调用。

后解压缩TinyXML后,将这六个文件添加到你的c++工程中,分别是tinystr.h、tinystr.cpp、tinyxml.h、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp。


如本示例中,只有 main.cpp 才是测试代码:



编写代码时,只需要包含 tinyxml.h 头文件即可,但是,编译时却需要把所有.cpp 文件都加上

示例代码如下:

?
1
2
3
4
5
#include <stdio.h>
#include "tinyxml.h"
#include <iostream>
#include <cstring>
using  namespace  std;
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
  TiXmlDocument:文档类,它代表了整个xml文件
  TiXmlDeclaration:声明类,它表示文件的声明部分
  TiXmlComment:注释类,它表示文件的注释部分
  TiXmlElement:元素类,它是文件的主要部分,并且支持嵌套结构,一般使用这种结构来分类的存储信息,它可以包含属性类和文本类
  TiXmlAttribute/TiXmlAttributeSet:元素属性,它一般嵌套在元素中,用于记录此元素的一些属性
  TiXmlText:文本对象,它嵌套在某个元素内部
*/
//创建xml文件
int  writeXmlFile()
{
  TiXmlDocument *writeDoc =  new  TiXmlDocument;  //xml文档指针
  
  //文档格式声明
  TiXmlDeclaration *decl =  new  TiXmlDeclaration( "1.0" "UTF-8" "yes" );
  writeDoc->LinkEndChild(decl);  //写入文档
?
1
  int  n = 3;  //父节点个数
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  TiXmlElement *RootElement =  new  TiXmlElement( "Info" ); //根元素
  RootElement->SetAttribute( "num" , n);  //属性
  writeDoc->LinkEndChild(RootElement);
  
  for ( int  i=0; i<n; i++) //n个父节点
  {
   TiXmlElement *StuElement =  new  TiXmlElement( "Stu" ); //Stu
   //设置属性
   StuElement->SetAttribute( "class" , "A" );
   if (2 == i)
   {
     StuElement->SetAttribute( "class" , "B" );
   }
   
   StuElement->SetAttribute( "id" ,i+1);
   StuElement->SetAttribute( "flag" , (i+1)*10);
   RootElement->LinkEndChild(StuElement); //父节点写入文档
  
   //姓名
   TiXmlElement *nameElement =  new  TiXmlElement( "name" );
   StuElement->LinkEndChild(nameElement);
?
1
2
3
4
5
6
   TiXmlText *nameContent =  new  TiXmlText( "mike" );
   nameElement->LinkEndChild(nameContent);
   
   //分数
   TiXmlElement *scoreElement =  new  TiXmlElement( "score" );
   StuElement->LinkEndChild(scoreElement);
?
1
2
3
4
5
6
   TiXmlText *scoreContent =  new  TiXmlText( "88" );
   scoreElement->LinkEndChild(scoreContent);
   
   //城市
   TiXmlElement *cityElement =  new  TiXmlElement( "city" );
   StuElement->LinkEndChild(cityElement);
?
1
2
3
4
5
6
7
8
9
10
   TiXmlText *cityContent =  new  TiXmlText( "Shenzhen" );
   cityElement->LinkEndChild(cityContent);
   
  }
  
  writeDoc->SaveFile( "stu_info.xml" );
  delete  writeDoc;
  
  return  1;
}
?
1
2
3
4
5
6
7
8
9
10
//解析xml文件
int  readXmlFile()
{
  TiXmlDocument mydoc( "stu_info.xml" ); //xml文档对象
  bool  loadOk=mydoc.LoadFile(); //加载文档
  if (!loadOk)
  {
   cout<< "could not load the test file.Error:" <<mydoc.ErrorDesc()<<endl;
   exit (1);
  }
?
1
2
3
4
  TiXmlElement *RootElement=mydoc.RootElement();  //根元素, Info
  cout<<  "[root name]"  << RootElement->Value() << "\n" ;
  
  TiXmlElement *pEle=RootElement;
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  //遍历该结点
  for (TiXmlElement *StuElement = pEle->FirstChildElement(); //第一个子元素
   StuElement != NULL;
   StuElement = StuElement->NextSiblingElement()) //下一个兄弟元素
  {
   // StuElement->Value() 节点名称
   cout<< StuElement->Value() << " " ;
   TiXmlAttribute *pAttr=StuElement->FirstAttribute(); //第一个属性
   
   while ( NULL != pAttr)  //输出所有属性
   {
    cout<<pAttr->Name()<< ":" <<pAttr->Value()<< " " ;
    pAttr=pAttr->Next();
   }
   cout<<endl;
   
   //输出子元素的值
   for (TiXmlElement *sonElement=StuElement->FirstChildElement();
   sonElement;
   sonElement=sonElement->NextSiblingElement())
   {
    cout<<sonElement->FirstChild()->Value()<<endl;
   }
  }
  
  return  1;
}
?
1
2
3
4
5
6
7
int  main( int  argc,  char  *argv[])
{
  
  writeXmlFile();
  printf ( "\nafter write\n" );
  
  readXmlFile();
?
1
2
  return  0;
}

编译运行结果如下:



生成的xml文件内容如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<? xml  version = "1.0"  encoding = "UTF-8"  standalone = "yes"  ?>
 
< Info  num = "3" >
 
     < Stu  class = "A"  id = "1"  flag = "10" >
 
         < name >mike</ name >
 
         < score >88</ score >
 
         < city >Shenzhen</ city >
 
     </ Stu >
 
     < Stu  class = "A"  id = "2"  flag = "20" >
 
         < name >mike</ name >
 
         < score >88</ score >
 
         < city >Shenzhen</ city >
 
     </ Stu >
 
     < Stu  class = "B"  id = "3"  flag = "30" >
 
         < name >mike</ name >
 
         < score >88</ score >
 
         < city >Shenzhen</ city >
 
     </ Stu >
 
</ Info >

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值