请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好。 TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。 DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。 如下是一个XML片段:
7 X" `5 W& l1 K+ K/ Q Z <Persons>0 Y7 ^; Q; P# y1 y4 R2 r6 w$ v
<Person ID="1">
/ [8 {, e3 W6 W: u1 F <name>周星星</name>
; \7 I, a, B6 y) c3 P <age>20</age>
7 G; o1 B9 O5 V- v( y" Q1 F/ B </Person>
! O% {' w% ^2 ~: s) _ <Person ID="2">5 i" r0 b3 M. j& K4 E4 G! u
<name>白晶晶</name>
8 r y7 W, L6 Z. S. ^2 ^ <age>18</age>
( g) Q. p, v7 B/ M% s/ k </Person>( B/ k5 V6 x" i2 D% w9 a8 Y1 `
</Persons>' j0 H( a; Y) h' ^5 O) x
2 P& Y- n, K% [5 e* b$ x% J 在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文件:. E7 l( i9 m4 k: y2 |" \8 U6 d' f% t
<Persons>
% b" P+ o( @2 G/ T$ [2 S3 q <Person ID="1">
" y m( L1 L( f9 [, P <name>phinecos</name>
2 g; O3 k. S( }: C <age>22</age>
; n; g, n8 i: ~( s$ d9 I </Person>6 ?4 [7 [7 K N5 |6 Q" Z# W7 ?
</Persons>
8 h% {' Z% J9 l
9 s( e9 s- }/ t6 P% p) A读写XML文件的程序代码:, S% u# p i6 S4 k* z
#include <iostream>
$ A$ e" @; w1 s2 X6 j; N#include "tinyxml.h"
4 \6 B; r( E9 o/ x#include "tinystr.h") v- t. s9 U1 b I5 c+ H
#include <string>* J$ ]. h" t% \5 t4 c
#include <windows.h>
# J+ [3 |9 _ b$ x7 Z2 C#include <atlstr.h>9 g4 c3 j. K" X/ Y
using namespace std;
0 Q! X* m* B5 G
, G W; n* i p: I9 Q$ QCString GetAppPath(); l$ T& i& r; |8 H+ s9 S7 M
{//获取应用程序根目录" H$ l) t9 |& F5 P- ]" M
TCHAR modulePath[MAX_PATH];
3 ?9 v# ]4 _# m2 O GetModuleFileName(NULL, modulePath, MAX_PATH);/ z0 ]' `' n% n) S; m, ]6 q& g
CString strModulePath(modulePath);
& B: K0 h( R, L2 m. M" n strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));
8 P& M4 F- ?: b7 z8 T0 e return strModulePath;/ C- x5 s1 B9 t* g8 B
}
9 V' ], |: }" E* M! G7 H
6 T o+ Y+ ^) z+ Z/ g) U( @4 J0 gbool CreateXmlFile(string& szFileName)
. j r3 `% f* ?) ^& `# P" u5 Z0 Q{//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false' P" q) `. @9 M' z
try
, ?8 V) |# M% O {% k* T! l( e, R, H2 r, l" N
//创建一个XML的文档对象。
. @. \7 o& _$ S( { TiXmlDocument *myDocument = new TiXmlDocument();
6 L' N- m0 k: n6 H& T //创建一个根元素并连接。' y" P( w7 Y3 O- C6 J# n
TiXmlElement *RootElement = new TiXmlElement("Persons");
7 Y- z( X3 K" k- W G& M* h myDocument->LinkEndChild(RootElement);4 D3 r1 q- K/ L. k
//创建一个Person元素并连接。
5 m! O3 x n* O% B1 X' ?) B) x TiXmlElement *PersonElement = new TiXmlElement("Person");
( _8 R( R2 x; N. {6 l9 e0 u) f& E! v RootElement->LinkEndChild(PersonElement);; h& W$ F% a* I- h
//设置Person元素的属性。6 k2 H- t" N+ s' ~* U
PersonElement->SetAttribute("ID", "1");
) D; a* q0 f+ k$ C) _ //创建name元素、age元素并连接。! q8 T2 u3 J. v* g" i" t: O# `) M
TiXmlElement *NameElement = new TiXmlElement("name");
! B) O4 _/ Q" P0 h TiXmlElement *AgeElement = new TiXmlElement("age");
: }. u; R2 e1 r4 e! [ PersonElement->LinkEndChild(NameElement);! c1 T& g# k8 g- @8 K
PersonElement->LinkEndChild(AgeElement);
+ m, N9 O' d2 T/ P3 o# R2 n //设置name元素和age元素的内容并连接。
z7 S% q: |' O' w TiXmlText *NameContent = new TiXmlText("周星星");6 M+ P T- f. |* j; M" B
TiXmlText *AgeContent = new TiXmlText("22");! q6 K% |) M3 Y6 n/ V$ x7 Q3 i% O
NameElement->LinkEndChild(NameContent);8 }) s/ M% h2 _# {
AgeElement->LinkEndChild(AgeContent);
. Y4 P) y2 v2 H6 Q( X CString appPath = GetAppPath(); a) [+ |! p; Y* |7 ]6 U1 E# q
string seperator = "\\";- a9 i) T6 T) t( i; r7 t0 \5 C' G$ Y R
string fullPath = appPath.GetBuffer(0) +seperator+szFileName;; j- U1 x" i: ?" x3 x6 W
myDocument->SaveFile(fullPath.c_str());//保存到文件
8 C* d8 V9 S) c, B B; Q0 Z" N }5 z0 V1 I! t& v/ u6 Q
catch (string& e)
$ P% O+ y8 q. W5 c! x" ` {
) P) D5 N- D) R% r5 e7 c1 G, @ return false;
) h; y! U' [* \: Q! z, q }9 j5 k2 v5 P, K* [1 O6 B2 W2 U
return true;
5 R' E& Z+ K. i2 ~}
, J4 h4 {3 @ P( _* q: t, L$ Y! D7 ^0 l! c6 N+ c. }
bool ReadXmlFile(string& szFileName), N' i8 t3 {) d! K/ X
{//读取Xml文件,并遍历
' ?. Q/ C) y9 W5 s5 W8 L, x" W8 Y try- Z% f4 M, q7 z1 L& f3 ^
{
5 N' n0 M% G/ R$ n. F5 z CString appPath = GetAppPath();
0 d1 R1 P( q' {9 `2 K# f, I string seperator = "\\";
; x3 c" G# g- E* S string fullPath = appPath.GetBuffer(0) +seperator+szFileName;" y/ n7 E7 p7 F( W+ o* a' V
//创建一个XML的文档对象。
# G- g5 H( f, G TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());( ?9 R' v, h: F: ~( \
myDocument->LoadFile();3 M9 }0 O3 R- \, }
//获得根元素,即Persons。
8 [- I9 d6 t" h: B4 a/ } TiXmlElement *RootElement = myDocument->RootElement();: O8 q) [) t1 T9 Q% Q- @
//输出根元素名称,即输出Persons。2 e* W6 D+ V+ T7 f$ t2 k4 C6 w
cout << RootElement->Value() << endl;
, u& \1 i% A- u8 h4 t //获得第一个Person节点。; E1 ?; i5 f) @5 H6 F6 q
TiXmlElement *FirstPerson = RootElement->FirstChildElement();0 A& u3 q* {/ o# z0 m4 h2 m. l
//获得第一个Person的name节点和age节点和ID属性。( f0 f5 c3 m- y# u# f, k$ a+ d
TiXmlElement *NameElement = FirstPerson->FirstChildElement();+ v8 C9 F6 k/ u3 L
TiXmlElement *AgeElement = NameElement->NextSiblingElement();" k% w/ w: f! Y N* p* ?+ G
TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();* [, U# D1 j% I# L
//输出第一个Person的name内容,即周星星;age内容,即;ID属性,即。
: K3 |) c. [# z( d [+ t/ a1 h5 O- e cout << NameElement->FirstChild()->Value() << endl;
5 ^ g7 k2 T, Q4 i2 G; t- x9 ~2 M& I cout << AgeElement->FirstChild()->Value() << endl;4 N2 @- Z4 _5 ?9 v( e7 l6 x* E: o
cout << IDAttribute->Value()<< endl;
+ O: Q- O3 G5 X- d }+ z3 q$ @/ O' J; n
catch (string& e)2 W i. t' |, h4 j3 @4 z0 t
{1 c9 l) I; l! e- o2 C
return false;
. e K1 n k' N }
4 d6 e4 x H2 q& Z return true;
1 S v) T- k' }}
8 r# b* [ Q, y: T' [int main()
4 N- ~7 ?. K% G0 p' n{
- [, U2 L7 c4 v/ H- j string fileName = "info.xml";
' Y l' k) Z/ {9 J# B! l CreateXmlFile(fileName);' O' B. c8 d. |- H0 h
ReadXmlFile(fileName);
4 ^- m9 r" c: q4 r8 {}
9 _5 t6 V3 f, w4 \( b: z6 S6 a: b. \8 d0 J! S5 W! b
5 u( B: H7 K7 T V6 `6 d- f |