请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好。 TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。 DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。 如下是一个XML片段:
3 A: h: A8 d/ B <Persons>
4 N' K! \# g7 L8 \9 H <Person ID="1">$ I3 [4 B! `+ E8 A
<name>周星星</name>7 @' B' _( Z, [' Y
<age>20</age>
) g8 `9 z& {5 `+ G </Person>5 M: A: H+ T5 m! m9 A5 d7 F+ a
<Person ID="2">
2 r+ a, r) t. m ]0 A <name>白晶晶</name>
: x1 e/ r! g g0 c5 [ <age>18</age>
" T7 k: s- E3 X! N! k3 \ w7 z( I </Person>, U& O; o! d8 g, a
</Persons>4 X# x5 o. r7 C; `+ c
+ o2 R- {* j, Q/ N- i. { j' t
在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文件:$ o: |! t4 K2 Q3 [* n1 q$ W
<Persons>
( D4 u& ]! W7 I8 E; D5 f <Person ID="1"> l: I O I6 U
<name>phinecos</name>' p+ i7 ]. _& r) n; s# S
<age>22</age>
; q- l( M( o0 E- d! ?: k. W </Person>! s/ ^. X+ F! b i# y1 \
</Persons>/ C4 c/ ] c) a" _3 x. O
$ R' B- i! j$ g( B' z* {' A
读写XML文件的程序代码:- n; ?5 d( A1 H) r+ @
#include <iostream>
+ |/ f, p/ w. T# \ z. z#include "tinyxml.h"' o" `- d0 e9 J o9 ~. O8 \
#include "tinystr.h"+ }! P8 ?; s0 \% d
#include <string>! c+ L7 m0 O" w9 J# t/ K0 C3 M
#include <windows.h>. t3 P y9 J( m1 P5 T- `7 Y
#include <atlstr.h>
' ^1 L2 @, N; F4 m3 i) xusing namespace std;
4 y& ?& M" O9 y
9 d9 P% @( i$ U4 M" sCString GetAppPath(): h5 B x5 b- v- }4 y
{//获取应用程序根目录
# _! E2 }# n u- ^+ Q TCHAR modulePath[MAX_PATH];
8 m$ m1 c* [$ R' z GetModuleFileName(NULL, modulePath, MAX_PATH);
9 j9 |1 @: R7 e8 F CString strModulePath(modulePath);
7 o; t3 ^6 g. F6 i" p/ y- L" ~ strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));, V0 v, s& s. y* p9 Z, f
return strModulePath;; w4 Y* ~& O9 G: z" T
}
" G* Q9 w) s# R y+ C* P' Z$ l; [( _
bool CreateXmlFile(string& szFileName). B# T6 H* \8 Z T2 H
{//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false3 H; R. m9 F+ K! A" q3 v% _; ]1 m
try
6 E, D, G- w7 U9 m {
0 }' n7 x# o8 A7 Y/ ?( w: j //创建一个XML的文档对象。
3 ?% R8 q0 g+ F, ^. ^ TiXmlDocument *myDocument = new TiXmlDocument();( @9 R! r7 q( _/ G R
//创建一个根元素并连接。
# k$ i/ g; U# I. q TiXmlElement *RootElement = new TiXmlElement("Persons");4 c0 J: N/ D- ?0 B$ { c
myDocument->LinkEndChild(RootElement);
0 T4 Y, H; m2 D) s0 q //创建一个Person元素并连接。4 L) o8 k. o$ S$ l
TiXmlElement *PersonElement = new TiXmlElement("Person");
" n+ M: @! l7 l G O RootElement->LinkEndChild(PersonElement);/ L) m* X% e; x; }
//设置Person元素的属性。6 z# ^6 r7 p. Q6 q8 ?. B+ i+ M
PersonElement->SetAttribute("ID", "1");
6 T* }- L( X4 d7 e* w% w n //创建name元素、age元素并连接。, l$ _* K: @7 X( f: G! R# [
TiXmlElement *NameElement = new TiXmlElement("name");- M0 H% H7 s) i# [
TiXmlElement *AgeElement = new TiXmlElement("age"); r f- [6 M/ c7 J
PersonElement->LinkEndChild(NameElement);, F2 ~3 M& a5 N" C) G0 ~- m C8 q
PersonElement->LinkEndChild(AgeElement);8 _$ }! `9 B0 z' e4 Q6 {5 I$ G, k, c% S
//设置name元素和age元素的内容并连接。
% }1 W1 D. I! I( I% @ TiXmlText *NameContent = new TiXmlText("周星星");
6 \9 V* C, N0 T3 d+ _/ y TiXmlText *AgeContent = new TiXmlText("22");
2 N, c" ] |, A @) b2 g+ |: D NameElement->LinkEndChild(NameContent);
; X4 V$ B' v4 k+ L+ t AgeElement->LinkEndChild(AgeContent);
2 o2 i- }0 a1 X$ o CString appPath = GetAppPath();) b7 m g) e5 q7 j0 F
string seperator = "\\";
X: t, T: x4 A' |% J" h6 j string fullPath = appPath.GetBuffer(0) +seperator+szFileName;; U9 \: E4 n1 g5 S% a
myDocument->SaveFile(fullPath.c_str());//保存到文件5 K' |5 [2 |: k/ K. f2 q
}
- c6 f! v& H: [1 r catch (string& e)+ X, e! o. y2 J
{4 c5 k! e/ |. ~8 {2 Y% e+ x+ ?& n; y
return false;% b# z4 D r& k& M1 [* \* z! Y0 |
}; I7 j! D* A: C( M
return true;: [# X% _( X! x
}; M3 \3 P4 l9 t, ^
7 ?; ^! M3 \% E3 {
bool ReadXmlFile(string& szFileName)0 Z0 s. K9 p1 j/ f, b
{//读取Xml文件,并遍历2 Q' L# [2 V5 W2 s
try5 D% b) _% H0 w/ ~
{3 R) F3 g+ n- m! x) U* R
CString appPath = GetAppPath();
- D( L7 [) B- N( k string seperator = "\\";, Q/ Q* J- N% O& p% p& |
string fullPath = appPath.GetBuffer(0) +seperator+szFileName;9 i: p+ e7 c* f2 y8 Q7 K/ u& j
//创建一个XML的文档对象。
% F( ^& `! e" @- H! J8 \ TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());1 V4 |8 i5 O! Y9 O6 f+ o
myDocument->LoadFile();1 a ~8 j6 m# c$ m; O0 D
//获得根元素,即Persons。
( V, r2 g3 _" M2 L9 v0 T: b% y6 @& { TiXmlElement *RootElement = myDocument->RootElement();
2 l* p4 a( p! ^& C1 N //输出根元素名称,即输出Persons。 y* ]0 W/ Z) e
cout << RootElement->Value() << endl;
( h9 j" r. v+ s" C2 C2 t/ ~# K; N% s' C" | //获得第一个Person节点。3 ^& e6 E, R! C2 a
TiXmlElement *FirstPerson = RootElement->FirstChildElement();
K6 Z: g* ? x: P //获得第一个Person的name节点和age节点和ID属性。
+ }2 H$ {0 x, s! p TiXmlElement *NameElement = FirstPerson->FirstChildElement();
; C9 H3 J, {8 r; P/ R$ | TiXmlElement *AgeElement = NameElement->NextSiblingElement();
0 m1 [" o, c' l7 u0 z) s: J( J, f TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();$ {: y, m1 O$ G6 `
//输出第一个Person的name内容,即周星星;age内容,即;ID属性,即。' ~" Z* O0 O& f e( V0 |
cout << NameElement->FirstChild()->Value() << endl;3 G' ?1 b8 i7 V) p) G9 N
cout << AgeElement->FirstChild()->Value() << endl;% x8 ?& @# ]1 X" I5 J' Z4 _
cout << IDAttribute->Value()<< endl;
# ~2 ]! R- n. M4 g( b }4 z% d, M$ `$ h5 A
catch (string& e)
c% H* \# @: ^, h7 ?% Y% j {- B. Q6 _# z8 ?9 ?, D/ \
return false;
8 @( G |1 U! z" R' p }
$ m- O. i& r2 W$ p: n( a# m; s K: l2 H return true;1 H0 t' R! G; t
}
" q' c) p# K8 T3 R2 K$ ?int main()2 a. B R* o% L6 g B9 _
{
( Q1 ?1 g2 o# g% T, e string fileName = "info.xml";. h$ {5 ]. r3 @2 Q3 u
CreateXmlFile(fileName);8 h# g6 V# C. M( R
ReadXmlFile(fileName);9 }' K, G; _7 T) D1 ]
}
! T+ Q' Z0 V" G4 a! ^7 g
8 N& [/ w9 } J$ } n0 W
8 n' a& V. G8 F5 t |