请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好。 TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。 DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。 如下是一个XML片段:
& B% R; ~$ p- d( T# x <Persons>
3 a* ]4 n, c6 [0 [4 {/ x- G <Person ID="1">
/ d2 l+ B- G% N3 O+ L7 z1 O <name>周星星</name>2 m' z- k0 ]: E, a; w
<age>20</age>4 h) O( p) B8 r+ K8 b1 N6 o
</Person>
! l8 p, w U( I" G& F$ O <Person ID="2">
1 p& y& r: D& r+ r <name>白晶晶</name>
' V1 }/ ~; s$ m1 V <age>18</age>+ O$ ^1 K& H1 `% C) J) _& _2 n) t
</Person>
0 r2 B2 W# t3 f | </Persons>
k! V: ^: o$ r" d: T+ m H; q
. `0 y7 Q1 w3 v 在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文件:3 ?3 d6 K* K0 a+ d7 O
<Persons>9 I$ S1 n k I$ W0 C
<Person ID="1">
# b/ \& S0 { L! x! d' r, a" n <name>phinecos</name>4 d$ }, e9 c6 q% u' G
<age>22</age>
/ h9 K* d# B1 H; G6 E </Person>
; u/ V1 h. j5 {7 }; g) B9 L0 A8 y</Persons>5 q! F, m, g) _) |: a
! @, i7 X; s6 h读写XML文件的程序代码:* Q( z: Y) }7 U; s; Y+ F! v
#include <iostream>
# Y8 J( W4 {' i4 s6 x3 v) a#include "tinyxml.h"
8 w4 p0 f6 n0 `% @6 C, Q! N#include "tinystr.h": y V; e3 y2 l( v8 k. V
#include <string>
. G2 S# z4 ~( I7 H#include <windows.h>% I; M! f3 T, I* V5 E3 {. Z* w
#include <atlstr.h>% p0 S: P6 ?; p$ [5 g4 |
using namespace std;" |% a) W1 i4 I. S& d s: V6 |: T7 X
& _/ U' k$ g* Y/ @CString GetAppPath()* d$ }1 h5 b8 v
{//获取应用程序根目录- s! F* n: x& p2 T/ d% j5 _' Q9 j, B' w/ X
TCHAR modulePath[MAX_PATH];( p% u4 Q3 N( J
GetModuleFileName(NULL, modulePath, MAX_PATH);
% N8 E$ q- _/ a4 w P$ l/ @ CString strModulePath(modulePath);7 v1 K' X- B+ ~& a( k2 n
strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));
: q( d& P* M y7 y) U% B; { return strModulePath;
3 W; V2 f- l2 `}
0 L" Y( P$ f# x* p- L+ D
! O* m2 i1 K; ]2 u7 lbool CreateXmlFile(string& szFileName)
3 Z- q( P& I* [. H- m Z: s+ x{//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false
6 x/ X6 J; o0 a3 ?7 J try
$ f$ x+ J1 @) V! v {
) [0 ]8 a, ^' o% M- ]2 l4 J5 k //创建一个XML的文档对象。: S( Z/ q9 q1 g
TiXmlDocument *myDocument = new TiXmlDocument();
: {9 Y' i0 T( W! ^7 B* y //创建一个根元素并连接。
( p/ m- V q$ y4 b TiXmlElement *RootElement = new TiXmlElement("Persons");
& X6 t4 Z( T( |7 P7 n myDocument->LinkEndChild(RootElement);
$ y- _1 @: m$ c: w. [5 C# H //创建一个Person元素并连接。; k1 o: e% a& W! E" K9 m9 i
TiXmlElement *PersonElement = new TiXmlElement("Person");
) p( G) s7 L2 l0 K0 B9 {1 v8 T RootElement->LinkEndChild(PersonElement);
+ H# F) j4 C6 c ?. x8 D9 W //设置Person元素的属性。: S- z2 ?( B: q6 j
PersonElement->SetAttribute("ID", "1");- J" z7 C5 q' g; Q, E9 y* O
//创建name元素、age元素并连接。
3 Y0 @- {! c# _5 H# h- N/ x8 M! g TiXmlElement *NameElement = new TiXmlElement("name");" K. k/ S; n, u6 {
TiXmlElement *AgeElement = new TiXmlElement("age");
( n% x1 t) \) b5 t PersonElement->LinkEndChild(NameElement);2 J* t6 v! A" K
PersonElement->LinkEndChild(AgeElement);
0 z" w2 m. `; A+ O: N! @9 t& Q: l //设置name元素和age元素的内容并连接。: g m/ O* B9 P v1 o; p
TiXmlText *NameContent = new TiXmlText("周星星");
) i \- ~" K V! K8 _1 K TiXmlText *AgeContent = new TiXmlText("22");& K% h( x/ l0 Q0 F4 _" Y- z
NameElement->LinkEndChild(NameContent);
$ C$ T+ y& o$ q AgeElement->LinkEndChild(AgeContent);
, ]" ?! S$ a& H' S CString appPath = GetAppPath();
5 q) ~$ s: Y* j% P- L string seperator = "\\";
! L q6 m) C8 j+ O string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
5 C. |" a( S- e$ { myDocument->SaveFile(fullPath.c_str());//保存到文件
6 ^, O7 A6 f( e0 B7 c' K }
0 d: e) o: h' p5 J catch (string& e)
: s; N* M; ^1 z6 S' h {
5 K5 }( H1 h! O% ]: a6 { return false;$ D, J$ L9 p. f+ v. Z7 z. W+ X: ]
} ^$ p9 r- z' P. I. l
return true;0 e, _. Y5 B* K5 Y/ S8 S& W9 K
}
" L& E* r& S# U: y2 d+ t
7 h7 B, i0 R9 j+ |8 u: [& ^bool ReadXmlFile(string& szFileName)6 }, F$ O$ S D( I3 G
{//读取Xml文件,并遍历 ^* n" G: z7 m6 y/ M
try
# ^ h; n; G0 B# [. P8 H {+ x4 S( u1 Y4 h4 k
CString appPath = GetAppPath();
! W, m9 L$ _0 ~0 [
: U8 e) l" s! q) s# F& q# `) Q; B& F1 C 登录/注册后可看大图 string seperator = "\\";
, j! U: X" G# \+ U) B9 v8 H string fullPath = appPath.GetBuffer(0) +seperator+szFileName;- M5 }; p3 G' _+ x+ G! c+ s. U
//创建一个XML的文档对象。5 m& u/ U4 s& X$ Z/ u# i0 x
TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());( K. n: Z. G% W9 N4 u1 `- V4 X- ^& Y' x+ @
myDocument->LoadFile();
% L: y! L+ w2 [0 {, w //获得根元素,即Persons。1 H$ B0 Z& Q' \. y2 F
TiXmlElement *RootElement = myDocument->RootElement();" S( I- Q2 B3 S" o- ]' C. a
//输出根元素名称,即输出Persons。
, i" X) N' R9 g( E/ W; ^0 M; W cout << RootElement->Value() << endl;
3 L3 t5 M* U w //获得第一个Person节点。: l# O$ s# T0 C: v
TiXmlElement *FirstPerson = RootElement->FirstChildElement();
) L- ?6 _' S+ ^ s$ O //获得第一个Person的name节点和age节点和ID属性。3 x' Y, j0 u% \. `( e* P
TiXmlElement *NameElement = FirstPerson->FirstChildElement(); a9 Y7 j/ e g' z. X
TiXmlElement *AgeElement = NameElement->NextSiblingElement();6 v F! q# }$ a0 E7 T+ j
TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();
3 _3 o/ h, S# S* J$ W8 g //输出第一个Person的name内容,即周星星;age内容,即;ID属性,即。
! d5 S8 y; I0 g" p cout << NameElement->FirstChild()->Value() << endl;
2 D5 L5 ^* Z! U" U3 q cout << AgeElement->FirstChild()->Value() << endl;5 e. W) ?; b0 v: h# m
cout << IDAttribute->Value()<< endl;
. _. |* Y# G+ x$ l( t/ x8 i }8 L9 Z4 b7 f4 }
catch (string& e)
# V. j( A2 ^+ {. l {$ q8 _" i: ^, W+ }4 M: z: F7 g
return false;
. d' B. t* }3 X+ C" o6 p }. [+ q7 e& u1 S( M1 u$ v
return true;
* ?5 }! {* D9 l+ x}3 \& {' f2 P* ?6 u0 p6 c/ F
int main()
Q' n; m4 @# c: Z" p) i# t$ V{2 D' r0 D& n6 D3 C2 r
string fileName = "info.xml";3 Z9 \2 k+ q5 R8 H
CreateXmlFile(fileName);8 Y X3 k7 Z/ Q) o' y
ReadXmlFile(fileName);7 {& f) o1 \# o6 Y7 y: e
}( b. r% R8 `+ t/ z2 t2 g$ J
2 T2 p' b) A3 G
1 h$ Y4 D# V$ W/ m
|