请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好。 TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。 DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。 如下是一个XML片段:
' P$ \* \6 q; J7 G <Persons>
9 y+ B' v' J |* [) H0 Y3 j <Person ID="1">& Z+ w0 J: w% g( b9 K8 c! Q" o0 P
<name>周星星</name>4 k3 H8 T' i- m7 ^% o- S( N
<age>20</age>
2 J% Q6 o. F) ]4 `! A' H </Person>
9 f6 F; q* K Y* \, w! \ <Person ID="2">
* ]1 D0 d0 B. t$ w. t4 h <name>白晶晶</name>" i$ C" |' V8 F
<age>18</age>" O1 T( I3 x9 O' i' x
</Person>
7 V8 n4 n" t7 { </Persons>
/ K# V# C8 N4 J+ p9 X
9 H3 i* W& v4 K' g* s C 在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文件:
7 J! p7 I, x1 w, y<Persons>8 R4 x8 D9 P8 @5 n1 X3 i* p, p" J
<Person ID="1">
2 v! u- e6 v) M, C( t6 ]4 }) \ c <name>phinecos</name>. f) C# M! b' K% P5 }! R
<age>22</age>& U* T2 o' |5 v7 ~$ w: s) ]
</Person>
3 G. N! {( {% c</Persons>
! G' E0 [7 C( t/ a1 G- \2 E! a7 @ ^! c8 m( ?; m
读写XML文件的程序代码:8 y; \5 ]+ J0 U+ w0 b
#include <iostream>
: i J, ]7 W& i#include "tinyxml.h"
1 F! e4 ^ c% z3 h' Q#include "tinystr.h"( \7 g, y) T% R/ C
#include <string>' |1 C! d* W3 U* F# F
#include <windows.h>
: I/ l; ~0 f) a7 Q% ~% V" x% W#include <atlstr.h>
1 h8 e6 e. H, a' L kusing namespace std;5 y; d: ?( O' k- X
e7 k6 H" d- W) Z* o
CString GetAppPath()
3 A p+ v" B+ w5 k* W. t{//获取应用程序根目录/ t r0 Q& w0 Q8 m1 X4 ~$ G
TCHAR modulePath[MAX_PATH];/ I, S8 F* y, F# R# g1 t, x
GetModuleFileName(NULL, modulePath, MAX_PATH);
! s/ d7 ^) I/ o1 V8 Z' R$ _7 w9 t CString strModulePath(modulePath);9 y& D2 q; }& `5 \2 |2 x) M
strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));* @3 C0 R3 A% x& J) g
return strModulePath;
/ [- z: ~) ~+ w2 l+ h}
% u! h( ~! T8 }; P# p. l) y k7 j' E7 G% q/ r
bool CreateXmlFile(string& szFileName)$ j! A& G6 n& ?2 q
{//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false, p+ ?" d, Y4 E' D% a7 e; ~
try
8 ^" J* A! S# [8 v! m$ F' d( p2 d# r- h {5 W& k; P; z8 p. n! q* s0 u
//创建一个XML的文档对象。0 U1 G% K/ ^/ @
TiXmlDocument *myDocument = new TiXmlDocument();
" \& X2 v' T: z) [: V& j" c ]1 L //创建一个根元素并连接。8 r6 Z% q( l, i8 a' q! r
TiXmlElement *RootElement = new TiXmlElement("Persons");
8 L @3 V$ p/ `6 T myDocument->LinkEndChild(RootElement);
7 _0 O6 H% `8 J8 l! t5 Q //创建一个Person元素并连接。
6 H: k5 H$ f- u% f5 N" l TiXmlElement *PersonElement = new TiXmlElement("Person");5 r- e' {- p0 p$ G9 o) V" n( F% q
RootElement->LinkEndChild(PersonElement);
) i) { b3 }. C5 i0 ? //设置Person元素的属性。
4 a {9 w6 I1 B: x# W. B PersonElement->SetAttribute("ID", "1"); }2 r! T" V& ]. P5 p5 r
//创建name元素、age元素并连接。
8 O+ v, `/ U3 _8 F8 v& D TiXmlElement *NameElement = new TiXmlElement("name");5 u7 ~; w, z" G! ?8 Z
TiXmlElement *AgeElement = new TiXmlElement("age");" f/ _8 p& g0 g3 A/ P1 k6 N
PersonElement->LinkEndChild(NameElement);
: X5 Z3 {! `, q7 v; { PersonElement->LinkEndChild(AgeElement);
% N7 C5 R& n$ p //设置name元素和age元素的内容并连接。' j! H) V3 e( o% @9 K5 `
TiXmlText *NameContent = new TiXmlText("周星星");/ L. s3 I) {% a( a7 c$ W( c$ a
TiXmlText *AgeContent = new TiXmlText("22");/ o3 h: U8 W- n, K& V% X% K$ F4 p) G
NameElement->LinkEndChild(NameContent);$ y2 z! K' c5 ]" h, g' f; }% ^( ]
AgeElement->LinkEndChild(AgeContent); F) i+ A W* y, n
CString appPath = GetAppPath();, q4 z+ m; q7 f9 A3 r* C' S
string seperator = "\\";
, U; q' a+ [" w: o) K* I string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
1 Q" H; u# Q, j! W. \- y myDocument->SaveFile(fullPath.c_str());//保存到文件( [8 V0 ~( h# `" ~
}' m7 i+ z' h0 \& |( o+ J- m
catch (string& e)/ Z$ w* {. [2 n9 r
{
" D" R5 x6 q2 {: A- o- R& `1 N return false; q1 r) J# m3 K v7 g9 c3 Y
}
" \( L% z2 ~3 k/ O( p return true;/ ]. g8 N" d0 @' U4 Y" ?4 N
}
/ a- y& K' z, [3 H0 U2 \2 Q5 v2 n4 u9 S4 P; G+ f
bool ReadXmlFile(string& szFileName)
% N: i6 l. ]* m& X2 E{//读取Xml文件,并遍历
) j, ]; X# f3 e5 h) m try
, g# r/ v n$ A, m {
" V0 r& ?& B. @8 t6 Q: N2 L& Z$ f2 z CString appPath = GetAppPath();2 {$ t6 a5 i( |0 I
string seperator = "\\";
0 \5 G, l4 R& [' G string fullPath = appPath.GetBuffer(0) +seperator+szFileName;+ I( f8 n# ~9 q/ g2 a( V
//创建一个XML的文档对象。
0 |, F) Z/ j- k* a& W6 o1 t TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());
- w- H0 ^/ D4 i$ Q+ ?4 D5 V; P myDocument->LoadFile();
; m/ S! M& c6 |+ n, T3 \ //获得根元素,即Persons。( T& n3 C4 v2 Z x; s
TiXmlElement *RootElement = myDocument->RootElement();
# _9 T3 w" J, g //输出根元素名称,即输出Persons。* O+ U! E, d2 b) n/ \- \
cout << RootElement->Value() << endl;
9 p0 ~% k+ o/ S //获得第一个Person节点。+ d' s& {& Z& h+ z+ |' q
TiXmlElement *FirstPerson = RootElement->FirstChildElement();9 Z5 k- A+ O! h/ Q& Q
//获得第一个Person的name节点和age节点和ID属性。8 }% Q- g# |9 T" H! l( D8 ~, b
TiXmlElement *NameElement = FirstPerson->FirstChildElement();
) C' O/ \& w7 S1 A; M$ @0 h TiXmlElement *AgeElement = NameElement->NextSiblingElement();. y6 y" r; v- E0 x- y$ x0 d
TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();
; `7 \3 v5 g# Y //输出第一个Person的name内容,即周星星;age内容,即;ID属性,即。
% ~$ \) a4 r, J" X/ K cout << NameElement->FirstChild()->Value() << endl;
* P& V& G0 J, H" y. `! s9 k- J+ o cout << AgeElement->FirstChild()->Value() << endl;2 c9 ]+ U2 e' z, c. m
cout << IDAttribute->Value()<< endl;
+ o8 x# f' a+ z. o+ p }$ |; I3 T7 W9 z( X
catch (string& e)! U1 S4 y E0 \* i4 W
{* x" f* N* {/ k4 R; @; r0 D) k
return false;
! X5 C; ?, Z9 |7 D3 D8 q }
) |& z+ ~" Y0 E- N& c return true;
0 I5 M* [: c5 a1 m0 t4 W}& Z- u! a5 V& @* r( f& i0 s% ^
int main()
) l; C& b1 r8 f{0 F1 g( B" e a* q6 E2 K
string fileName = "info.xml";5 O/ S9 I; `+ q* Z# l5 t1 X
CreateXmlFile(fileName);
; E$ ]& c$ Z+ B A) Q* C ReadXmlFile(fileName);! K! i) w- `. @: d N0 c
}" R% w! n1 @, X* `* C) i& ^+ F% t; i
5 ` M" V- o' P$ i1 S5 s- O
- D3 R4 b5 _, E+ {+ |4 O |