请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好。 TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。 DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。 如下是一个XML片段:
9 ?/ M: l" _5 A <Persons>3 r. h- b6 t& h6 r
<Person ID="1">* r( s2 f$ W- F" ?5 u D- U
<name>周星星</name>
) P5 W) r( F3 c, d; u c <age>20</age>
+ o' v' B$ l& |3 o </Person>
K: [% b% b! c- } <Person ID="2">
' J4 M5 }$ e: G) o$ W: ] <name>白晶晶</name>
, n1 n0 M% {5 P <age>18</age>, O9 ]5 S* G/ |, P E
</Person>
+ @! z; C5 q# v4 ?( I2 q2 s </Persons>& n7 v+ M7 f- Z# s6 m5 ^" f
5 h$ m# D. ?7 ^4 l: W* @0 n+ z
在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文件:6 j9 ?* q0 `% \% W
<Persons>7 L* v, h8 G( B% F; x& j" D
<Person ID="1">
) D1 ]. A1 h* d; z" o$ H <name>phinecos</name>- I8 ~( }" W0 D" Q- t! Z1 J
<age>22</age>
! k7 p/ O& f0 m( S9 s& Y. T6 R </Person>
[+ Z3 F- A0 |8 M7 q6 B</Persons>
6 {# [- S# j8 I n4 m6 A+ P) D+ P) a# M6 ^
读写XML文件的程序代码:
, X/ r& j# d( ] #include <iostream>
& w: {7 L+ F- M0 v8 B5 e3 T, e B2 G#include "tinyxml.h"& g4 q( E* u9 G% [- g, c& G9 |
#include "tinystr.h", i N9 n9 C: o1 M! o2 H
#include <string>
0 \8 f; P; I* Q: r3 z#include <windows.h>
( S- r8 P& ^5 U- p$ y. S& ?#include <atlstr.h>, f2 l, Z/ C2 x0 w0 g
using namespace std;
! u1 D' [6 x6 H1 a8 B# R& N' N
% x" X. P$ J/ JCString GetAppPath()
! B+ g- a+ n5 a7 [" O7 M{//获取应用程序根目录: b4 n( S" @# h
TCHAR modulePath[MAX_PATH];, W5 M: E- r; l- G( L* Y
GetModuleFileName(NULL, modulePath, MAX_PATH);7 f. `. o- s% x
CString strModulePath(modulePath);
. Y7 P9 i# \0 D strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));
5 c( v) d' {% Y6 a! H2 z return strModulePath;0 o/ t. G6 g5 E% _1 f% w. E8 z& @ s
}
1 ~" a9 o* A, n' \; ]
+ ~ b3 y+ x1 p: n2 W Wbool CreateXmlFile(string& szFileName)
3 E+ R) i3 C. n. Y1 n; I' N" h{//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false/ ]3 w; \$ Q6 X" e& ~# B- U9 U
try5 U2 N9 D4 M/ x" I
{) O. h& i. _& w
//创建一个XML的文档对象。1 j- B% L1 s6 O( d
TiXmlDocument *myDocument = new TiXmlDocument();
* d" ~! {) d' y( C+ q) e0 \ //创建一个根元素并连接。
9 ?+ ?8 c8 x' x: t L+ ~ TiXmlElement *RootElement = new TiXmlElement("Persons");8 a; Y6 h/ Z; b5 v
myDocument->LinkEndChild(RootElement);, P! a& @1 H$ Q
//创建一个Person元素并连接。; v5 V& F# O" ?5 s/ D
TiXmlElement *PersonElement = new TiXmlElement("Person");0 N- I0 f9 g, u) s& F2 F0 ?
RootElement->LinkEndChild(PersonElement);# a6 h. t {$ f7 m' X' B; y& p
//设置Person元素的属性。
% v$ T0 h" l( r2 e# U PersonElement->SetAttribute("ID", "1");
1 Z9 W+ S0 D8 m' J5 O4 e //创建name元素、age元素并连接。
+ g3 F, _0 ^- n- _+ S' K1 K TiXmlElement *NameElement = new TiXmlElement("name");
# Z6 z& }1 O3 H( s3 G; z1 \ TiXmlElement *AgeElement = new TiXmlElement("age");- h- i6 M$ T& Q# a
PersonElement->LinkEndChild(NameElement);
( d' b) s. }5 S: H PersonElement->LinkEndChild(AgeElement);
7 R0 j+ M3 O$ c //设置name元素和age元素的内容并连接。8 c4 Y+ Y6 i4 [! ~3 W4 Z
TiXmlText *NameContent = new TiXmlText("周星星");
, d0 G( K/ I8 T7 m% O! D( R% j TiXmlText *AgeContent = new TiXmlText("22");. N+ e |% o- m/ q
NameElement->LinkEndChild(NameContent);3 T+ z/ D6 L+ z* Y( }
AgeElement->LinkEndChild(AgeContent);
9 t4 V) K) x5 o+ i CString appPath = GetAppPath();
! a1 B, {" p7 U! b0 y: S string seperator = "\\";5 z6 e/ R; N5 `& o3 W. Z( h: b5 w* l
string fullPath = appPath.GetBuffer(0) +seperator+szFileName;/ t- V2 @' Y0 l; I
myDocument->SaveFile(fullPath.c_str());//保存到文件7 A( W% m O/ A1 C& l' P, @
}0 k1 U" s6 Z6 ]& u
catch (string& e)
Y5 P) }7 Z3 N5 _3 \ {* k0 h1 F6 w9 t( v
return false;
/ d! ?0 g/ M+ E4 h! N5 L& E }
5 s- ^4 S1 U$ l/ Z% Y" k4 ]2 K return true;, M; H2 y2 L2 Z% U
}7 l$ X1 W' W2 s+ r# S K
! Y1 J' J% W9 S) ^. G
bool ReadXmlFile(string& szFileName)
$ ]* a! h) _$ ^2 u0 `8 Q{//读取Xml文件,并遍历
3 A9 D. O! K$ ?; k5 C/ S/ _ try! d4 {* \& s1 C) v
{! L- H: y: X5 ~! Z
CString appPath = GetAppPath();
, I( C! v( {( y) } string seperator = "\\";
/ [3 G" s0 E( @ string fullPath = appPath.GetBuffer(0) +seperator+szFileName;* h9 _4 D* f( o/ I
//创建一个XML的文档对象。
N; o2 f7 z: y/ s TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());
" C3 H* x& j; B; T! f0 ] myDocument->LoadFile();
+ S l8 B" }4 q2 w6 @+ L7 w- W //获得根元素,即Persons。
8 L% ?3 {/ R/ ]9 T- x1 B TiXmlElement *RootElement = myDocument->RootElement();
1 J) _. D( T# U# D" N //输出根元素名称,即输出Persons。
$ V7 H( a2 ?2 `' {6 c cout << RootElement->Value() << endl;& M2 S% I/ [ T! M7 J, [8 c
//获得第一个Person节点。. d) M z" N8 H) [: R0 [
TiXmlElement *FirstPerson = RootElement->FirstChildElement();
9 ?; U" V2 n {( q+ |' F& _2 K/ P //获得第一个Person的name节点和age节点和ID属性。4 R/ h2 `4 [) ^# i5 W
TiXmlElement *NameElement = FirstPerson->FirstChildElement();+ G6 w" R* _% @* J( r0 H
TiXmlElement *AgeElement = NameElement->NextSiblingElement();1 P4 J) X7 O* x) a" q
TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();& G3 ^( k2 `! E* S- B3 \
//输出第一个Person的name内容,即周星星;age内容,即;ID属性,即。
- Z; V% D5 Y5 l4 A- J: g cout << NameElement->FirstChild()->Value() << endl;
2 R- w. `( W, N S" c! O! ~5 g cout << AgeElement->FirstChild()->Value() << endl;
3 `6 S8 ]& e, ^( r) s; W cout << IDAttribute->Value()<< endl;
_7 q8 s& L2 q. H; G }) o3 E n T1 n4 i( o
catch (string& e)6 o- P" t& Z# O% ^# f2 P
{1 u: A7 P0 t; {
return false;
- F! d8 V8 ?1 J! C( O) O }
6 J; }; D( a/ A8 ?: l return true;
( ^0 l1 K" K0 l; c}
3 r0 ]; H' J' g' c0 _2 ]int main()
& q- x+ B2 d+ k, x; h* n{
1 v m, ^$ ]- _! N. ]' W string fileName = "info.xml";
* u8 y; D4 X( `- S' U W CreateXmlFile(fileName);
( r8 p0 D% {1 }1 l ReadXmlFile(fileName);' @) m8 @6 o5 X' J
}+ H1 u1 X8 V0 I% \' {; u" X9 e
( H. b6 `9 v# S* n! p3 r0 I! |9 c
5 l# L( h. w/ ]: X% X2 Q0 Y |