请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好。 TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。 DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。 如下是一个XML片段:, W) h. |/ x: g2 r
<Persons>' }; @' X0 s( {7 r
<Person ID="1">
) v1 m- E2 P2 l4 s <name>周星星</name>: ]3 u9 t4 z" p( X7 j
<age>20</age>/ V+ L: h/ F/ |
</Person>! e0 s c! j8 _5 {9 F
<Person ID="2">% r5 t& l- ]) K _0 k, t! j0 Z
<name>白晶晶</name>
9 }1 j" W5 I1 \1 N <age>18</age>0 c) I! g+ ? m
</Person>
0 u0 p) t) ?6 o0 Q' t1 d6 y </Persons>+ G& n2 r1 o4 |7 k- T( G. J
. |! l% C, ~- p, 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文件:
. g* E; C/ b. L0 j3 B( P# w- f<Persons>
2 q- y8 n9 ~) Z+ \6 I! `: ^ <Person ID="1">
$ a3 [" ]" W5 y# Y7 X6 `9 g3 k <name>phinecos</name>5 g- Y) O' f/ G# J7 L! F
<age>22</age>
& \6 S- i) ?% I </Person>8 l2 ?) i5 |- W
</Persons>
8 o, I6 i9 X2 P" f7 A, M6 [9 t* O2 S8 m, ^. E0 N8 j9 K& s
读写XML文件的程序代码:3 U# ]$ e( x: a' `4 v4 N
#include <iostream>+ Q7 ?: p$ G3 [9 v! N+ i3 C2 f) q
#include "tinyxml.h"1 A6 X3 o$ \' Z X8 \
#include "tinystr.h"
2 i7 S$ z) \5 t: a, ?: ~8 G#include <string>
' U% a% Z" F6 N% `#include <windows.h>
' n3 N9 C- a1 n1 j6 R9 N& r! R#include <atlstr.h>: \% o( s G( |" f0 h
using namespace std;
0 w2 l; ~& C( _" z6 S9 R/ `
# B/ `& l/ a# C0 ^7 D% M/ H7 dCString GetAppPath()
' E3 g! ` d! ^1 y{//获取应用程序根目录
9 T5 y; c4 w& F" s7 l( O TCHAR modulePath[MAX_PATH];
9 S' i( A0 a; y! l5 u1 u GetModuleFileName(NULL, modulePath, MAX_PATH);) k& x6 i0 N1 n" g+ \7 }% j* j& O
CString strModulePath(modulePath);( Q, d- s. D- q
strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));
7 Y2 b h" i& i4 }0 ?* x: X return strModulePath;$ w7 {- I9 g& m/ o2 r7 _( Z7 B
}
$ H* C" a4 L- Y; U/ x( W2 M' T1 r" O! z c
bool CreateXmlFile(string& szFileName)
: o( ?9 j% N q* \; V# p# _{//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false, u( d( a- x/ a7 ~+ u n. k
try
7 V- ?% {# D& L$ I {* v; {; Q: D7 {+ z
//创建一个XML的文档对象。
: _, w7 f' B" Z. x TiXmlDocument *myDocument = new TiXmlDocument();! J( b7 u/ L' Z- c. O1 X
//创建一个根元素并连接。. d I7 x( `- N! x3 {* Q/ C
TiXmlElement *RootElement = new TiXmlElement("Persons");
6 o8 k2 q0 L$ N o myDocument->LinkEndChild(RootElement);
4 q6 C+ m) g7 h8 p* G7 K! T$ Q2 B //创建一个Person元素并连接。
8 C6 b# H" X; b* n TiXmlElement *PersonElement = new TiXmlElement("Person");
' `8 B8 T* F2 p- q, ~6 Y R! J RootElement->LinkEndChild(PersonElement);" T2 |, V) U" g1 s$ X2 [
//设置Person元素的属性。! v X# J h, U' M+ c: T0 v
PersonElement->SetAttribute("ID", "1");9 H' {& G3 {( k4 K1 C- f! I. @7 D
//创建name元素、age元素并连接。
. G7 n% }& [. Z3 S$ Q# ` TiXmlElement *NameElement = new TiXmlElement("name");5 u' s. x% u; F$ [, ]) Q! l
TiXmlElement *AgeElement = new TiXmlElement("age");2 U$ G6 v/ M& `5 u
PersonElement->LinkEndChild(NameElement);9 b: w3 E2 v8 f# s5 p
PersonElement->LinkEndChild(AgeElement);
' c+ } l& u/ q! u' l8 s. v, G //设置name元素和age元素的内容并连接。$ @0 J/ x# D O, ~( F0 M; u5 X
TiXmlText *NameContent = new TiXmlText("周星星");
& K* A6 h" x+ I: H3 j TiXmlText *AgeContent = new TiXmlText("22");
1 ?/ o5 [+ N" W# i, s" | NameElement->LinkEndChild(NameContent);7 u$ s( `6 J# Q, \" L8 B
AgeElement->LinkEndChild(AgeContent);
/ X8 v% N* M# u CString appPath = GetAppPath();8 E5 a( ]" P; G8 u- w: I7 O
string seperator = "\\";: U p0 q$ b2 I* Q' r4 C+ `
string fullPath = appPath.GetBuffer(0) +seperator+szFileName;$ L2 W2 W$ R: {1 q, S
myDocument->SaveFile(fullPath.c_str());//保存到文件2 @* \( b' z9 ?4 f
}
+ e, c1 _/ E/ ]1 W- r catch (string& e)3 A+ ^8 F0 M8 Y d" X) ~- e4 i
{
' s7 s- {5 J2 }5 v/ _) k4 B- t, o return false;" a2 r6 B5 W# p8 ]" M: L
}
1 ?8 [! w- E; \ return true;
$ L$ b! s8 s" S! d}2 |6 U0 z9 c& \) C6 R- K+ s+ i
4 o+ O3 Y1 s4 n. dbool ReadXmlFile(string& szFileName); m) }( ] s& I6 |8 M7 n
{//读取Xml文件,并遍历
6 F2 n: u1 k8 Q( D try: w7 S1 Q' B- y( L, y9 P# ^& E
{: x4 h. E R6 [" h) T/ e
CString appPath = GetAppPath();. V4 f, X! s$ D) O6 N" t
string seperator = "\\";
5 e2 k; @1 p4 R: R- K* y string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
) z8 R5 ]9 h& _: u( e //创建一个XML的文档对象。
6 l m9 K; M4 m TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());& r- d; t) r. m3 c
myDocument->LoadFile();8 d5 P* e( q7 j7 Y! U. V
//获得根元素,即Persons。% T4 c) a! \! Q% }
TiXmlElement *RootElement = myDocument->RootElement();* |, s5 H. h" ]. g4 M6 ]
//输出根元素名称,即输出Persons。
: |4 t1 H% _1 H d6 @ cout << RootElement->Value() << endl;
* _6 Z0 K6 X) k2 l //获得第一个Person节点。% B7 v$ \- j7 ]3 H5 Y
TiXmlElement *FirstPerson = RootElement->FirstChildElement();$ }/ |- q* A# p' ?7 u' S; Y
//获得第一个Person的name节点和age节点和ID属性。
* }* @5 z" x( F9 F( i. N TiXmlElement *NameElement = FirstPerson->FirstChildElement();
; Q. N5 S7 i; D: d TiXmlElement *AgeElement = NameElement->NextSiblingElement();
& P7 A2 F. f' B TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();
6 n2 x5 P4 ^! D0 Y) `( n$ @& ` //输出第一个Person的name内容,即周星星;age内容,即;ID属性,即。, s2 [+ r2 I8 J2 h" v) m( B& a( {
cout << NameElement->FirstChild()->Value() << endl;$ o3 T) S/ i E% _! k+ g
cout << AgeElement->FirstChild()->Value() << endl; S9 K2 _: y8 z
cout << IDAttribute->Value()<< endl;1 q/ I3 l. M4 B) [3 c L. }
}
: L. k) I" Z R( D catch (string& e). O+ F0 I5 |. p! o8 n$ C; |
{- f7 x/ Q( U4 M) X0 U6 U
return false;, j# z. f; z* x/ ?; W9 u8 X
}. w4 e% M6 } C! N6 f
return true;9 }% Q! }" Q2 U( T
}
# \) b; t* d* L ?/ Hint main()
9 G* \/ X! A; q1 r{
7 U/ Y3 z0 Y" S1 d; e. P* [3 I- d string fileName = "info.xml";9 b( p, d& P k( \0 H' v3 K. h4 J
CreateXmlFile(fileName);
' J; O: @' q2 G+ ~# s0 A4 M ReadXmlFile(fileName);: g+ c$ o$ @+ R3 f. ^- p6 T( ^
}: n% @4 w+ S# g0 q. z6 {4 D
3 S6 i1 ]7 j6 d2 J. n7 m( N% b( M% k D% |4 Q
|