请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好。 TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。 DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。 如下是一个XML片段: T$ L+ q( g7 f, d
<Persons>
; G/ Y4 [, ?+ r) T4 D <Person ID="1">: I# I. X5 r) A D
<name>周星星</name>
' U: W# Z- S0 J <age>20</age>
6 o. y! G$ E/ q. y# v, K </Person>
# @6 s6 i7 i! s2 D! g+ I7 |1 R <Person ID="2">
# y% Y7 Q$ @* T1 T1 ? <name>白晶晶</name>
' O4 p" T% Y! | <age>18</age>$ a3 }: A4 K3 F8 B! S
</Person># V% ]+ A( s. f) {1 w
</Persons>. J" B1 B* c% R( a7 x- l: l
5 N- M: ]* W* G6 `. z
在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文件:2 `, s3 ~# r6 K. a0 R
<Persons>; h! u, X4 e# M f& }
<Person ID="1">
# W8 f6 a; I) s <name>phinecos</name># u3 \9 ]1 Q& o) H# Y! t0 G7 q
<age>22</age>
/ x' r( A9 |! N7 ]: b! L </Person>
4 l4 t2 m0 K9 t</Persons>
. Q& \" ?/ Y2 h' q4 W; i8 ?' D2 q- T' o
读写XML文件的程序代码:6 a+ t& S8 M" T/ ]
#include <iostream>+ O, x) [% Q, W; D& T
#include "tinyxml.h"
* Y' l: A; P8 R5 m#include "tinystr.h"# ^: y$ r3 ]* A9 J
#include <string>
4 a0 c. `1 S, f2 E- A) v$ o" j#include <windows.h>
8 E* E5 c: w+ c: P& b4 _#include <atlstr.h>
, B- [! o3 D+ v- Vusing namespace std;1 ]. Q6 U9 H/ c: P0 d+ f+ U
' J! }+ F M$ \
CString GetAppPath()
3 r+ r3 a- O2 N{//获取应用程序根目录4 z4 F* ^. o7 j$ V W
TCHAR modulePath[MAX_PATH];6 X; {+ h# E Y6 y, S0 ~9 |
GetModuleFileName(NULL, modulePath, MAX_PATH);
3 Q* a# `* @" d, y' R6 _ CString strModulePath(modulePath);2 _4 h' C) K" Y# _' C1 u6 ?) W
strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));
. j4 U; S5 ~8 | return strModulePath;
$ }' ~; L0 s" Y2 q3 [3 F}
1 o' d! b) B8 v3 p& t/ o7 l+ `' x% c7 u8 O: c5 o
bool CreateXmlFile(string& szFileName)
6 V8 K Q& u, |* a{//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false
! s# J# X# m2 A8 R9 A) S try2 L$ _6 u; A% _/ X. V$ A
{
7 F4 B$ e! C% q, S+ Z6 o //创建一个XML的文档对象。, N9 M. u7 b7 n
TiXmlDocument *myDocument = new TiXmlDocument();" p. d: P# v! {1 T. h
//创建一个根元素并连接。
$ B( W2 R8 |5 v3 e6 C TiXmlElement *RootElement = new TiXmlElement("Persons");# d. d: ?, ]/ p9 [' P9 d5 d
myDocument->LinkEndChild(RootElement);
8 {, `( E' R& \3 U //创建一个Person元素并连接。
* x! \9 D4 t; w+ g4 S TiXmlElement *PersonElement = new TiXmlElement("Person");9 b: _( V' K4 J% M3 H0 ~
RootElement->LinkEndChild(PersonElement);
9 m, ?: T( N! n$ L0 o' ` //设置Person元素的属性。
, ^2 V: Y, y/ j/ ]- g: I( ? PersonElement->SetAttribute("ID", "1");$ q( ?7 a* c' b( R3 ]2 [& F
//创建name元素、age元素并连接。+ D! v5 q) ]7 F
TiXmlElement *NameElement = new TiXmlElement("name");
( w" z/ Z# w0 n TiXmlElement *AgeElement = new TiXmlElement("age");' Q8 B9 ~) j$ K
PersonElement->LinkEndChild(NameElement);' _4 E4 z( a7 w" }
PersonElement->LinkEndChild(AgeElement);5 W' N7 V, ~& B$ F% n9 ^+ F- h
//设置name元素和age元素的内容并连接。5 l5 Q2 ?' C( Y5 k7 l
TiXmlText *NameContent = new TiXmlText("周星星");" V' e$ P( t* y- y7 }( M: F- o5 t
TiXmlText *AgeContent = new TiXmlText("22");
2 D+ H2 _8 w }9 O- E NameElement->LinkEndChild(NameContent);0 j, i) Z0 ]5 A/ j; l
AgeElement->LinkEndChild(AgeContent);
7 r+ h& C3 F) L+ U) C CString appPath = GetAppPath();
1 f7 N$ h- G( h# A& Q! a* F string seperator = "\\";
+ d" M/ I: z; `& K1 ? string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
5 N- [* j2 |: I myDocument->SaveFile(fullPath.c_str());//保存到文件
; H+ r( }$ D# {! z& H% e }- I% b9 x, I) \' D8 m
catch (string& e)# q; I* I9 G# F9 e: m% A9 c
{; n: j2 A5 c7 c# @ A P. ]0 g
return false;
; w- k5 a% |3 Q, ~+ Q }$ F3 p( K+ N3 ?/ P6 A* o7 o
return true;, @, t$ ?" _, o+ D
}1 f3 b+ d- I9 {6 ^, |9 A$ H
9 q+ o5 e* Q* w8 P; m( j) \2 ebool ReadXmlFile(string& szFileName)
S6 x: g5 |8 |. o1 n5 R5 U{//读取Xml文件,并遍历0 n/ [ w8 r* [9 O
try
/ k$ r! J- d- r/ O }! [, k {1 h) p% i, C( T( K, w: [( X! @6 }+ g
CString appPath = GetAppPath();
( |4 Z6 f2 e' d' { string seperator = "\\";
4 \& V! _; a. V9 x) W4 v string fullPath = appPath.GetBuffer(0) +seperator+szFileName;6 ]: k/ N1 U/ B d- r$ |( @( z
//创建一个XML的文档对象。- l1 v B2 i/ `$ u. ?8 t
TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());
8 i$ J7 N+ ]" C0 C& Q8 Q( A9 H myDocument->LoadFile();
1 j8 e9 D: h% W* w$ V J6 } //获得根元素,即Persons。
2 g# b5 @* G( w$ q7 e9 d TiXmlElement *RootElement = myDocument->RootElement();
3 X% T4 z4 Q" p //输出根元素名称,即输出Persons。
! N. d; A3 s. I. U* c9 d cout << RootElement->Value() << endl;
$ I( D2 ^. }, H6 P+ H2 C0 b' r; I //获得第一个Person节点。% ~$ p2 y1 R8 q& I9 t i
TiXmlElement *FirstPerson = RootElement->FirstChildElement();& g/ V5 R$ G% o& J: R8 N
//获得第一个Person的name节点和age节点和ID属性。
) p) z1 }# G& f3 }2 b TiXmlElement *NameElement = FirstPerson->FirstChildElement();2 ]" |" }% l$ H& i a; A% z
TiXmlElement *AgeElement = NameElement->NextSiblingElement();, ?; o# r C1 j; o
TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();
( V5 c8 g$ ^# o& J6 m //输出第一个Person的name内容,即周星星;age内容,即;ID属性,即。& m, ^8 m7 l2 u( }3 z2 k) e
cout << NameElement->FirstChild()->Value() << endl;
. f( i2 z7 p1 r# `; _% B cout << AgeElement->FirstChild()->Value() << endl;
; e1 r- y! k P0 K$ U1 X cout << IDAttribute->Value()<< endl;
' \) r) V1 e% i" S! h }8 ]6 U" l7 @ R0 d5 e9 ? E! H; R
catch (string& e)/ r, w4 d( b# ], _* K
{
- i5 }, l* |3 p$ k0 D return false;
1 A& u/ F6 @8 R2 S1 |2 Q6 u0 o k- p }
8 F8 v' [2 O8 w/ g4 x& i. [ return true;
- ]& I4 p& x6 t+ u}
, r( e9 y7 K0 U- uint main()+ D( ^1 j& g/ q$ F- U5 L9 _9 q& d
{
6 V7 G: K% X' I/ e2 |; K/ m string fileName = "info.xml";
: Z& g. a6 T% W- @8 ]# G k CreateXmlFile(fileName);
$ M8 U6 @- y+ R& z; K7 U R( a ReadXmlFile(fileName);
3 _: J2 |% O2 ` E3 v}
6 i( J; R" q9 ^) `2 X% g
2 E' D/ {5 ~8 d; g( C3 h5 t( _4 X) ]- Z, w9 A
|