请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好。 TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。 DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。 如下是一个XML片段:; }" v7 |' h, f! \
<Persons>9 b' J, M m2 k J5 \
<Person ID="1">
! c9 h6 u4 E) V: T" T- o3 Q9 @ <name>周星星</name>* S( d% J/ s- t! g
<age>20</age>7 e- k# ^4 P8 \# d, e- v l2 v
</Person>
8 i( j; R" k+ p v- P2 t( I+ t <Person ID="2">$ x% n1 ]8 o6 g& a2 m
<name>白晶晶</name>
. W4 w/ [; x7 b <age>18</age>. Z4 w; P9 w) C3 Z9 u
</Person>) H7 ?8 |# H5 B% C7 ~4 `2 X
</Persons>
9 }, R0 h/ X) S8 X# S" D4 d* V% q# u1 e' _3 j+ d. e- k! u# L
在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来调用。举个例子就可以说明一切。。。 对应的XML文件:+ W8 a. e; S( z% I' F: N
<Persons>
# N* f; W V% \* T b% n <Person ID="1">
7 m4 P4 q/ G3 a; a5 Q* ^7 @$ X5 c7 t <name>phinecos</name>
! {5 m& r$ R9 M4 y; K7 K <age>22</age>( _ w5 H$ b. D8 ?
</Person>
) u8 O) ]& b. M" O( X</Persons>" s$ k5 P2 ?& w! z- r4 f0 G( I( t
7 h/ b8 `& _1 m. S读写XML文件的程序代码:
# i5 H/ N, \' i' b' v #include <iostream>
1 I' g% `- b* U#include "tinyxml.h"* e! H9 j" |7 I5 A$ z, |; m9 @
#include "tinystr.h"
: ` r2 a x' g# }7 k7 T#include <string>% H/ t9 I2 F R; a& D
#include <windows.h>
% z, _; u8 x v#include <atlstr.h>$ z" Q L2 v$ w' V+ u. u) H& o
2 ]6 A6 B0 \4 W) y* |! Y8 Q( Z0 A' H# y 登录/注册后可看大图 using namespace std;
. g/ [/ K' N* r* t3 }- W- X' b' ?% m
4 m: S( t$ T8 H9 Y) ECString GetAppPath()
( K3 j. t( R9 c1 I# E{//获取应用程序根目录
. Z/ J2 K( @! m+ t TCHAR modulePath[MAX_PATH];
! j3 u3 j+ d$ }% x GetModuleFileName(NULL, modulePath, MAX_PATH);
. R5 [/ V! O3 |3 b( b$ @! ] CString strModulePath(modulePath);
4 {: ?( Y9 r' }5 y0 Z strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));% F1 x% N! h( p) o8 x' m
return strModulePath;0 m5 U1 n: E* l3 t* u/ j
}- M, B8 ] E$ _0 q. a& p
. l1 Y. n% A4 ^$ D9 l3 i2 P4 I& Y
bool CreateXmlFile(string& szFileName)
$ R4 p" s% o7 ~- h9 _% G{//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false
8 v9 {; F5 M+ n; k4 P+ o, } try4 ~2 _$ o: A( T% J2 I. U P$ g; s: }
{3 K. t7 t% G& J+ Y$ z4 h; R0 ^
//创建一个XML的文档对象。4 ]. Y3 _& T1 Z8 i& ~2 i2 T6 U
TiXmlDocument *myDocument = new TiXmlDocument();
: K$ J' ?. I# I% x& r5 [ //创建一个根元素并连接。
5 V @3 ?/ D* c& g( Y4 w TiXmlElement *RootElement = new TiXmlElement("Persons");) f4 Z) o2 H' ?
myDocument->LinkEndChild(RootElement);1 r$ k3 a4 g5 K' W: K
//创建一个Person元素并连接。$ F e6 ?4 L0 R# z E, V
2 g; E1 Q/ ^6 k" u9 I0 Z) w" e1 K4 D. D4 g 登录/注册后可看大图 TiXmlElement *PersonElement = new TiXmlElement("Person");
: @4 F/ b0 w, |* K( b3 G7 l RootElement->LinkEndChild(PersonElement);: w3 q& i2 r* N, w
//设置Person元素的属性。. f( \! f$ U) K5 T
PersonElement->SetAttribute("ID", "1");2 c* _% f8 C d7 h
//创建name元素、age元素并连接。
: [( \9 A; w1 `( W) o4 Q9 W1 t TiXmlElement *NameElement = new TiXmlElement("name");
- d/ v! H# Y+ Q/ a) ]( S% n; P TiXmlElement *AgeElement = new TiXmlElement("age");
! a+ S, M- K5 K5 \, [8 q0 m PersonElement->LinkEndChild(NameElement);' w& t% W4 g( Z+ \
PersonElement->LinkEndChild(AgeElement);- {4 C& A# j0 c3 N/ }2 e& Z& A! L2 m
//设置name元素和age元素的内容并连接。. J: X$ v* o0 V) G. \% n8 K
TiXmlText *NameContent = new TiXmlText("周星星");$ i# D9 j1 |5 K
TiXmlText *AgeContent = new TiXmlText("22");
5 @% q: w( t- ]! @ [$ e" B8 b1 r NameElement->LinkEndChild(NameContent);6 Q* P# u0 g$ t. ?* K
AgeElement->LinkEndChild(AgeContent);
7 F: W, b; i6 {. F CString appPath = GetAppPath();, D/ H3 [+ g8 \/ _. B. V- U
string seperator = "\\";
: C4 K3 z- W$ R" K" A string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
3 n+ l5 L" e: d1 w! G+ U myDocument->SaveFile(fullPath.c_str());//保存到文件" @( E4 `4 d% t9 P: D: R
}& H& k( `0 A7 A5 i) X' O# u
catch (string& e)8 b9 f; k2 S: W- Q' J
{
- H' b$ l, W6 c7 _ return false;
- t4 n" b Z4 f; v& M& r }
- y/ c% g3 p" q+ u% x* `* z2 H2 _ return true;2 }3 v! D8 @& ~" t
}
: p. N1 Y$ c; r8 ^. w
; y# I( T% U* {" k0 X. `bool ReadXmlFile(string& szFileName)8 ^8 j4 T3 S% b6 v! f6 R
{//读取Xml文件,并遍历
7 A+ t" v. U4 D* Y6 J/ C try
8 q4 o) [5 j# C( }# i6 x {
+ e5 T) M+ \5 J* }0 K CString appPath = GetAppPath();. c4 `$ A; K, M
string seperator = "\\";* k$ x! I$ w! R
string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
- N/ m8 |2 b6 e; w3 `5 t( {4 L3 c) u //创建一个XML的文档对象。/ f8 K: D8 {! `1 h
TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());
% F0 }0 ]+ Y! F8 Z0 i4 r/ t. Z myDocument->LoadFile();. i% p5 }, |" O$ ^5 h! u
//获得根元素,即Persons。
% M" C: R3 N9 e TiXmlElement *RootElement = myDocument->RootElement();
+ U6 |9 x9 @* O6 F //输出根元素名称,即输出Persons。) x% m; W' x# A. W4 C" r
cout << RootElement->Value() << endl;
9 s; D" r6 H8 t2 j //获得第一个Person节点。. ?* [, |+ o: Z+ t
TiXmlElement *FirstPerson = RootElement->FirstChildElement();
; u- `( C# C1 z' l. b! z' F9 D //获得第一个Person的name节点和age节点和ID属性。; H; D8 Z( d9 D. c) [% ]# C
TiXmlElement *NameElement = FirstPerson->FirstChildElement();
1 H+ F# L$ M% b1 ]. R TiXmlElement *AgeElement = NameElement->NextSiblingElement();
5 y8 Y' l( I) z4 U; e& u# Q TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();% N+ V: q' X( g8 R! L9 b
//输出第一个Person的name内容,即周星星;age内容,即;ID属性,即。
1 K- s( f! |1 ?/ T8 I cout << NameElement->FirstChild()->Value() << endl; r2 [. z3 l5 ~' `* B
cout << AgeElement->FirstChild()->Value() << endl;
% q/ o# _+ m- a cout << IDAttribute->Value()<< endl;# T0 n6 F: |9 j- l( [4 b, A# ~2 f
}
5 G- X6 P& c5 K/ `( \; Z catch (string& e)- U# R3 i, D. T0 w0 F, n
{8 n1 ]1 f- O7 A$ k. S* g
return false;
" ^( L8 r( W! ^& R }* ]: [- S2 O, O9 P
return true;
. c# a9 o; Q/ ^# g v}
1 y: A' d1 f7 ~int main()
. S+ E( d( r7 u) E. | C{
8 p4 L7 y' C! H8 d string fileName = "info.xml";
* c* P! J6 | R6 Y8 [ CreateXmlFile(fileName);- ?! j, }$ {. s- E" U4 t) J
ReadXmlFile(fileName);
5 u0 `0 [& g9 _}8 Q& ]: Y& F- s* ^: C. N
% @7 x8 l/ K% G: b, p
0 K, ~- ^; Z% n5 }! g1 \( ]& ~ |