请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好。 TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。 DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。 如下是一个XML片段:% Q) F8 d6 \1 b2 q
<Persons>4 ?4 m% f0 F( P
<Person ID="1">
) r/ o7 e& X J4 d. t: Q- g) ]4 B <name>周星星</name>$ U9 Q$ t7 @; b" A: `0 u! [
7 |6 I; g+ Q4 \+ n& N8 B& }, C) O1 Y" G
登录/注册后可看大图 <age>20</age>" A- i9 W, n E0 X- U% R
</Person>& s, M' N. j8 P$ J7 b4 L. G5 [
<Person ID="2">- P2 j: X, t- S! D
<name>白晶晶</name>
( K! R) b" a' p" j <age>18</age>
- G; X( O& G* j7 Y, z </Person>% { o' G% N6 `7 k2 f8 S4 u" P
</Persons>
" v3 I+ G% |) V; m7 L
) X1 ?2 Q1 U+ o" Y8 D% E7 G 在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文件:
- C& [) z& t( Z7 N, b<Persons>7 Y# |6 T p/ H6 |
<Person ID="1">
+ b( |; C: L' O; |' I& w <name>phinecos</name>
: H9 u2 k" t" ~! z) u: n <age>22</age>
0 Y5 G+ F7 u+ x </Person>
# o/ C6 l- Z( W( _5 e) \</Persons>
3 }& w: J6 p0 X' a. B4 f2 k/ T4 x$ |% C9 R
读写XML文件的程序代码:( i+ u5 [' @1 d! o; y- G
#include <iostream># a/ U* g6 m# _4 K
#include "tinyxml.h" a% O1 N, P, w
#include "tinystr.h"
8 ^( P1 {5 u+ K/ M5 r' s#include <string>
% K7 A" o4 N' ?, _: ^#include <windows.h>- h6 @3 `2 ~: ^/ Y& X. _, o L
#include <atlstr.h>
. I3 B) \0 t" ]# W4 i5 N& E* `: @( S& ausing namespace std;
- ^3 e+ V- m/ g1 t' M1 x
* Z. S) t8 j# r4 @9 r) h& ~% HCString GetAppPath()
8 p8 w' o; _2 M6 j* U{//获取应用程序根目录0 s/ k0 @- i' K1 ?, a9 j
TCHAR modulePath[MAX_PATH];8 Y- y' U1 H2 s! q2 }# | U
GetModuleFileName(NULL, modulePath, MAX_PATH);, l N5 G7 s: U" m$ e( p; i/ \
CString strModulePath(modulePath);
& Y# ~; O* K2 \ strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));* _$ p* X) Y. R' _, a# R
return strModulePath;
! I9 x" E' `& ]( j& A' {}
/ v9 @- N" V0 f7 K) ^# y( P* I9 e7 i
bool CreateXmlFile(string& szFileName)
, v7 `$ H" o/ z3 d9 U, q( x* G{//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false
, Z' y+ e. L2 L try2 N& x: z) D# O! c/ k$ b q' k' V
{
& z& X/ c6 q, w& ?+ f5 ^ //创建一个XML的文档对象。3 S, y, w | ~$ `& ]; A1 x
TiXmlDocument *myDocument = new TiXmlDocument();
( T; Z A: z) ?; I' b# [ //创建一个根元素并连接。( Y$ e; Q) y/ `
TiXmlElement *RootElement = new TiXmlElement("Persons");* F% c9 N" D% [0 h2 \: k: U+ u$ ~
myDocument->LinkEndChild(RootElement);
. P: J9 M$ v7 M! @5 e //创建一个Person元素并连接。0 {6 g, d0 G* x6 c5 c
TiXmlElement *PersonElement = new TiXmlElement("Person");4 n8 [7 W$ R" ]
RootElement->LinkEndChild(PersonElement);2 u0 s9 [ E2 c9 d
//设置Person元素的属性。
5 R2 B, e# S* D+ j( n PersonElement->SetAttribute("ID", "1");
+ @% g! f7 k# k+ x4 C5 _ //创建name元素、age元素并连接。) v8 t+ l" F& Q( j: G$ A
TiXmlElement *NameElement = new TiXmlElement("name");4 z5 u5 C4 F! z9 @/ A: I
TiXmlElement *AgeElement = new TiXmlElement("age");
$ v! u, d, w6 ]- Q1 w3 z! F; U+ m PersonElement->LinkEndChild(NameElement);
' w- X& \* g: @ PersonElement->LinkEndChild(AgeElement);( S$ i6 s$ z K; y3 \3 ] |; D2 G
//设置name元素和age元素的内容并连接。
9 P2 P$ V0 Q5 c" _9 h6 W9 x TiXmlText *NameContent = new TiXmlText("周星星");
# s' m! M9 t+ j K* r& |6 N6 C TiXmlText *AgeContent = new TiXmlText("22");
$ I8 X9 `- m8 d NameElement->LinkEndChild(NameContent);' O/ S2 m( z0 |. n: w9 Y
AgeElement->LinkEndChild(AgeContent);( `- w- o# X: `! ]
CString appPath = GetAppPath();
$ ]; w/ Q" |6 Z string seperator = "\\";: j0 N, D, }* _1 y
string fullPath = appPath.GetBuffer(0) +seperator+szFileName;1 z1 \* T) Q1 M/ s) n7 j: r
myDocument->SaveFile(fullPath.c_str());//保存到文件3 L! F: F+ T( x, v. E
}
6 S/ E b! k/ ?+ R: c catch (string& e)
1 }) N+ B; t! W r' k# F {3 v. v' {% x% [1 ~( v
return false;# O% N* C' ]8 g( [2 M
}
. r) M# g+ T( b3 q; ~3 Z return true; X4 x# V9 F8 {9 f5 t
}
8 _6 ~$ v" z! ~, |8 |" T0 n* L3 O! g; b' u7 n
bool ReadXmlFile(string& szFileName)* y' g7 Z8 D3 Y; h j- F
{//读取Xml文件,并遍历/ z. l: H& L# J* Y
try
# l w3 I1 a$ r- g" B7 X {
3 Z2 ]; E3 P5 T: s5 q CString appPath = GetAppPath();# [& K8 {% n" G z) d& S* `
string seperator = "\\";
+ b' ]* g2 O% X0 c( O string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
9 h, f7 Q/ B+ M* Y3 z0 q //创建一个XML的文档对象。
' `/ d6 g& R4 R TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());
- c: ]9 U% a$ m6 g8 V myDocument->LoadFile();# C4 n$ U# J. D6 e: a2 ~
//获得根元素,即Persons。
4 a' r; h4 a6 R# ^, w TiXmlElement *RootElement = myDocument->RootElement();5 \8 B s4 ]! j$ b1 C# c
//输出根元素名称,即输出Persons。
6 N5 k1 x) s9 H cout << RootElement->Value() << endl;+ y+ ~5 ^& g) L: _. ?1 ?
//获得第一个Person节点。
5 b& t6 x$ ]% I4 m- f" D7 S% E! l4 R, w TiXmlElement *FirstPerson = RootElement->FirstChildElement();
2 Z+ V6 L$ _9 V+ N% u) q7 _; G1 \5 h' x //获得第一个Person的name节点和age节点和ID属性。
' ]' N- X; V1 |+ U- k TiXmlElement *NameElement = FirstPerson->FirstChildElement();8 }9 d% H4 t, h/ X/ Q5 L
TiXmlElement *AgeElement = NameElement->NextSiblingElement();# Y; b1 y$ X" b
TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();
- B0 w) a- o7 |* h% _7 z //输出第一个Person的name内容,即周星星;age内容,即;ID属性,即。7 W+ s; v5 D, `; o7 D; o) U# Z
cout << NameElement->FirstChild()->Value() << endl;0 ~+ H6 E1 P6 `% b: ] c; t" X& r
cout << AgeElement->FirstChild()->Value() << endl;
% Q2 b2 ?$ Q9 q# g% g% g: | cout << IDAttribute->Value()<< endl;$ Y+ a( g. n/ R P4 `0 }
}& |7 b# Y3 v$ w& W
catch (string& e)* W4 {! K3 ~$ `( }
{
) F/ z- m! x( A3 o- U7 E return false;# e1 t) Q& D; O! s2 O
}
) }5 m# K9 D' M" j return true;/ ^: ^3 k) h7 i4 u
}
$ j* ^7 h8 \ K5 P$ J& [# Dint main()
" S+ Q% X4 `, b0 M1 M4 C{; f9 t. N' G, a7 s! D2 t
string fileName = "info.xml";9 l1 d$ {& q6 G
CreateXmlFile(fileName);
% h% ^$ v; @8 M: }, x- q: h/ d1 n, N ReadXmlFile(fileName);
1 y9 R+ W8 w2 L! [% H+ @}
" N3 m3 u$ f; Y0 @6 T& s5 D7 ^" F( W a' L4 Y: _& E
4 h6 `- M) Q. r2 I5 c3 G. i, S
|