请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好。 TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。 DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。 如下是一个XML片段:
! U) I7 O$ t2 J6 z4 v/ y ` <Persons>3 A8 ?, v+ X7 b5 u* l
<Person ID="1">) E L) @" ?( ]7 ~& j2 E. y9 G
<name>周星星</name>
. \7 D1 K; w F9 A( V- O/ b <age>20</age>. T P( |# ~7 I. C0 I% w0 d
</Person>- p, C9 X# f: M9 `0 f- `( {
<Person ID="2">
) b, F/ k( l& n <name>白晶晶</name>+ A: @$ p ^) ?$ G y2 t
<age>18</age>
, N: }) o$ J" i9 f </Person>* t4 n( S/ x! G
</Persons>
, s: ?5 Z4 S$ Y$ G
6 B9 |, u6 W; c1 w9 s# i 在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文件:
5 [: N8 T8 m. M6 y9 q B<Persons>$ N+ s1 |2 B1 I+ \
<Person ID="1">, D; {1 J* e* ~4 }
<name>phinecos</name>/ Y/ r! M/ z, s$ g$ @# Y
<age>22</age>! {) n4 q4 k! }
</Person>8 k: \# @0 ?. a6 V' f' D
</Persons>1 T: U# k# b: N5 g6 P% q; p* p
3 J Q( u$ b9 q0 T
读写XML文件的程序代码:
& N6 d6 J' E3 ]* @ #include <iostream>
. g" o, u$ V8 D* E: D7 d#include "tinyxml.h"
4 M& j/ L: u8 ?3 s3 m/ N- \#include "tinystr.h"
_- C' \( l& w' ]! G2 X( @#include <string>
* f3 _5 q2 z3 q ]# V8 k3 N#include <windows.h>! C1 E1 A$ v5 k
#include <atlstr.h>
+ _# } V! u+ \( d, ausing namespace std;4 @6 \" E8 q' I
( g7 J, H3 j; F, X: Y( i
CString GetAppPath()
& B( z% w2 l9 P; `. v) L{//获取应用程序根目录% L" n" }% V9 p! T) z
TCHAR modulePath[MAX_PATH];
0 f, z$ ^0 y6 I& n GetModuleFileName(NULL, modulePath, MAX_PATH);
3 X) e) O6 _9 A4 V+ i$ c# O1 ? CString strModulePath(modulePath);
! T1 ?9 P7 ^/ e$ z6 i' ` ] strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));! ]4 e! w& r; Z3 d }3 [
return strModulePath;0 U8 @+ m9 @, h8 `; E0 |6 y
}$ B7 M, Y4 F: v4 z
T3 K0 R' y; a4 U( N& ubool CreateXmlFile(string& szFileName)/ q' R& A* i0 a V$ T7 F. f2 `+ P
{//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false/ b, _9 B, H6 ]9 b9 E) }
try
$ W9 G L0 {4 A0 ]% q {( d9 q1 q& R# u1 _+ e4 ^* V% [
//创建一个XML的文档对象。; g9 C; _6 K$ h
TiXmlDocument *myDocument = new TiXmlDocument();
) e! l r$ g6 J+ Q0 U+ X //创建一个根元素并连接。
[; C+ w) R! L( d TiXmlElement *RootElement = new TiXmlElement("Persons");( S1 P* |$ E R( Y
myDocument->LinkEndChild(RootElement);
5 a B/ i1 x" S9 h //创建一个Person元素并连接。. c/ r- ^1 u0 \; l+ T
TiXmlElement *PersonElement = new TiXmlElement("Person");
1 P' ^# H3 L/ t9 l& Q- B# y/ _3 V RootElement->LinkEndChild(PersonElement);
. p$ P; w$ i, W9 x4 w //设置Person元素的属性。
' q7 h5 E+ {3 U8 Y PersonElement->SetAttribute("ID", "1");. _' B, { F8 P2 ~! P0 q3 ?! y! k
//创建name元素、age元素并连接。8 `) R' d, h+ q8 S" r3 A" t* Z& l
TiXmlElement *NameElement = new TiXmlElement("name");
, c$ R, p* ]0 U8 j5 k Y TiXmlElement *AgeElement = new TiXmlElement("age");5 m5 b1 `* @0 w7 Q6 e7 d: f% B- r
PersonElement->LinkEndChild(NameElement);2 j- ]7 l8 D1 t% P3 M+ d
PersonElement->LinkEndChild(AgeElement);/ Y5 M6 n% O2 a' S) k5 x' y; P7 S
//设置name元素和age元素的内容并连接。
! M: b% o z, i" E/ _ TiXmlText *NameContent = new TiXmlText("周星星");
. A1 d0 F0 U% K6 J8 m( e) Y6 O TiXmlText *AgeContent = new TiXmlText("22");) o" X2 D; Z7 @6 z5 E/ \
NameElement->LinkEndChild(NameContent);
& T6 @7 U( v) p9 I; ] AgeElement->LinkEndChild(AgeContent);
5 A3 b9 {& [" s! f# c: Y CString appPath = GetAppPath();- K9 A3 B3 d* y4 j* y& H
- Z7 e, ^3 f1 `/ R" t8 s1 E+ E/ a& A% B
登录/注册后可看大图 string seperator = "\\";* E& n. E4 C9 X0 D. d$ l
string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
3 z- F' k- h4 Y0 R& T. F myDocument->SaveFile(fullPath.c_str());//保存到文件) G* ~3 j4 S0 F+ S" ]
}
, V# X6 @5 _( ` }/ ]0 P8 V catch (string& e)
& X8 x' t. @7 k% h! n3 @ { S) ^8 y3 M$ Z8 d# o3 G
return false;
; D9 k. B6 r6 h7 B }
% ?8 m( w& Q- L6 x" z4 Q- h$ H3 S return true;
# B+ T; T- I) C2 O" R}9 ]! k! w- W6 Z5 P( J3 L7 A7 P
3 m! l0 n9 ?) bbool ReadXmlFile(string& szFileName)
, }. e8 K" l5 \% \, z# {; o$ w{//读取Xml文件,并遍历
& G% t7 ~- a# L: q: Y) N' g try
: o+ }' i) k: w6 H; e: L; d/ G {
8 J% z: q$ V! j! j. f, I+ h CString appPath = GetAppPath(); I4 e# u' Y, p6 E; }
string seperator = "\\";
, ?# R7 q# N' ]* ~" L4 T string fullPath = appPath.GetBuffer(0) +seperator+szFileName;/ ?" s$ o; r9 U! i
//创建一个XML的文档对象。
) r+ r6 ]6 W* e# [' ^6 ^/ ? TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());
" O) q9 s0 Y' {2 g* M myDocument->LoadFile();
# P) s) y7 z& w //获得根元素,即Persons。
( n4 z6 o* K; I TiXmlElement *RootElement = myDocument->RootElement();
' R% h0 }# w2 S" T7 a8 Y8 x //输出根元素名称,即输出Persons。
# P* {5 \% f L' M1 i" I cout << RootElement->Value() << endl;# R2 J8 U# [5 o! t5 o
//获得第一个Person节点。8 ^( F4 ~3 I7 F
TiXmlElement *FirstPerson = RootElement->FirstChildElement();
, h& r: _; R' q. D- \ //获得第一个Person的name节点和age节点和ID属性。
3 |' K4 w/ m' r! D, `5 u0 q8 x$ y TiXmlElement *NameElement = FirstPerson->FirstChildElement();
: |9 f0 s3 Q- x# u- E TiXmlElement *AgeElement = NameElement->NextSiblingElement();
$ e" I! F# A* @! g) n( m9 p TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();" M3 p6 r" V& m4 J/ [
//输出第一个Person的name内容,即周星星;age内容,即;ID属性,即。
0 j0 Z/ D: _( o3 J; g- A) H cout << NameElement->FirstChild()->Value() << endl;% T% G8 W& B' ^+ n* u( x& V
cout << AgeElement->FirstChild()->Value() << endl;. O4 h5 X; r3 s) l: z" }
cout << IDAttribute->Value()<< endl;
( k- e/ r& v& Y) l% ^! L }$ }: W* w6 Q! k+ ]
catch (string& e)
$ A# _' ?$ H$ }- g9 K {3 D8 l. ~# c2 [. w
return false;& p7 e$ h' \& B" e1 S
}9 R6 y+ Y) q) R6 j- k3 v
return true;( N( h( e# q' M; Z3 i/ `9 K' v
}% w# ?( X d6 q+ T
int main()
: Q+ A4 |% r! D6 z7 F3 g1 F{) t t& v+ k* p6 [4 A, {
string fileName = "info.xml";. }: g, T/ E7 f% F
CreateXmlFile(fileName);
" o$ W! q/ Y+ M ReadXmlFile(fileName);
+ i& ~& w5 Y5 a: g6 P! O. k n; i}
6 _. w$ N4 V7 }. ~, y4 K, m1 t/ _$ x2 N* `
/ D4 ^3 }* O: I) F+ R |