请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好。 TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。 DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。 如下是一个XML片段:' g9 V) m1 d8 O0 X
<Persons>
0 m! g7 |3 J# g/ J) {7 a+ H <Person ID="1">
2 O5 Q. G+ x8 j0 ] O$ h( N9 h1 m <name>周星星</name>
8 b. Y3 w4 g. |4 ~; `2 k/ M1 u" ? <age>20</age>! L) E; ]+ p7 I& M. w
</Person>/ p! D9 s; r, a/ F; F \
<Person ID="2">
" V; |; ^8 n4 q, p& e- ~' d
, @0 l# e6 R& E" A8 x/ n6 e7 p/ ~# Q0 ~ 登录/注册后可看大图 <name>白晶晶</name>
t* z% G7 k9 }' J5 ]/ q) N <age>18</age>
3 B$ p" t& o0 y </Person>$ i6 _5 l0 n" O8 L
</Persons>
4 [1 h: W9 i6 w( m: Y, |3 z8 Q9 }+ W/ w4 `" C8 o7 p
在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 w' E% G% q$ ?2 P
<Persons>
. x/ L }- r; G9 {! a0 z' k <Person ID="1">
5 l6 m1 R* ^! _4 u7 y <name>phinecos</name>
0 Y" n$ d9 c9 y8 i$ l' T' p <age>22</age>
) W3 }& _2 w3 k! c8 m! X, E </Person>
, r* y# P) V5 r% s1 H( W0 M" C+ N9 i- {</Persons>( Q: {& `2 x9 c7 `; ]
# s( E6 \. Y# e读写XML文件的程序代码:
. p# r o" G' }$ t& Q& r #include <iostream>
7 ]& a7 |$ C, @0 G7 x6 j' K#include "tinyxml.h"
. f: y* L! x. e3 _& q
" K- h3 V- Q9 {' F& ~7 c$ k$ ?/ h& Q; C 登录/注册后可看大图 #include "tinystr.h"
5 w9 `3 L' @2 b6 B; t" w5 G#include <string>
. @$ p- @- I/ l8 |#include <windows.h>6 M# ? Y6 V1 r: [
#include <atlstr.h>
6 T& a' N7 _3 B3 \6 \7 gusing namespace std;
* x: D4 e0 `, g
/ C p2 X7 M6 e8 ^3 E- L; h4 xCString GetAppPath()5 V5 z& ?, W! A, g6 N
{//获取应用程序根目录6 w s5 K% B' H; P' U
TCHAR modulePath[MAX_PATH];5 A! m4 z; q B; H' q' D
GetModuleFileName(NULL, modulePath, MAX_PATH);
6 d5 ]. O# u. d( K! i0 l, Y% |' S# M CString strModulePath(modulePath);
& F' w2 H" \$ {. O( W3 l y strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));
5 n, L2 Q# o8 a! Y return strModulePath;
) A# G- O: l w2 B}0 p# |6 I8 x: W
& p, \; X1 H) C4 X2 J& r4 hbool CreateXmlFile(string& szFileName)
2 i) `+ |' Z" f5 J/ w{//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false- H6 u* }# Z2 y3 K7 B" c
try
8 ~& I* q9 }; h+ l0 q, z {
g& A9 f* c8 N o //创建一个XML的文档对象。
6 ~! W1 N( P3 h: s TiXmlDocument *myDocument = new TiXmlDocument();
" Y7 n/ e. ~& r4 d! _! e //创建一个根元素并连接。
% n* N5 @, u3 |' u7 i TiXmlElement *RootElement = new TiXmlElement("Persons");( |. t/ e, L" L% ? O2 X- l
myDocument->LinkEndChild(RootElement);) [; a C' r4 P5 r: b1 Y- z8 W" }, J
//创建一个Person元素并连接。
; Y) n* V9 O9 M2 ] y TiXmlElement *PersonElement = new TiXmlElement("Person");
2 |$ w: j* K& _5 R+ r RootElement->LinkEndChild(PersonElement);& R* C5 c& c* I2 |2 i T- ^/ d
//设置Person元素的属性。
* A/ |; R p ?2 }2 f PersonElement->SetAttribute("ID", "1");
# B9 g* E- {1 {# K //创建name元素、age元素并连接。8 Q1 M1 u) r3 _6 s3 N! W& i- J
TiXmlElement *NameElement = new TiXmlElement("name");
0 J0 F" P$ E& f8 ]; a TiXmlElement *AgeElement = new TiXmlElement("age");
; N, u# I* t( }, R# Z PersonElement->LinkEndChild(NameElement);
& h: r" G2 H; `' Y1 i PersonElement->LinkEndChild(AgeElement);
' I4 B$ |; a+ s //设置name元素和age元素的内容并连接。
. g8 P; f: y( a0 _0 C2 R TiXmlText *NameContent = new TiXmlText("周星星");
v. X5 h5 d9 k6 e2 t6 Q TiXmlText *AgeContent = new TiXmlText("22");5 e" V' B& m& l2 P6 w) B
NameElement->LinkEndChild(NameContent);
0 g# E$ h8 g2 \- j; E; m1 { AgeElement->LinkEndChild(AgeContent);
9 `. Q" [* T! e CString appPath = GetAppPath();
. M/ B' v' a' Q! z4 c string seperator = "\\";- ]* @/ q0 j. O" ]
string fullPath = appPath.GetBuffer(0) +seperator+szFileName;9 F8 e" x ^6 K4 c
myDocument->SaveFile(fullPath.c_str());//保存到文件
. T' G8 ~' _& m! c: y3 u, C }9 l! u" h0 f8 x7 ]1 n
catch (string& e)
+ k- d/ M5 K& ?- M+ \& F4 O {
& y4 M; I$ m: r* z* j9 x return false;; O3 t7 _3 q e8 P8 ?
}
) R! w8 S' K5 p8 U return true;
9 P8 c5 |4 I. a$ o: I% O}& B# D7 d. x* F# ]$ i
% o; a( m, s* Y) ^
bool ReadXmlFile(string& szFileName)% x: ~& r4 h% B: O! ]/ X3 J
{//读取Xml文件,并遍历; R0 _8 ]8 k1 n# A7 n! }+ \
try# f, F/ {% d$ ^$ F* \" b" { Y( d
{' w% v% K% A2 \( c% U9 a# P3 }
CString appPath = GetAppPath();
2 _ n ?, [; p; ^4 g4 z3 T string seperator = "\\";2 Z4 }4 d1 |. c/ Z7 g! ]4 _$ ~
string fullPath = appPath.GetBuffer(0) +seperator+szFileName;2 g+ t8 [; x# `( x
//创建一个XML的文档对象。6 K/ x: V7 F9 t& u! \) f
TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());
& c; l, }' T/ G3 ^4 ]' [: m myDocument->LoadFile();) O; p# p! L3 }+ \& c/ K
//获得根元素,即Persons。* O. D4 q+ @5 b2 E; N
TiXmlElement *RootElement = myDocument->RootElement();3 x% |8 G4 e, l9 z3 @
//输出根元素名称,即输出Persons。
; H$ F8 p3 n! _4 l& _% }. |1 r% f cout << RootElement->Value() << endl;4 [/ _2 u% e! ^% b) g$ P/ Q
//获得第一个Person节点。
0 t. m! E/ i8 c Y* `8 y* S1 s TiXmlElement *FirstPerson = RootElement->FirstChildElement();$ @" o! E, C6 ?6 ^' j# b5 l+ P
//获得第一个Person的name节点和age节点和ID属性。
2 z- ]6 J: E4 P+ x TiXmlElement *NameElement = FirstPerson->FirstChildElement();# y6 C. X# v$ m
TiXmlElement *AgeElement = NameElement->NextSiblingElement();
/ Z7 X0 J1 w @$ B! x" h! ]) ` TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();% M$ c& [% q% @6 q
//输出第一个Person的name内容,即周星星;age内容,即;ID属性,即。- n4 n( s3 p5 K4 z& f3 P
cout << NameElement->FirstChild()->Value() << endl;
* K% J+ G, @8 h1 N' Z cout << AgeElement->FirstChild()->Value() << endl;
+ b8 P( w4 Y# c9 _ cout << IDAttribute->Value()<< endl;
/ K3 i, b/ [! d: M% ] }
1 {$ T- O" O0 V H( G1 J8 r catch (string& e)$ G) u- Q4 l `: R: f: R
{4 s7 K3 x+ B" b- X
return false;
! _8 j9 {+ s. M! X4 X }
( c% n' d6 n4 b% r5 Q$ q# W+ y return true;
5 D( E. z1 n, V}$ c1 n2 b. F7 ?8 A# @
int main()
1 s! ?2 I! J6 _! x$ d{
" B" T$ a! e5 t! p( G0 Z# ^1 ~6 a* j string fileName = "info.xml";
) h' y% Y2 k. i% ~ CreateXmlFile(fileName);, h% P, l+ x5 m3 T# c
ReadXmlFile(fileName);
) h6 X) c' z, n r; O' i}
; J/ I0 |# B, o4 X% T6 e% V- k
U# n# |3 m) J8 A- k! C/ V- P# o- A( T! ~- h& s/ f
|