请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好。 TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。 DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。 如下是一个XML片段:
0 }# p2 X. A8 O. m4 Y <Persons>
8 F) M6 [2 b2 p5 \6 p- G8 f <Person ID="1">5 s3 o' J- p% Y0 R z$ n* J
<name>周星星</name> q0 z9 s* p8 r( m! Q
<age>20</age>, C& A, d& j' A1 d0 {2 @
</Person>, _7 h" T8 c$ Y
<Person ID="2">6 p4 l3 h& m6 h8 ^6 t# V3 X
<name>白晶晶</name>
- \/ G4 v: [! ]* {5 q <age>18</age>' ^0 I8 G. ^8 ?: y* M
</Person>
: j( T H% s/ t9 }0 J </Persons>
5 K9 @/ [" G i' A, g7 M; Z N: Q5 W& u5 z% I
在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文件:) r6 t$ M4 a& C) v+ h% u, O; @8 N# d
, S( A) g: W# G# ^& E* d+ B3 W" N9 w2 J 登录/注册后可看大图 <Persons>( _2 S2 |" j8 k) V7 }+ n
<Person ID="1">
: Z/ ?: Z1 l5 N) \4 |( s <name>phinecos</name>+ \7 a/ P# p0 y- u0 B7 H) g M
<age>22</age>
" D/ U8 {" A- f1 k </Person>- |$ s1 r7 m; Z. ~( R! ]: f }
</Persons>
- o( g5 H+ E9 H7 G' j2 N9 A& n3 F& _" s
读写XML文件的程序代码:8 C4 b4 b7 K+ m9 } h" `
#include <iostream>9 u4 G5 J, |7 _( a9 M9 t
#include "tinyxml.h"
4 u6 X$ Q+ m; p- w* P#include "tinystr.h"0 s! [0 b5 i0 V8 u. M. S9 n' E
#include <string>
- f8 }5 [3 t9 h% h; ]+ e#include <windows.h>
6 s7 B: V- X8 ^% z# N#include <atlstr.h>
3 b: B1 h1 Z2 e- O [ iusing namespace std;0 }7 @3 x9 l- Q6 F2 J! T
: ?1 Z: \* w7 f1 HCString GetAppPath()! h5 d3 w3 F) W: `5 R% N3 A
{//获取应用程序根目录! c( g) E# v8 ?/ m7 u) w! ^- x, d
TCHAR modulePath[MAX_PATH];
3 g, i: N# K. t& W c GetModuleFileName(NULL, modulePath, MAX_PATH);
8 q4 P! f# Z! a9 j6 H CString strModulePath(modulePath);8 `1 L( z7 w. n2 @% \0 `% G
strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));
1 ~3 N. i, i2 N- C& ^9 A z return strModulePath;4 B F- |6 N; c; p A- [
}; I; _2 ]" `" P6 l
2 ]2 t' E5 ]7 i" v, a
bool CreateXmlFile(string& szFileName)9 X+ b0 q& o) r3 j2 C; f, K
{//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false
4 ^+ }: Q' H/ r try
- Q& D- d+ D7 m* }. p) x g {' Q* L6 @% ^# K9 N% k
//创建一个XML的文档对象。
y9 \8 f4 k, G% d+ R TiXmlDocument *myDocument = new TiXmlDocument();4 ?" \( c! x. X# g2 C
//创建一个根元素并连接。4 z6 F5 z5 F) E3 S# c4 p
TiXmlElement *RootElement = new TiXmlElement("Persons");9 M( ^# A# S3 v% m2 n$ J
myDocument->LinkEndChild(RootElement);
4 i4 \2 a" I4 Y# I/ M! I& Y //创建一个Person元素并连接。4 U! Q% Z9 _+ ]5 _! ^4 J. A
TiXmlElement *PersonElement = new TiXmlElement("Person");5 O" @2 O; d9 ~+ y6 W
RootElement->LinkEndChild(PersonElement);. Y0 s" K" ?0 w. t4 \3 R+ ^; u/ |
//设置Person元素的属性。- m/ W4 D5 g" Q
PersonElement->SetAttribute("ID", "1");* W3 ^) f! O- k4 N6 e) T
//创建name元素、age元素并连接。: ~& i' U. o8 p0 ~/ U
TiXmlElement *NameElement = new TiXmlElement("name");; E# T: \! i; {8 v) I" M
TiXmlElement *AgeElement = new TiXmlElement("age");
' e9 g* |6 x8 z" N PersonElement->LinkEndChild(NameElement);
U/ _0 m w+ M' v) V2 Z PersonElement->LinkEndChild(AgeElement);: L% _1 L1 |2 ^
//设置name元素和age元素的内容并连接。 w. y* H; ?" M/ l
TiXmlText *NameContent = new TiXmlText("周星星");
+ K: q$ r. A* v" i* r5 o1 w TiXmlText *AgeContent = new TiXmlText("22");
& y3 J3 K3 K6 Z6 t7 }! z4 J NameElement->LinkEndChild(NameContent);; P' `' H3 l7 _; D9 I
AgeElement->LinkEndChild(AgeContent); C9 a0 |; \$ j6 ?$ @
CString appPath = GetAppPath(); a( [; w" W, [/ n3 w
string seperator = "\\";
% I1 q7 M# W5 L2 F! L% `! J" q string fullPath = appPath.GetBuffer(0) +seperator+szFileName;( R) Q( q+ G+ L' n3 b
myDocument->SaveFile(fullPath.c_str());//保存到文件4 _) r$ y$ i, M6 t) H( u5 A) d1 ?( r
}
% z: I% n8 r( z8 s% J catch (string& e)
+ j7 m9 w( n. I/ d! T {
3 t; E2 Y8 ^9 y& p7 d return false;: }+ m/ H8 c' D
}
: m1 Y! n' S' _8 M2 v5 j return true;9 `3 T& O2 w% C/ L
}0 x: n- V" i2 B, ], b* f$ c y
4 J4 N5 p; V5 pbool ReadXmlFile(string& szFileName)6 ~* `. j5 ]/ ^/ G& N H! y
{//读取Xml文件,并遍历
4 t% J3 h5 r$ w; _+ u: H) w) G try3 M9 j0 `. |/ Z3 j
{
v$ b+ P1 w% @6 k" c4 X. J CString appPath = GetAppPath();/ e& P+ A/ I- h+ w" s% ?4 P
string seperator = "\\";/ K3 V9 q" f! f
string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
1 X9 q5 W) h, R# F S //创建一个XML的文档对象。
9 W, m+ _, J" w TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());
5 ^2 D0 u, I" N% I1 ]9 O+ s myDocument->LoadFile();
+ F0 X# y. ?" M1 P; U //获得根元素,即Persons。) v: N0 z7 Y8 S& c8 D( v8 _$ b5 @5 O: ]
TiXmlElement *RootElement = myDocument->RootElement();
) l$ E$ a% n* a+ a% k1 B //输出根元素名称,即输出Persons。
: |6 S8 |7 G- A6 o0 `" t/ t cout << RootElement->Value() << endl;
$ t$ Z' H* d" T( r1 M //获得第一个Person节点。
5 a% |! z$ l3 e: ^. V& E( ? TiXmlElement *FirstPerson = RootElement->FirstChildElement();
6 Z8 Q# ?: u; z1 W //获得第一个Person的name节点和age节点和ID属性。
, E _9 z; k$ R1 ?) }- i TiXmlElement *NameElement = FirstPerson->FirstChildElement();
8 w. J# r9 F/ ^) K TiXmlElement *AgeElement = NameElement->NextSiblingElement();
9 s: d/ c! {% v) P* [ TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();
% J K7 q8 g) Y% h1 S6 Z# g //输出第一个Person的name内容,即周星星;age内容,即;ID属性,即。$ \. X/ K! t$ j/ r
cout << NameElement->FirstChild()->Value() << endl;
' M; b2 R! r% g: w cout << AgeElement->FirstChild()->Value() << endl;
4 @0 W. G, [! @- D( P cout << IDAttribute->Value()<< endl;
7 T. J' Q5 K' H }
, a1 Q% u$ g! B# W3 P catch (string& e)
6 r+ q. g+ P6 w4 T( p: F, r {6 W& ?, M5 Y+ i
return false;
# Q# l* m4 h1 [0 `4 K1 f# k& g }
7 I0 W. l+ x& k8 w6 B, }: e return true;
% u5 d) F" I1 I8 _- D) ]9 J q}
2 w* v. |5 o& Q& S1 z" Sint main()
5 T% y, s% o8 x{# M \# b- ]& n% |+ E8 }
string fileName = "info.xml";
+ u) ?+ A" b3 L. U3 h% N CreateXmlFile(fileName);. s" Q) C1 Q, g6 Z4 G1 H
ReadXmlFile(fileName);7 p/ t0 X% l! F- N
}
8 l l1 o' K7 B6 `& V: K3 r# ?" W2 y- Q' Z9 x4 {( f# \; T# [! x8 o
' i& Q1 W! j* w- C; j
|