请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好。 TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。 DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。 如下是一个XML片段:' @% {" [0 R# Q" X8 ~
<Persons>
* i2 v( `; V1 k$ H8 s <Person ID="1">
( Z. F: t2 A% W! B <name>周星星</name>
+ X1 ~! W9 R/ ]' y t* j <age>20</age>% z! `- [. u- r
</Person>
6 X; [* g3 g' b7 o0 p <Person ID="2">; b, C* o5 q1 e/ K" i8 ?) ~( q: B& p
<name>白晶晶</name>; o& g ?0 [$ M; d4 E1 H" k% c3 x) l
<age>18</age>7 `+ @4 M0 B# S2 G; c
</Person>/ u+ `; S$ d: x4 w. D
</Persons>* n# Y( D" P" v) Y+ D$ c y
# k, K8 I% ]5 a8 N$ _) j y1 x 在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文件:+ {' ^/ \5 `3 `0 v7 k' {
<Persons>/ T3 u: J3 `$ y! y1 n$ V
<Person ID="1">
3 i) v7 O3 i+ V+ r$ j <name>phinecos</name>/ s2 c/ n8 L8 ?. v! @# T* H
<age>22</age>; f& v4 o( X4 s8 D
</Person>
. S+ p/ B+ L4 M' @2 s6 g</Persons>, f. d7 ^9 j7 @6 ~6 t* o
) [/ T8 Z8 ^; F5 j
读写XML文件的程序代码:7 o: D d7 h! |/ F; E" O
#include <iostream>
1 e1 K" S$ ]( S' Z" ]7 t- N$ U#include "tinyxml.h"
) |) {* {& V) r+ }. Q#include "tinystr.h"# m, K" P' |" c% y# ?
#include <string>
/ [: R, Y- v. D7 [$ j#include <windows.h># \8 P, W* p! h% j- n! ~) Z
#include <atlstr.h>
$ {9 @* z- W l2 v9 @+ \) ousing namespace std;9 n& U3 ^0 @3 z1 Q3 l# ~& p
" Y& l; L- o! l' T6 v7 |% h9 H7 qCString GetAppPath()
C# d% C6 X; ?) c4 E{//获取应用程序根目录
( N% k( f0 k% m& U; u TCHAR modulePath[MAX_PATH];
9 f/ P+ ~0 @: `/ m# A7 E GetModuleFileName(NULL, modulePath, MAX_PATH);0 [$ q( ~7 t& U" H% [
CString strModulePath(modulePath);
. D$ q8 |) T- q" x- W+ T$ \ strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));
: j9 d5 W5 C7 |7 @! J return strModulePath;! z$ J- u& \. G# [& [/ |; L
: F! l$ n& t% {: a: N6 E o, a" ?) g1 m: M 登录/注册后可看大图 }
8 V2 b ?8 D5 S+ U4 C% I5 Z( E0 F. y9 h2 p/ R6 l) L
bool CreateXmlFile(string& szFileName)
& ?5 ^1 o- f: ^2 Y) v% ?; O* i' f{//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false) S9 H+ y0 Z2 P7 D, P
try
5 I8 O; F0 l! B3 r! S8 z- ^- I# { {
7 \$ h) G! ^- P8 A# i7 L l //创建一个XML的文档对象。
; F7 _6 M6 v8 h5 e; C. F* s TiXmlDocument *myDocument = new TiXmlDocument();
) _5 e4 D* e/ r4 k4 t //创建一个根元素并连接。
' d9 E) h* ~& u8 S2 S, X TiXmlElement *RootElement = new TiXmlElement("Persons");
4 Q0 i. S l6 l; Z myDocument->LinkEndChild(RootElement);6 _# b6 R% \4 `, j' b/ M( v' A
//创建一个Person元素并连接。9 S3 t0 ~% h/ S5 ~: k. O
TiXmlElement *PersonElement = new TiXmlElement("Person");" I& @$ U% |, T& ?1 a, _
RootElement->LinkEndChild(PersonElement);
+ \$ f; D3 c% D+ O //设置Person元素的属性。" W4 R+ q" d# N8 G& ?" G3 o" S
PersonElement->SetAttribute("ID", "1");
% ?' W; ]4 J+ K: B1 b R: W9 M+ P //创建name元素、age元素并连接。
6 F% u6 a! K$ V, G2 ]& a4 f3 L4 ] TiXmlElement *NameElement = new TiXmlElement("name");4 R! O5 _. e, q
TiXmlElement *AgeElement = new TiXmlElement("age");! K! K3 f P: `6 ~8 J
PersonElement->LinkEndChild(NameElement);
/ y1 b4 ^6 g/ J5 M- b$ T8 c PersonElement->LinkEndChild(AgeElement);
; K7 l. A, T0 l( r% G //设置name元素和age元素的内容并连接。
1 C* X# W! C3 H) b4 H TiXmlText *NameContent = new TiXmlText("周星星");
- ]4 `$ U3 c- L& k) H TiXmlText *AgeContent = new TiXmlText("22");+ l$ O% X9 N }1 L: i
NameElement->LinkEndChild(NameContent);6 F& v- l7 x+ U/ f+ s( ~
AgeElement->LinkEndChild(AgeContent);$ G$ C2 o1 i, _# q% N' L2 T% l
CString appPath = GetAppPath();& S/ ^1 h' {# G5 x" C" X
string seperator = "\\";8 }" e* n: W' a5 v. m6 T
string fullPath = appPath.GetBuffer(0) +seperator+szFileName;' O# J" x; Y5 ^( v$ }
myDocument->SaveFile(fullPath.c_str());//保存到文件
( @8 r% G" I- B) l3 k7 ? }* n9 c! g( J3 l- a( e) c
catch (string& e)
; M& U3 w4 u r4 x3 r; `% N {5 \2 z+ U3 w/ v+ a
return false;
( {/ f: w7 y0 c7 `7 v5 D- _% Z } s D' `6 |, I J4 G
return true;1 _: [, q+ r4 a2 M5 q
}
( _: b: ~; \8 d9 c
( p2 O, M& t8 ?, c: ]" pbool ReadXmlFile(string& szFileName)! c& t, Y4 D; A
{//读取Xml文件,并遍历
! j0 ~# r' m) Z; X- V try
- R7 Y8 `2 N+ e' @' p( j {9 S! `3 N; G* m M
CString appPath = GetAppPath();
4 l! [* h+ i6 k R5 g string seperator = "\\";
/ a: j- o! j5 T' q) _& h! R string fullPath = appPath.GetBuffer(0) +seperator+szFileName;4 T8 [$ B- _$ ?% U% l5 c+ N* T
//创建一个XML的文档对象。
y4 l5 n8 `* k$ Z TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());( f1 |, c$ |! K
myDocument->LoadFile();
9 V+ B. `$ k/ D. ?# O //获得根元素,即Persons。
6 h" G5 s7 Y g6 i% r0 s& ?( p TiXmlElement *RootElement = myDocument->RootElement();$ I- @$ b( n3 v& G3 B2 X; g' x
//输出根元素名称,即输出Persons。
, S0 g; y5 ]( m% h* C: C cout << RootElement->Value() << endl;
6 x$ i2 a% _$ I! H //获得第一个Person节点。/ a$ A# z9 `+ t- A( |' M
TiXmlElement *FirstPerson = RootElement->FirstChildElement();
% Y0 v+ N8 N2 a) A3 G H //获得第一个Person的name节点和age节点和ID属性。
f/ M9 E! ^5 w1 [8 E1 Z TiXmlElement *NameElement = FirstPerson->FirstChildElement();& T& @9 I+ x, r M
TiXmlElement *AgeElement = NameElement->NextSiblingElement();5 V4 |$ }, s* \- z1 K
TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();% y/ R* Y; {5 A R# x" x
//输出第一个Person的name内容,即周星星;age内容,即;ID属性,即。 k n' X. S1 S5 x
cout << NameElement->FirstChild()->Value() << endl;
s5 |+ x( W, }* g9 m$ W. z, j cout << AgeElement->FirstChild()->Value() << endl;+ Y( H0 _& k. M+ K; |0 M" A4 U8 s
cout << IDAttribute->Value()<< endl;
- V. L% O- v v( k3 D2 S( A }
, ~# x7 u- s. h1 e( T catch (string& e): Q1 K# P6 \" \# A) F
{
F) v7 d) S; X: h0 K1 ^ return false;9 P O1 Z9 L' h* u! i
} R- ~- P" {4 I9 S, \; N
return true;
' g8 [6 n# W3 w5 f}' I! N) W2 B% @- e% T4 d% k( ^" I& K
int main()
' h. Z; q8 K/ I/ G! g/ D4 g{
! c' o/ G/ g* F0 a! R: U string fileName = "info.xml";3 k! T4 P {$ y* H3 K
CreateXmlFile(fileName);
% k1 H. ^0 e+ y ReadXmlFile(fileName);2 _3 l$ _, f: }; v1 y! o
}
1 D0 y$ O/ @9 U \* s& I: U$ E$ X* U. ?- q3 g/ D
7 I" v0 W& e$ z8 e! a8 ` |