请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好。 TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。 DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。 如下是一个XML片段:. I; M( b( y0 B+ [
<Persons>" o3 D: b# a/ V4 D
<Person ID="1">) B1 N# B# ~; p% F
<name>周星星</name>" F, D9 O& e! W: Q8 X
<age>20</age>
8 e. M6 Y8 v# F, u* g </Person>
) |# g6 g$ y/ F9 ?" b <Person ID="2">
3 B: @( _5 {1 M. n <name>白晶晶</name>
6 a6 G4 }" ?$ \" \8 j% p ]" ?! i. g- s <age>18</age>
% m; j5 a+ K, L& {8 j, M0 D </Person># z2 T9 i- I/ h' B: r3 S1 F6 I
</Persons>
0 k2 `# X6 p) J2 G
' b6 C" z ], J$ o3 o8 ? 在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文件:( W. P+ S9 n* p: v; h0 s
<Persons>) O7 Z" S: G; Z) g3 z z! z
<Person ID="1">, e, N n, Z/ R* q6 X
<name>phinecos</name>
( H# M( b$ B8 S% k$ N <age>22</age>, R4 v" A9 \# P
</Person>! ~+ z/ d& Z \) Q4 K" X' _
</Persons>6 o" [4 q, Z: `$ P4 q0 b
, d9 z6 L9 n+ ]/ D0 z/ ~
读写XML文件的程序代码:% d1 Z+ b: w" C$ k5 X' H3 @: X. d
#include <iostream>' z+ u; g N0 d- a7 V
#include "tinyxml.h" c1 b. m; h2 J! W3 w) _. T
#include "tinystr.h"8 K+ L" y- p2 D! y: d. |8 {" w
#include <string>
. f& J0 g( L7 G$ U* q#include <windows.h>0 F& N! ~8 W! S: f0 U7 k
#include <atlstr.h>
, X8 ?5 G# m$ \5 wusing namespace std;
: E) k5 y7 S I8 d8 q+ Q J% R: X3 \5 G
CString GetAppPath()
/ o1 b3 H- y* w{//获取应用程序根目录
1 E' {9 G( j0 ?: g2 c1 w TCHAR modulePath[MAX_PATH];& Q# F4 T! ^% L8 p
GetModuleFileName(NULL, modulePath, MAX_PATH);
1 H9 [1 e0 u c7 P CString strModulePath(modulePath);
4 D& D1 V, w$ a3 Y" d7 m& \5 _ strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));! w+ A2 a6 F+ p2 |4 x
return strModulePath;
$ b# A" s% C; |0 w [6 ^; X}- c& q/ t* g* \: @
8 T$ F5 X. N' Q! h0 M* O
bool CreateXmlFile(string& szFileName), c" G( V, V0 R5 ?6 ^( c F7 Y% F
{//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false
' h! X. V- ~2 l" Y \) x% n" ^; \% G try+ H6 Y: i8 O& [1 V
{9 J2 Y* ~3 ]' q: ^6 u% Y
//创建一个XML的文档对象。
( R8 {3 d% \2 o, V4 \ TiXmlDocument *myDocument = new TiXmlDocument();1 E! r+ J/ E1 e/ B
//创建一个根元素并连接。* B& U' C6 i3 l8 j/ G
TiXmlElement *RootElement = new TiXmlElement("Persons");/ p# F/ e: m4 L0 z8 f' b; v
myDocument->LinkEndChild(RootElement);
$ T: H7 V7 a0 q7 d //创建一个Person元素并连接。
* _4 H6 G! `5 C) g# M TiXmlElement *PersonElement = new TiXmlElement("Person");
) c D& s6 z1 w2 ?$ { RootElement->LinkEndChild(PersonElement);( w; y/ f, N/ r9 v' z( [
//设置Person元素的属性。2 L4 {0 q$ C g: j
PersonElement->SetAttribute("ID", "1");
* ^* d* ^) W/ P" L( @' D( H. m //创建name元素、age元素并连接。 c& M) N0 R S0 U$ b$ i
TiXmlElement *NameElement = new TiXmlElement("name");: s% Y0 }! w. A" o* ?( F) n- M
TiXmlElement *AgeElement = new TiXmlElement("age");
* n& `# P T& R# q. h) Z PersonElement->LinkEndChild(NameElement);
2 h% w- C3 M% u. [$ _) [6 X PersonElement->LinkEndChild(AgeElement);
2 X# l+ ^7 c2 J3 N; r. e8 `1 n //设置name元素和age元素的内容并连接。" B+ G; p0 Z5 B4 v# i+ I
TiXmlText *NameContent = new TiXmlText("周星星");5 G0 L; @6 @5 s0 \( B" U& ?; S
TiXmlText *AgeContent = new TiXmlText("22");
- m/ ?9 l% u" q' j# I NameElement->LinkEndChild(NameContent);+ D9 W: L5 d; g9 j
AgeElement->LinkEndChild(AgeContent);
! m l+ ^2 l% c" U2 \ CString appPath = GetAppPath();$ y) I# x: V% R
string seperator = "\\";
: O9 |0 K& d6 Z0 e- p2 Q string fullPath = appPath.GetBuffer(0) +seperator+szFileName;2 s( a. ?* D5 z( i7 h* m- Y/ ]
myDocument->SaveFile(fullPath.c_str());//保存到文件3 o4 K1 T0 D$ ^. j
}( X$ a6 m7 E9 X- h& T& s
catch (string& e)3 m2 ^. A/ \. p& W
{
8 y/ ?) ~# z! F( t2 @ return false;
* p) z: F* d' I$ T; \ }
7 L6 i" G( z% c- _$ ~ return true;, j% X6 u( d. G' d4 d# S4 C
}
7 I4 ~5 x3 j8 V# j6 }* I! m. I
bool ReadXmlFile(string& szFileName)2 S2 O. _- _! \3 h( p2 F3 Z
{//读取Xml文件,并遍历
8 X6 p" Q4 q! _& j; N try
, J1 j# v; R6 x2 r$ h! T: A {
- p( [7 P# A+ u9 T' Z CString appPath = GetAppPath();1 z6 L, V" ^9 ]& H0 U/ ~ D
string seperator = "\\"; P, k8 P6 W: p
string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
# S4 f1 W# h$ \2 Y* x" z* ]. i //创建一个XML的文档对象。
* H. _" D3 t- g( K TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());' R% m. M- A0 m N P4 }# e
myDocument->LoadFile();5 p4 E2 \# L4 F5 g h/ N! [
//获得根元素,即Persons。
" I7 d$ @3 E/ f TiXmlElement *RootElement = myDocument->RootElement();! {" ?( k8 g& T$ r1 a. e7 w
//输出根元素名称,即输出Persons。/ m6 e: j' z8 v- o
cout << RootElement->Value() << endl;
3 A- @4 s9 ?( E# n; C2 } //获得第一个Person节点。
' ~" I% U: i0 J5 |3 u TiXmlElement *FirstPerson = RootElement->FirstChildElement();
5 z1 B; L7 W8 c/ N) l //获得第一个Person的name节点和age节点和ID属性。
5 k# l* n: B7 w* J" m TiXmlElement *NameElement = FirstPerson->FirstChildElement();8 Z! o9 E/ u v1 w9 h
TiXmlElement *AgeElement = NameElement->NextSiblingElement();
6 I# D J+ i3 D0 Q4 K1 y9 v TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();6 V! e' o- D: n. X4 q) l- Q
//输出第一个Person的name内容,即周星星;age内容,即;ID属性,即。, q# P5 ~* z* n: f
cout << NameElement->FirstChild()->Value() << endl;0 ? u8 g) @& K3 v" R5 f [
cout << AgeElement->FirstChild()->Value() << endl;
" r3 ^* A( T) s8 C' |' C; n# W cout << IDAttribute->Value()<< endl;
3 S: ?$ A( }' \! ~7 |2 G& C }
8 G2 y( ~$ X4 h* v catch (string& e)
& Q% g! b- t: r9 \ {
) l$ m4 X$ K- N. L return false;
) v9 o2 U- }8 I8 H2 Y: T, o2 Q9 A }
' R! u# t& {" I; k0 \$ J T y return true;
% F* h8 S+ Z& R b1 X/ A}( A5 N1 V" G g: b
int main()
/ j0 q6 M7 I- a1 k0 |. m i' |" C* F{
' @/ \3 _8 O; S: j9 L. o L. G string fileName = "info.xml";% G8 [# k& c% c# G( N
CreateXmlFile(fileName);
. e( c$ {+ @/ S ReadXmlFile(fileName);
- y+ J2 j% U1 R( }: C- T+ n4 p}9 T6 i2 J' \9 C5 p) R1 i
( |4 k5 g, W, I! m& i
" J7 D3 c# {8 F$ \5 M |