请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好。 TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。 DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。 如下是一个XML片段:( d9 ?" g1 M4 c5 C& x
<Persons>
8 v- S# {; D6 k2 g4 S" s <Person ID="1">6 M, m+ K' e/ x9 m! W
<name>周星星</name>
. d G* i4 [2 N" G4 |' X6 f <age>20</age>
8 y$ `: e& j$ Z* R </Person>/ x& g* L. X3 c4 F
<Person ID="2">. i) D n) G% f: Z$ j) s7 \: q
<name>白晶晶</name>
& e: e$ l; Y. ?, ?, Y <age>18</age>
8 Y9 C' ?9 a( W J$ l5 q" y! d1 b# u </Person>7 y) L3 n6 _" c: M8 x4 Y7 z
</Persons>( p/ ]5 V/ h/ ^. _& C" E
1 f1 l/ t+ ^3 L f! ?
在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 H6 d9 G. ]" L<Persons>9 w7 q Y1 ]% s: Q, O& w" f( \. `6 z
<Person ID="1">1 A9 E, U: R3 w) {8 {" G
<name>phinecos</name>
9 D/ Y# O' W: M/ s r* \ <age>22</age>, q) c* p8 v( R! k- f5 O: L9 y
</Person>; K- b8 G. X; q! x; o; i# k- C' o6 C
</Persons>% R! W) u. l! Y( Q, E3 k0 w; _! r/ u
/ ^- U6 d- |) n0 E6 \3 W读写XML文件的程序代码:
4 ?& X5 F9 E) z: ~/ R- c #include <iostream># K$ i, r, s) J$ t6 K! E3 [
#include "tinyxml.h"( Y. u: o, V* y! P9 b: q, E
#include "tinystr.h"6 ?0 O! ?3 O o% K
#include <string>
9 a. M, q' S1 f+ e: K+ v" T q#include <windows.h>
3 R- q( I' ^2 \( X#include <atlstr.h>9 ~ n, D2 s# D6 f) k
using namespace std;
6 X8 Q; p, a- {7 A
. o- y, u* c5 i D" p: K3 lCString GetAppPath()
4 q( l( T; H2 O0 U{//获取应用程序根目录
N0 q4 o3 k3 x5 x: Q5 B% d7 U* I TCHAR modulePath[MAX_PATH];8 B6 V0 P3 k2 r/ [! O) S5 G( \
GetModuleFileName(NULL, modulePath, MAX_PATH);* y1 ] V2 s) m/ l; S# v3 s4 a
CString strModulePath(modulePath);% U+ c; m$ G& {1 j }
strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));
* v8 w' @7 O( V. Z" H; O* ? return strModulePath;
; y2 Z/ T$ |( {2 b+ `( w& U}4 L3 M. t. a- F, U1 f- N
$ q8 C4 X9 V+ ^+ K; m/ _
bool CreateXmlFile(string& szFileName)( Q- Q3 D/ b) n8 x5 G( p
{//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false
' U. O# \+ H: a. J. B try7 r. M# `6 b3 Y/ u* r1 h+ U- E7 O& ~
{
* F" X/ ~+ m2 l1 w" A5 [ //创建一个XML的文档对象。% ^! |6 _) B$ T, C p% J
TiXmlDocument *myDocument = new TiXmlDocument();
: L; p1 @# \8 r8 c0 ? //创建一个根元素并连接。% F' l( D( V, L& L
TiXmlElement *RootElement = new TiXmlElement("Persons");
, X+ [# U" i2 L2 A myDocument->LinkEndChild(RootElement);
r! X8 z" |( c: Y //创建一个Person元素并连接。1 M) e: k6 N3 M# ?$ d8 ~* ~
TiXmlElement *PersonElement = new TiXmlElement("Person");
8 C2 C4 g2 c( G" p4 `8 F1 C RootElement->LinkEndChild(PersonElement);2 e: i9 k, |0 [6 [# w
//设置Person元素的属性。7 V5 n" b7 P, V
PersonElement->SetAttribute("ID", "1");: R( z! {/ y6 N' S& s% u- b
//创建name元素、age元素并连接。: x9 l0 r3 L# O2 J
TiXmlElement *NameElement = new TiXmlElement("name");0 {+ R- X" ?, Q. o4 W1 b# ~# S( {
TiXmlElement *AgeElement = new TiXmlElement("age");
. A( R1 W9 m' d4 Y/ z PersonElement->LinkEndChild(NameElement);
+ V' X9 W9 s3 f2 \0 o8 ^ PersonElement->LinkEndChild(AgeElement);5 u/ ~. e7 K1 T& q$ p: F
//设置name元素和age元素的内容并连接。
: [7 g* l. s' ?2 h3 G) s! } TiXmlText *NameContent = new TiXmlText("周星星");
) C2 m% S q0 E# T TiXmlText *AgeContent = new TiXmlText("22");
; e9 T Y2 P0 Z' k; C NameElement->LinkEndChild(NameContent);
8 v7 C' G; i4 B) U7 H5 o* K% V4 d AgeElement->LinkEndChild(AgeContent);
& `9 r+ z6 B" a6 s; u CString appPath = GetAppPath();
! N( M. n4 W* O) E/ N; g3 x/ z+ E string seperator = "\\";
4 Q& I2 T# q7 w! A( k5 ?( f string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
5 I. r1 a! h5 `5 e) Z7 g2 }7 P1 ] myDocument->SaveFile(fullPath.c_str());//保存到文件
w. u; O' W/ q1 R }$ E" s6 u' O3 z- H2 `4 y8 ~
catch (string& e)
- M5 S! n, s% I s4 J0 ~3 M {6 R0 M& [" P4 A8 b% u, }
return false;
" o% L- ^' n4 {$ J M }
; |9 g* j4 ?# T& l$ g return true;0 [7 W* U1 ^& u9 K4 H) O7 o
}5 R! E" }$ `2 ^: U: p
1 Q+ l, n1 Y$ j. H$ D6 t1 D+ I, q
bool ReadXmlFile(string& szFileName) N9 u& `9 ]7 j" f3 s4 H, r
{//读取Xml文件,并遍历
! ~# z5 |9 t6 }; o- a7 v8 v try
: l! M1 y0 T. e3 z+ M& C( h5 r {3 |8 ~+ @9 \' g/ |
CString appPath = GetAppPath(); q6 e# p( O2 k/ p R
string seperator = "\\";
; k4 U- K5 c4 |9 Q$ m m string fullPath = appPath.GetBuffer(0) +seperator+szFileName;: f, x' g: V7 b3 X* V
//创建一个XML的文档对象。
/ x+ Z: a! d6 Q! M' p1 z) V TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());
3 Q' M- H$ Q. X! y5 R9 } myDocument->LoadFile();
6 a" ^. b9 d5 x( s //获得根元素,即Persons。
7 j, R! A: G( j TiXmlElement *RootElement = myDocument->RootElement();
, l; z; ~9 r9 X/ _) e1 K$ }2 ? //输出根元素名称,即输出Persons。2 Y- X5 \" j1 D2 o5 x$ p& w
; F) s1 I' W& g" H( M& k. y! N& O: g& z 登录/注册后可看大图 cout << RootElement->Value() << endl;
8 w, Y9 N( w5 {% B7 m //获得第一个Person节点。' i. g1 v4 w2 M/ K& M) b1 l
TiXmlElement *FirstPerson = RootElement->FirstChildElement();8 n! Y) f. `" _$ r( @4 |
5 [& a2 c! H ?* h7 Z4 w: j- a% O0 N$ @, U 登录/注册后可看大图 //获得第一个Person的name节点和age节点和ID属性。
! g% g% Z5 o$ f3 w8 M' n TiXmlElement *NameElement = FirstPerson->FirstChildElement();
' h- V. l3 m! j7 K6 Y TiXmlElement *AgeElement = NameElement->NextSiblingElement();
6 |# Q. ~) G1 {, u- o" R TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();0 C' Y5 J" ^: ^( I
//输出第一个Person的name内容,即周星星;age内容,即;ID属性,即。1 ?) w4 [1 l$ v9 `. U, k8 z% E
cout << NameElement->FirstChild()->Value() << endl;0 I7 f3 E9 _, ]0 b9 d
cout << AgeElement->FirstChild()->Value() << endl;0 N. Z7 q3 W \) R+ z
cout << IDAttribute->Value()<< endl;
. q, J$ w: W" P. {1 O }
* i k' S6 D- \ catch (string& e)
9 H' S* Z) o! U% P {
+ D( U4 u6 e$ ?* Q5 i- Q! R return false;1 s0 T0 a: }; y& J
' E% ^3 ?* ]' z" \" f, B8 r+ c+ t6 h1 b
登录/注册后可看大图 }
8 _2 c. O/ R4 U) N2 J return true;
( B9 ^, y y% B# ]1 F( J}
% W8 K5 h3 Q# j' eint main()0 R, G. G' |/ x# l% M# Q. b
{1 t8 @' a6 }5 n2 e
string fileName = "info.xml";8 y# j7 Y( |0 d7 h7 U
CreateXmlFile(fileName);
! |( T/ i; |7 W# c ReadXmlFile(fileName);
( B$ `% s) L4 N) B: C# e}
/ C; F4 W b8 p9 S! d% C$ M2 |' }4 y. B
3 s& a- X! x0 c% ~0 L. \/ ]
|