请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好。 TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。 DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。 如下是一个XML片段:, U' K) \. l: K, U$ L. d- L0 }
<Persons>5 ^) |" W) e8 t9 k# r
<Person ID="1">
& b- |9 y( ]4 Q2 N. e9 z7 i3 I <name>周星星</name> M9 G, y2 G% k0 ?4 T, d
<age>20</age>
9 V/ ~: O# p3 @ F2 f1 G </Person>8 {( F1 v" R; U8 }, _! T; C
<Person ID="2">/ c0 F" s# Q/ I. N
<name>白晶晶</name>2 s; f3 z! W/ K
<age>18</age>
# k) \0 b) d5 s' y1 H </Person>
8 l6 Q7 P# O0 r& T' P7 n </Persons>
* o* r3 A ~$ u8 A5 P" \" O) B0 |* P7 i" O2 G w
在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文件:6 {6 j/ G( Z3 V
<Persons>
$ t6 |) l3 m$ @! u( y- h <Person ID="1">6 S7 d0 C( T; A4 _4 ?0 g# g5 v
<name>phinecos</name>' ~- B" J9 |8 M1 Q% m( |" Y
<age>22</age>
1 E3 C' d; a' a+ t( N( o+ @7 s6 w& t </Person>: l6 k4 M9 x1 [- Q
</Persons>
6 \+ O& B' e+ e0 N" ~* Z' O, }8 \6 ?$ f* @8 q( g
读写XML文件的程序代码:( _ f- Q# G1 }/ X. e; m0 ?
#include <iostream># Y) l2 p. F5 z9 @. p
#include "tinyxml.h"
# \% s$ ? j( ~* z: o& |#include "tinystr.h"
) h9 \- x2 c% {#include <string>2 p! A# v7 { q
#include <windows.h>
, c2 }1 ~4 m$ F9 r2 {2 T#include <atlstr.h>
5 _/ P2 ^7 B$ ]& m4 m1 \% Gusing namespace std;. b6 Q- m/ s& T/ I$ V/ T% z
4 G% O* D) h3 Q; w2 E# G3 ]
CString GetAppPath()
9 m$ J9 H; e* ^: P{//获取应用程序根目录
: n3 B5 X5 i" a& u( s. n TCHAR modulePath[MAX_PATH];
: R# p- e! j4 A/ ` GetModuleFileName(NULL, modulePath, MAX_PATH);; z: @5 N' ^8 s
CString strModulePath(modulePath);! U! _+ ]7 X0 w3 q) K- |& x
strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));% {) o8 e- H$ p, U3 J
return strModulePath;
, a: R; D( A0 @& e1 a, ^( v}& }1 y" U2 m" v9 O2 Z
0 g' g$ d% L# T3 [" ^9 i. vbool CreateXmlFile(string& szFileName)
$ Z/ ^- `7 x l9 `% n3 ~{//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false5 e7 X1 e% g; ]; C8 x. D
try0 L) y& {, D" a+ E
{
. b" q; ]& y4 ?* W4 v: X4 t4 K //创建一个XML的文档对象。
( V5 _/ r( A; e# L% N/ s* l$ } TiXmlDocument *myDocument = new TiXmlDocument();4 r# z) C8 Z! _7 y% \
//创建一个根元素并连接。
2 n/ E+ R |: O* c( `- ~: {0 a TiXmlElement *RootElement = new TiXmlElement("Persons");
( R5 `$ l; E' u/ A3 R/ i9 ? myDocument->LinkEndChild(RootElement);1 R, I) ~& v1 n. w- Y( b! ?
//创建一个Person元素并连接。
* B8 o+ n5 i) B) l7 k5 o TiXmlElement *PersonElement = new TiXmlElement("Person");
; A1 j2 @5 F0 Y2 J% I. H- q. j RootElement->LinkEndChild(PersonElement);2 P% _- M2 V% l) R) ?6 H
//设置Person元素的属性。 ]. w- j$ Y% }4 }/ X+ _
PersonElement->SetAttribute("ID", "1");0 u+ k# M6 a5 @4 Y
//创建name元素、age元素并连接。
6 n/ V( O% |) \+ k' p TiXmlElement *NameElement = new TiXmlElement("name");
8 ~' u+ l+ b3 v- s2 k2 E' Z0 ?+ F TiXmlElement *AgeElement = new TiXmlElement("age");( f7 ~+ J/ \) V2 [+ {
PersonElement->LinkEndChild(NameElement);
0 ^6 K& Q6 T9 V% O PersonElement->LinkEndChild(AgeElement);
. O Y( i* W0 u: Z4 i) Q& C //设置name元素和age元素的内容并连接。
+ e+ t& E# D! l h! {7 _ TiXmlText *NameContent = new TiXmlText("周星星");
9 w( P& N: `/ h TiXmlText *AgeContent = new TiXmlText("22");
) b3 E* C5 O8 p6 I- _ NameElement->LinkEndChild(NameContent);/ P Z m, j. b0 I
AgeElement->LinkEndChild(AgeContent);7 g3 a; q8 A# M( t
CString appPath = GetAppPath();( L8 O: }% _5 a& x
string seperator = "\\";- {; x- z# e: h- v5 d, a! q$ M
string fullPath = appPath.GetBuffer(0) +seperator+szFileName;' K- j2 o3 Y H% j
myDocument->SaveFile(fullPath.c_str());//保存到文件
7 l( I4 z3 W2 C9 \' e0 C8 { }% ~; k5 b n& y# O
catch (string& e)3 C. V! w. L$ ]0 ~! M9 n/ x+ z
{2 b1 P8 p$ m5 \: F* }; |
return false;5 k9 F0 a V0 X3 {% r }
}
8 Q1 K* [6 Z* M/ V return true;
0 B N9 A/ [2 k- x4 a+ |2 t- J# p% o( e}
# o8 {" H4 E, K8 |; F" Q
$ y: d- v+ B) D9 }" jbool ReadXmlFile(string& szFileName)
6 m3 ^6 b: S% A( q{//读取Xml文件,并遍历% _) N9 F) Z" S( [
try
# _- @" O! l# }7 r0 P% s! i {" z) B1 h" @4 @# b: r( a. r
CString appPath = GetAppPath();: z" o* T( r* H( l2 P& R
string seperator = "\\";
2 I2 R; ^# W7 F) N6 k string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
" m0 d; T9 f7 |4 O7 | //创建一个XML的文档对象。8 D2 x# W+ P* Y8 T
TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());
: Q4 I# P+ C. j6 f2 ] j3 D/ S myDocument->LoadFile();3 ]& Q$ l- E& x7 Q0 D# s3 b
//获得根元素,即Persons。8 c" q- D+ y6 [# V# E; C) c
TiXmlElement *RootElement = myDocument->RootElement();4 ^, k& ^) D- @
//输出根元素名称,即输出Persons。
- j' n% Y6 v* s6 M, r cout << RootElement->Value() << endl;( A/ b, I5 D) M" b$ e
//获得第一个Person节点。
: P7 |) _2 z( Q; w TiXmlElement *FirstPerson = RootElement->FirstChildElement();
$ ^4 N- Y# X: p/ \' h: J: { //获得第一个Person的name节点和age节点和ID属性。3 n( X! |2 d$ H2 `9 c: M; f
TiXmlElement *NameElement = FirstPerson->FirstChildElement();, {" r m: f+ }0 ]- g8 D) z
TiXmlElement *AgeElement = NameElement->NextSiblingElement();6 `0 t3 B+ t2 h1 h; ?
TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();
& }% C+ z) r1 D9 x% H& h' V7 j //输出第一个Person的name内容,即周星星;age内容,即;ID属性,即。0 e2 @( @8 q- v1 X
cout << NameElement->FirstChild()->Value() << endl;
5 Q7 G- e6 p, [! B Z+ d cout << AgeElement->FirstChild()->Value() << endl;7 q9 ?. R+ A, g2 g. V
cout << IDAttribute->Value()<< endl;
# y/ G8 t' U* \$ Z! S }
5 W$ Y" P! y0 M6 Z8 o catch (string& e)
( g1 x0 b4 g( \" d9 v {
- ]: G U( C$ R1 m8 t: j, n- O' } return false;
q7 e, E) C) M }* y6 N7 f( U, ]: i2 p; s
return true;: e3 R% e7 h# o5 Z2 s' n( s
}) d$ M* @- p9 S7 d' v
int main()+ h4 r2 w, }% j' \4 \2 R$ h! R3 w8 F
{4 z4 E" G- Z5 B4 ]% K7 l
string fileName = "info.xml";7 j. ~; c4 ^1 o- a3 v, h. p
CreateXmlFile(fileName); \* x7 H4 t7 x i/ x9 `7 F
ReadXmlFile(fileName);
6 k& ^7 O% Z4 Q; W5 a, G}
; L, K% j( m) ?9 x3 o- o' v: \4 ?$ C
( ~3 m. n8 V9 g" E9 R* @- z* H, x! a7 J/ g0 J
|