请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
×
读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好。 TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。 DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。 如下是一个XML片段: u3 x7 Y# Q4 f3 j2 u9 ^1 `4 _6 G
<Persons>
6 o- |% Z# c& C. a+ Y. X <Person ID="1">+ `9 N! Z: z) \. |! w2 o
<name>周星星</name>4 ~3 i' R5 e/ E2 t4 V! Y
<age>20</age>2 S# B3 I' {8 q# a
</Person>
+ d- v, x* N; O$ X3 a4 n0 k& p% n <Person ID="2">
. P. R* ]& s7 k* C. S <name>白晶晶</name>( Q2 @3 S8 K' ^% `# t) Z, \7 w
<age>18</age>+ r* X! }, F0 W! R
</Person>
/ I" u( N& I- A </Persons>
9 s4 g, ?4 ?+ e3 d+ k, a' W& g; k: K# c4 k; Q+ I1 F, m. M
在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文件:, `& D$ ] ~3 w- {3 a7 }; L% k
<Persons>, X7 N+ F) M+ }& m7 I$ A% C+ |
<Person ID="1">
. o2 G$ a$ L! F8 a, R <name>phinecos</name>
t- d- Y. C# ~( i, L9 `- h# @ <age>22</age>
8 r+ b3 P* {* _+ ^: C( h% R6 W- P, P </Person>, Q, z1 D% a# O+ S3 x; W9 w
</Persons> r+ Y- Q* I: `
# j3 `, ~3 k- u, \' S
读写XML文件的程序代码:+ D% Y' z, u9 u4 R1 n9 h+ i' y; _
#include <iostream>
# Z2 i; ~* C3 G1 _3 U2 [' K9 B8 t#include "tinyxml.h"
" L Z, M' G i! V# k- L# I, i#include "tinystr.h"9 w2 y, n8 [2 ?
#include <string>
! h9 u, G/ D, G) a8 D$ c: I( H#include <windows.h># x7 G* F `4 g L
#include <atlstr.h>
. X! j3 f0 C, V' d& I1 Husing namespace std;
# J) D8 k6 j' |4 @& S4 |4 ^6 Z4 B
5 d) B1 @ M. s' I7 y2 Q9 gCString GetAppPath()& [" Y' ? t) [5 ~, B
{//获取应用程序根目录
# g) R" m; F" i$ i; u9 u( i TCHAR modulePath[MAX_PATH];
) P# ]# a. e( X% k GetModuleFileName(NULL, modulePath, MAX_PATH);
" G: m" }% V* r7 E2 W CString strModulePath(modulePath);1 z' ^! @. a( V
strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));
! `4 ?) m" @6 u! o2 f( w& O% K9 c return strModulePath;6 a' k0 c9 t) a, Q; B
}
; s5 X+ o m/ D! ^5 B( T+ O& Z- P6 @3 `6 F% _
bool CreateXmlFile(string& szFileName)
9 Y9 M1 _6 I" I$ q* N, F{//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false
7 G4 a* R3 k$ b try
& p) q( F4 a6 w5 D; x/ v {
7 v9 g0 I4 m: i/ G) c( b //创建一个XML的文档对象。
# L( Y8 Q6 F7 k TiXmlDocument *myDocument = new TiXmlDocument();# d! V1 c' M+ K
//创建一个根元素并连接。! I- Y$ F0 a" n) ~; s/ U
TiXmlElement *RootElement = new TiXmlElement("Persons");
( y4 G- d+ c4 |9 O: p# \ myDocument->LinkEndChild(RootElement);+ R2 J+ j9 z* ^& e" P6 P
//创建一个Person元素并连接。/ Z- A% P2 [, l% E9 V" m; r
TiXmlElement *PersonElement = new TiXmlElement("Person");- D- R" R1 | M( R2 e
RootElement->LinkEndChild(PersonElement);
( d/ a, F, {2 c4 c //设置Person元素的属性。
+ s* j) Q5 o) T1 H, F PersonElement->SetAttribute("ID", "1");" e% r) @( r: r/ N: M
//创建name元素、age元素并连接。
J$ x: } x6 F5 V8 O/ K TiXmlElement *NameElement = new TiXmlElement("name");; d0 T) r& o# T- z* J7 t
TiXmlElement *AgeElement = new TiXmlElement("age");
! E/ C y$ J. @$ j1 z- v PersonElement->LinkEndChild(NameElement);
5 Z' v% S$ Y1 I1 {" M- O) r PersonElement->LinkEndChild(AgeElement);& m8 ^/ h$ K2 \0 r3 {( M/ G! s9 n
//设置name元素和age元素的内容并连接。3 y% B6 D: z* c3 A; @' Q5 E7 n+ A0 K- I
TiXmlText *NameContent = new TiXmlText("周星星");! C+ ?; |3 S# a: v9 F
TiXmlText *AgeContent = new TiXmlText("22");7 K p; W9 M6 r1 s
NameElement->LinkEndChild(NameContent);
/ B& ]1 m3 w3 Z L" W( R- q AgeElement->LinkEndChild(AgeContent);& P6 ~: c! p0 N! e& G2 Z
CString appPath = GetAppPath();: a6 k2 H0 q. |9 c$ j3 Y* z
string seperator = "\\";
4 M+ v8 T6 V- v, N string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
" \$ b: a5 {; L2 B* Q. U3 y+ H" H& ? myDocument->SaveFile(fullPath.c_str());//保存到文件
* P) z6 W5 X4 G. H }) e+ u( j) v! h6 V4 e
catch (string& e)% y5 G& J9 N6 M# M
{- `* b7 D m8 i
return false;' ? `! M1 o" w" Z4 g4 r0 Z+ @
}" U- i4 m2 C: D$ J5 n
return true;
1 _# F3 j9 f& O; z3 o}
& Z0 `# D1 \2 L' v, M8 X3 P9 y' Z8 E1 V3 s" g' X2 I7 r1 z0 T
bool ReadXmlFile(string& szFileName)
9 Q+ M, N$ Y# k8 ^- t: m& P{//读取Xml文件,并遍历
2 M, e" M$ f$ e/ a# A9 x try
! ?+ i5 D' N3 r; m$ J! \ {! c8 c6 i+ y" X5 L( F( J
CString appPath = GetAppPath();! s5 |! o G* S' y- g1 V4 p1 c
string seperator = "\\";
7 Q$ s }3 g- ? string fullPath = appPath.GetBuffer(0) +seperator+szFileName;) P6 D4 `& _# o7 h
//创建一个XML的文档对象。& W7 q% f" _* }, j5 z2 t; u
TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());5 \; {: v# z6 L& J t4 M4 i, @
myDocument->LoadFile();+ W5 W& A, _; ]# K( N
//获得根元素,即Persons。
T/ A1 E9 v6 e5 }/ Q6 Z6 L- I# F TiXmlElement *RootElement = myDocument->RootElement();& o6 x- O# V. k+ \, c
//输出根元素名称,即输出Persons。
, Z" H, C) c; L q Q/ g' Q4 {1 c3 N4 w0 A- T cout << RootElement->Value() << endl;: L/ u6 E4 b$ d. [
//获得第一个Person节点。9 M0 n B8 m" {4 C- a
TiXmlElement *FirstPerson = RootElement->FirstChildElement();
3 H" q# m! |* [. i/ v K: Q //获得第一个Person的name节点和age节点和ID属性。
8 T0 `4 I( l# D TiXmlElement *NameElement = FirstPerson->FirstChildElement();/ g9 `9 H. I! I+ N: I9 Z
TiXmlElement *AgeElement = NameElement->NextSiblingElement();
6 Q R: v0 h& g1 p, V TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();
1 ^2 N2 V5 j( y //输出第一个Person的name内容,即周星星;age内容,即;ID属性,即。
' u4 b$ M0 m) o4 O2 e$ |# f cout << NameElement->FirstChild()->Value() << endl;
: S) d6 ?: z- m/ M cout << AgeElement->FirstChild()->Value() << endl;& Z: ^0 U' z. S1 N C6 k
cout << IDAttribute->Value()<< endl;
" B- g! t; o& ~& j- z }
* J: B( }! ?. Y2 _- \+ w2 ] catch (string& e)# w' O1 H% g# c) l7 Z, `
{6 A- ]! L+ U0 s, ^6 Y
return false;, _! h6 w. h3 H. Q6 j; b
}, S2 Q% u1 ^6 l# D
return true;
( s4 h+ v0 r. C# k! P* P$ J- Q}: v8 {' N+ z- q. D2 n
int main()
- K4 I0 K% Y( K9 M% `{! S6 {7 Y. b$ `: p
string fileName = "info.xml";) g" I$ a) C* ^* [1 \9 g
CreateXmlFile(fileName);
" T# B& T1 b r4 u$ W3 \/ \8 J* A0 Q ReadXmlFile(fileName);% @& v$ l% K3 R5 K9 I) h
}$ q! v: i- L M' f; f8 r
! Z; L$ k" j: X) |
' M% {- s$ @8 z6 P+ o5 w0 M0 {) a |