请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好。 TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。 DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。 如下是一个XML片段:
) V( P' n1 ?) w5 \9 @ <Persons>* |8 d" d, z( |8 z N
<Person ID="1">
$ M1 Q1 L) [& ^2 b# T <name>周星星</name>8 a( N" D( T- v3 N4 [) A+ n) b
<age>20</age>) t1 m; N* N% \" R$ E& U% [! ]. k
</Person>
2 K5 O% s V& ~5 F D$ p6 ` <Person ID="2">
+ j: x1 G$ e X$ u) H% n# Z0 q <name>白晶晶</name>3 ]; N9 B6 t% n5 Z. L/ r! w
<age>18</age>, k6 x6 w3 L' V# L
</Person>
) O" a; I! l, ` f5 D+ Z0 { </Persons>$ ^ b" L. J6 ]4 A" W- L9 w! P$ U p
9 ^5 B1 I- o0 B! G- c% B* Q7 B, K 在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文件:
* b- t. n8 }; ]% Q7 S+ }<Persons>: U: n# ?! L$ P* C3 d4 z) H
<Person ID="1">/ p+ \6 v0 S6 x
<name>phinecos</name>
( X% f0 r3 q3 b$ b! H <age>22</age>* V0 l, p* s% ]: C4 c1 b
</Person>' F- \' Y/ V8 B6 y
</Persons>
2 A- I/ |' W/ q
R7 h; h4 o2 ]读写XML文件的程序代码:
5 m5 ~: L* W, Y1 n/ F, ? #include <iostream>& o7 @1 S0 \$ v. ?
#include "tinyxml.h"
5 m, |; T. v- M#include "tinystr.h"
) \) T+ ^2 Y! G! B: W1 O" K#include <string>- a+ t" b5 r! G3 n: t+ m
#include <windows.h>
0 W1 `! P' M8 i. g7 X. \4 H#include <atlstr.h>
/ c _7 L% ? s8 \8 `using namespace std;. ~2 ^1 u& p2 P" m
6 O/ l. C" ^8 E; p+ |' bCString GetAppPath()
' X( J d4 E- [" X{//获取应用程序根目录
7 h5 e0 j" {$ U9 U4 }2 H% a TCHAR modulePath[MAX_PATH];( k8 c/ A& p3 n
GetModuleFileName(NULL, modulePath, MAX_PATH);
9 b6 {- N" l! b! G8 f CString strModulePath(modulePath);
5 S$ [9 _2 I' a strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));( k& ^ b) u% P4 }0 n& }
return strModulePath;
1 o8 \! b) r* z& c}( ]( i5 q. [1 l$ l: d0 y
" f; J0 B* j& ^9 \; r7 h' B
bool CreateXmlFile(string& szFileName)
/ ?9 D/ W' I5 D. T7 V{//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false: k, m+ e. K5 O, }
try0 _$ L8 x! ^$ J( Q+ a
{
5 c, O0 _4 W( P% M //创建一个XML的文档对象。
$ a/ e- ^5 d0 V+ w' O& \ TiXmlDocument *myDocument = new TiXmlDocument();
0 w: d) I) Q, s" b% }' b //创建一个根元素并连接。0 J( j7 m4 P* w5 F! p. g
TiXmlElement *RootElement = new TiXmlElement("Persons");
+ e# I7 u2 u4 F) z$ o. ` myDocument->LinkEndChild(RootElement);4 Q$ j* @: Q. E* p( r
//创建一个Person元素并连接。8 ?! N" I* G7 u
TiXmlElement *PersonElement = new TiXmlElement("Person");
% M8 r. z4 t' U8 ~: \ RootElement->LinkEndChild(PersonElement);
v/ p: U$ ?7 t" t8 N //设置Person元素的属性。
% S/ J. T* R. e! j) V$ I( x6 P PersonElement->SetAttribute("ID", "1");& v% N! K1 f/ r0 J ~$ |2 J1 n4 J
//创建name元素、age元素并连接。
# K- d9 \( ^4 o/ s, f% [ TiXmlElement *NameElement = new TiXmlElement("name");
6 E }5 w. E5 R# j: R5 [, S TiXmlElement *AgeElement = new TiXmlElement("age");* W' a% _) U. p
PersonElement->LinkEndChild(NameElement);$ c2 B7 e! P `1 X
PersonElement->LinkEndChild(AgeElement);
" x# B; r+ q3 r. L //设置name元素和age元素的内容并连接。
7 ? B, M a5 o+ U TiXmlText *NameContent = new TiXmlText("周星星");
0 Q8 l3 {& E8 h TiXmlText *AgeContent = new TiXmlText("22");7 O- o2 ~+ R: y. Z
NameElement->LinkEndChild(NameContent);
% [: L5 n8 e6 c- `4 G# R1 { AgeElement->LinkEndChild(AgeContent);
9 A9 j& ?5 }% z$ c9 U CString appPath = GetAppPath();
- O+ n+ B8 s6 S5 d5 n8 \7 m6 w& D string seperator = "\\";& P, A+ T) Y! R3 p! t6 E8 k4 Z
string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
6 s; n% I) w; \; s% B myDocument->SaveFile(fullPath.c_str());//保存到文件& G V1 L7 W! [' q8 V6 n+ n
}
( A' J) Z$ L& @* C0 H* _ catch (string& e)
6 k' k; \% ]$ u @ {
5 f- q+ C2 `$ Q' F& k. B# E return false;* G1 c+ R2 r. C5 I
}
4 r: z- o @! w9 S; o# k return true;, ^1 k( z) W$ K8 p8 M
}0 y3 M5 X& d( l: c: k7 b" v" w
1 e/ ^' u; b4 a' p2 Mbool ReadXmlFile(string& szFileName)
3 O% }$ A f% y+ M! h) d0 D0 U{//读取Xml文件,并遍历2 C" w3 }3 J7 l0 E8 U
try ?2 J1 t A) R$ `- @, q E* [
{% k# |2 l" n. M% d/ {
CString appPath = GetAppPath();1 Q- V& s R. m; u9 w
string seperator = "\\";2 v) v! y1 T& C$ \9 B! Q5 U; L" q
string fullPath = appPath.GetBuffer(0) +seperator+szFileName;4 }$ ^/ h# @( s: h
//创建一个XML的文档对象。 V2 m9 j' T, s" n C# _
TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());/ ^7 P' ?" ?1 h, u! c
myDocument->LoadFile();: C( T4 y9 D& Y2 S
//获得根元素,即Persons。
6 K! g1 s8 @; U" K, l" T& s2 C5 k TiXmlElement *RootElement = myDocument->RootElement();0 F) \7 ]% {$ D! h Y
//输出根元素名称,即输出Persons。
" m' {3 @" i. n1 Q' _ ~ cout << RootElement->Value() << endl;
' o# h2 G0 l3 N; L/ G x //获得第一个Person节点。3 L* p R k- N9 f- n
TiXmlElement *FirstPerson = RootElement->FirstChildElement();
! B* ^5 T4 v6 I' ] ~2 P //获得第一个Person的name节点和age节点和ID属性。9 [/ J5 c+ I5 D2 O3 H& h( O: \
TiXmlElement *NameElement = FirstPerson->FirstChildElement();4 t, A. c d% O1 [! l: h' ?
TiXmlElement *AgeElement = NameElement->NextSiblingElement();! r9 B, F9 _/ V7 {1 L# \
TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();, S/ _/ j% X; |0 V! r7 K
//输出第一个Person的name内容,即周星星;age内容,即;ID属性,即。5 k, i2 a6 Y9 _7 w9 c) f d
cout << NameElement->FirstChild()->Value() << endl;% P7 q; w% b5 u
cout << AgeElement->FirstChild()->Value() << endl;
" J# h$ K5 U, t* G/ p. g8 d# f% z cout << IDAttribute->Value()<< endl;7 y" z1 Z* N1 M8 h1 q0 G
}
0 y4 \) o/ P+ u0 P: @8 a catch (string& e)
* ^) }$ N" Z. [% X {
( g0 f: D& `' p. m return false;
0 {" h8 H+ k3 ^, v# v }- [/ E, g3 M) v" y$ P) a+ Q2 X
return true;5 o% j* U- ^. R2 A* e* W7 s- w
}8 Y! e9 O5 O% D7 M6 ~
int main(); O( ?- v$ R1 V$ L7 v6 T8 f- ~
{ E) B" K, p2 Q' J
string fileName = "info.xml";- ?( V% \* a# z
CreateXmlFile(fileName);/ Y" w0 x6 L3 S
ReadXmlFile(fileName);
1 \2 B1 n$ N: `. T1 Z# p9 s' k}( i) T6 @1 a4 c9 `8 G4 K7 f
; ~$ s+ `; i9 I: F7 ? |# h6 |8 [& _3 L, G; U
|