请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好。 TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。 DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。 如下是一个XML片段:! q1 e; X9 ]- \" E& u
<Persons>& b3 U$ g( R- ~# \2 L
<Person ID="1">
5 i8 v1 e3 i; `( r <name>周星星</name>
F6 ?; s3 d; `+ L1 H2 w <age>20</age>; ]5 a2 U* x& g$ |+ E
</Person>
& Q1 u/ c1 x1 {; |$ B' _8 E <Person ID="2">
; e( v% d* L5 C! m. N$ O* \ <name>白晶晶</name>
6 g5 o9 ~; B9 K0 ^( G D% o <age>18</age>
7 f" r7 T. d! z4 S </Person>4 U( M( w8 [ e6 u7 o# f3 o
</Persons>% t" s6 u. [' k8 N( M5 O
) R4 A- u! o: d5 n" 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文件:0 r" ~( _; Q9 `. T+ R* |4 k: Q
<Persons>: i* g7 {9 B1 b" M* P
<Person ID="1">8 P- i# a! \6 H z, I, E
<name>phinecos</name>
- q- x% X3 x5 l <age>22</age>1 Q% i" b2 `" i4 Q: u
</Person>3 `0 }. V, O! \0 h
</Persons>
& Y8 D7 m5 R) j! Y# ^$ d+ j
( v6 u0 [! w, h! x/ w2 s读写XML文件的程序代码:5 `+ X! }% \! q. Y& @
#include <iostream>- Z3 c6 z4 I6 K. Q) |# g( S Q2 x6 R
#include "tinyxml.h"
/ x! o# M1 p- p Q0 B#include "tinystr.h"2 W' f0 K2 D2 J: R* t# v
#include <string>) _* Y) M% @# @
#include <windows.h>
/ }& L% E- i6 |" a#include <atlstr.h>
' q2 j- q% g* a' `* d( nusing namespace std;# d# B! A$ C8 ]" j9 d
7 R! }7 N1 A3 {6 H" T+ U0 r
CString GetAppPath()% ]6 J) q" |% Y' q ~& m O, ^
{//获取应用程序根目录3 |. @- r2 |9 d1 a- x" B4 ]2 E
TCHAR modulePath[MAX_PATH];( ~. c* y) _/ p, s0 P3 e% L, d
GetModuleFileName(NULL, modulePath, MAX_PATH);
5 J; N h+ u" n! U" M1 q- v CString strModulePath(modulePath);
2 Q9 g' r# }& h. r- e- V strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));
+ k0 I4 _2 q' z0 T7 N+ s return strModulePath;
: y/ J$ _) k) s6 Y7 z* o& l}
2 a5 x$ ^; S, m3 Z; P# @4 g4 ?( D8 }! N+ t( F, T
bool CreateXmlFile(string& szFileName)
- F( N+ I. R6 `, `{//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false: ~& s9 y. H' o _/ O. A
try
4 P+ L6 ?. Q, x: _7 D# L/ H {$ ]$ U6 B. U) [3 \6 I9 W0 K
//创建一个XML的文档对象。
1 @3 w1 } a o) T3 O2 j TiXmlDocument *myDocument = new TiXmlDocument();4 s, N+ F4 M3 t& L
//创建一个根元素并连接。
4 `5 C, p7 H) q% H+ L4 g. {* [ TiXmlElement *RootElement = new TiXmlElement("Persons");5 o# A, \9 ]2 Q O# V8 F: d
myDocument->LinkEndChild(RootElement);
; s8 R8 Z& a3 G9 n& U //创建一个Person元素并连接。9 V$ `8 a, w& l0 _
TiXmlElement *PersonElement = new TiXmlElement("Person");
# F3 J3 x* D1 w. O# } RootElement->LinkEndChild(PersonElement);0 \) Y, C. v8 m( v8 N H
//设置Person元素的属性。2 ^& d$ J$ q6 L
PersonElement->SetAttribute("ID", "1");5 A" h5 d; o6 K0 f
//创建name元素、age元素并连接。2 y* K' \& n e+ s
TiXmlElement *NameElement = new TiXmlElement("name");
: o6 D) o3 l1 u e$ n, v V TiXmlElement *AgeElement = new TiXmlElement("age");
9 Y* P7 t. d6 t PersonElement->LinkEndChild(NameElement);; G. O' A* l s1 W- G4 @/ T- _
PersonElement->LinkEndChild(AgeElement);
) s, z2 S; z% d. o+ {0 A8 j$ o* P7 N //设置name元素和age元素的内容并连接。
9 `6 [, [6 H/ j$ _" O' j! B5 ?8 K TiXmlText *NameContent = new TiXmlText("周星星");0 s* r5 n* T5 B& D- s
TiXmlText *AgeContent = new TiXmlText("22");; }1 o8 n9 V$ I) A5 _2 s
NameElement->LinkEndChild(NameContent);
! R! C/ f$ a" |+ k, U7 m AgeElement->LinkEndChild(AgeContent);0 S* b5 Q8 \: x( p) m! M
CString appPath = GetAppPath();
$ Y3 Z- J2 t" W string seperator = "\\";! {' F0 F" F" f& U7 u% A, k
string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
8 j/ E+ |* _; v( u8 P' X5 i myDocument->SaveFile(fullPath.c_str());//保存到文件- h+ k8 D2 ^; h9 f# W; a( e+ K
}
" O! e6 @, | }# z4 j+ H t6 A% e6 d catch (string& e)
7 e+ k7 Z9 A4 H4 T0 r. m/ m {
- O# c# I, p/ G; p! y, o return false;' ~; e2 I: j( E
}
3 H" {) g2 {' Y' o3 [ return true;6 G# y) q( _) `' k7 D4 M
}
1 K4 F* \2 W/ @* I4 |6 ]; C3 ?
" K3 b! c; k2 @! ^$ gbool ReadXmlFile(string& szFileName)+ j: x" a7 c# |2 W6 }
{//读取Xml文件,并遍历
: V. i" U v7 [) Y' @* q try% G ?) U% w* c) a2 M5 k
{
. t4 l0 S) Y, {) l4 s CString appPath = GetAppPath();
2 C3 G* g5 _: d. P1 x1 j- E! Q string seperator = "\\";4 U; P+ p( z6 c O0 B
string fullPath = appPath.GetBuffer(0) +seperator+szFileName;- T8 R+ I# G7 a( U# O" n; _( s
//创建一个XML的文档对象。
: C+ k1 r* ?/ C6 T+ L TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());9 C0 k" }6 R- ~4 j. S
myDocument->LoadFile();5 W, v/ P; k! v) R3 m( t
//获得根元素,即Persons。
* q/ w# K% J+ `7 r" N1 E TiXmlElement *RootElement = myDocument->RootElement();
6 f4 E- C) r. a# e' e K //输出根元素名称,即输出Persons。
2 Z5 S) B) L+ P9 | cout << RootElement->Value() << endl;
- |% y) X6 H! Y# x, b //获得第一个Person节点。1 r- ~6 E% d/ O' S# o/ }
TiXmlElement *FirstPerson = RootElement->FirstChildElement();. r7 U) t) Y- S8 |5 g
//获得第一个Person的name节点和age节点和ID属性。
5 F U5 r( r- T" _0 n& a2 V/ A TiXmlElement *NameElement = FirstPerson->FirstChildElement();- s/ ^2 R3 N, x7 j2 I
TiXmlElement *AgeElement = NameElement->NextSiblingElement();
1 s# q9 E8 P5 {- w. c8 ] TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();
) p9 b# i; Y% {% P/ B" x //输出第一个Person的name内容,即周星星;age内容,即;ID属性,即。
# A8 s7 I6 n% i' \3 C4 o cout << NameElement->FirstChild()->Value() << endl;6 ]# Q# P3 ^2 @* `4 l
cout << AgeElement->FirstChild()->Value() << endl;
, u/ j: k0 O8 s* b+ k0 C cout << IDAttribute->Value()<< endl;
1 O# ~. q4 @9 g7 p1 |) r }
- ~9 }" m: O4 E+ r6 t catch (string& e)% q. K3 h& q' h
{
1 F9 ^7 I' D9 I: n- G- `3 a return false;. C. l4 i4 \( T' H9 K) M
}
! L# s3 Y4 g; h& o8 Y J& E return true;0 ]9 ?1 i% `- h3 I) o9 V
}
# r1 d. g6 y5 Vint main()+ |4 v0 a! j: `3 ?2 K+ I
{
" e( w( y; M& n: u string fileName = "info.xml";
% }$ W! Y/ c) x CreateXmlFile(fileName);( f; ]$ A6 U3 G! l) s7 B
ReadXmlFile(fileName);
* F! c* V/ x' u! u- w}
9 B, W [5 U. ?( t+ R0 [. M% l9 Y8 V" p& B, m0 b6 e }; X
1 h) \6 s" {& Z9 O8 { |