请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好。 TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。 DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。 如下是一个XML片段:4 e D, D6 w0 U5 d/ @1 y& e9 d
<Persons>& T" P# w/ _9 M1 ^8 W1 k8 k
<Person ID="1">3 i0 e9 P, _7 M& {7 h: g2 g
<name>周星星</name>
# B+ V; e! R$ k) d5 {1 x! p, [! D <age>20</age>: ?2 z$ B, x% t/ a4 m/ H: J1 k, R
</Person>4 u4 ~5 d2 ~( ]& T" h5 Z
<Person ID="2">/ l% X& C4 k! k
<name>白晶晶</name>
* H1 l- z% b' g: I e* m0 @ <age>18</age>5 @5 h: {2 C( A1 f. F: p0 w
</Person>6 Q% u; _) x2 b/ _7 g" C
</Persons>8 R* ]4 S- G' ~' n) ^/ b3 K
' J5 a; b8 J; X% 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文件:3 ?& }1 j- L% F# t4 ^- L
<Persons>
) k" P ~0 L/ ^% A# p <Person ID="1">4 u I( u3 O9 V P, f. H' u9 D
<name>phinecos</name>5 |( w9 z" d% v" }9 S! P
<age>22</age>
- o& e# X9 ]0 I' \7 A </Person>* K3 V. A# T7 g) V2 E2 {' J
</Persons>
6 w! d- m& L' D' k. `$ k; l, _+ a V n5 z+ a+ t& t/ R, z
读写XML文件的程序代码:
& w, S2 ]7 U& N& g #include <iostream>
: \ ^. \- Y$ _, h2 H* \#include "tinyxml.h"7 h9 u$ b4 i0 V, D: B# b+ P
#include "tinystr.h"/ c/ _, ?' U$ t) f) {' E
#include <string>1 u! j9 W* I1 i# T! ` }- H
#include <windows.h>! B1 y/ v& Q3 {: H6 i0 A, Y+ I$ W
#include <atlstr.h>3 T# r6 _1 J9 J2 o7 T$ Q
using namespace std;
; u% g& q+ O# s, E2 N* s7 r! X* H- |$ z
CString GetAppPath()6 J) ~0 [: K# G( Q# J n: u
{//获取应用程序根目录
1 j7 { a9 [, r" z TCHAR modulePath[MAX_PATH];
" Z" _% G/ L$ b1 \( x/ ]3 T GetModuleFileName(NULL, modulePath, MAX_PATH);
% r) a+ V$ G0 [8 t/ | CString strModulePath(modulePath);$ v! S6 J8 n I* Y6 N- b* N
strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));/ K6 r! L# z+ P8 u
return strModulePath;
) w2 P" l( [ e% R+ V& h( v- }}
5 r( Z7 d8 Z! u; C; x/ P
7 b! S1 @, L' T! }! z$ g3 |bool CreateXmlFile(string& szFileName)- V8 h+ H \: |* i- g
{//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false
! g. U' K/ |/ [" H7 n1 y: d4 V2 e3 R try
" N2 B2 [4 [" Z- D* F { T9 c. r' S7 \& e1 [: K
//创建一个XML的文档对象。
3 Y9 _" z5 _4 p+ D. v TiXmlDocument *myDocument = new TiXmlDocument();/ {% D- Y. x) o8 K
//创建一个根元素并连接。9 _/ i5 y! T# H$ o0 S0 t
TiXmlElement *RootElement = new TiXmlElement("Persons");
) H" P/ z& w9 G myDocument->LinkEndChild(RootElement);
1 _. B+ c% f9 H! F6 N //创建一个Person元素并连接。& Z6 D/ B: W; D/ V# {' s
TiXmlElement *PersonElement = new TiXmlElement("Person");
7 v' q* m( W* ?& |* `# N RootElement->LinkEndChild(PersonElement);
7 q; \3 }* A+ O! r& M6 P //设置Person元素的属性。4 ~0 k0 }$ |# i2 c1 I5 w! ~
PersonElement->SetAttribute("ID", "1");
* c& b. K7 N8 z9 v' L //创建name元素、age元素并连接。5 k. l& X/ ~- e( W
TiXmlElement *NameElement = new TiXmlElement("name");! Y: z2 K, t. L' \
TiXmlElement *AgeElement = new TiXmlElement("age");
, O3 E0 W. g) T7 N7 J& L2 N PersonElement->LinkEndChild(NameElement);
8 U, C9 X1 G$ ^. i PersonElement->LinkEndChild(AgeElement);$ I, i5 s4 a% k
//设置name元素和age元素的内容并连接。( M- p$ P) E2 ^; M7 [* d5 ^
TiXmlText *NameContent = new TiXmlText("周星星");
- n$ W% ^3 T5 b1 k) k) v, T7 B TiXmlText *AgeContent = new TiXmlText("22");
$ h% m1 J. V; Y$ S NameElement->LinkEndChild(NameContent);1 x3 T7 @! e* P
AgeElement->LinkEndChild(AgeContent);
$ T0 I( X7 _. J' W CString appPath = GetAppPath();2 ^. {! s1 R7 ~
string seperator = "\\";" w9 q2 D5 B8 H6 F9 {! X5 s; F
string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
0 h8 A( x1 R; b0 ^- I2 g myDocument->SaveFile(fullPath.c_str());//保存到文件
: ]3 {9 `- \" l7 C/ P" N }3 p$ f% }) f/ ?- ^4 I6 T
catch (string& e)
6 \+ u. t/ o. @ t {
3 X3 r; B; [) y3 f/ s2 O return false;
- o/ O8 H3 }) q+ N$ g/ Y 9 l, u7 c0 y' @8 J' a) X! d, a* T' Q5 _
登录/注册后可看大图 }, N6 T+ K1 ~) d1 |
return true;: f4 n3 P5 D% s1 H
}
2 z: \3 f1 a1 q b2 s! _2 O. p# v1 F9 U0 P2 P
bool ReadXmlFile(string& szFileName)
7 {- ~. X+ A& ]: m8 U6 m{//读取Xml文件,并遍历
; N3 ^2 z" Z0 [7 H+ f try7 q. b& r9 h$ c3 g
{
6 Q; \9 g3 B: Y' H$ e; W9 q7 g CString appPath = GetAppPath();7 t }( x) f9 W4 k. `# e
string seperator = "\\";
* D Z5 P% Z0 @7 P# y6 u string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
- }0 d, a5 @5 s //创建一个XML的文档对象。
6 Z& n- ]& A% q( a% \ TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());2 B% I# R9 R. d! N/ `! r" J
myDocument->LoadFile();6 i1 k" j, F9 \7 X
//获得根元素,即Persons。' A# X/ @2 o; v
TiXmlElement *RootElement = myDocument->RootElement();
* s* d1 m9 k$ k1 Z //输出根元素名称,即输出Persons。3 N8 N/ `0 K1 `4 z# Z; }- h# `
cout << RootElement->Value() << endl;- v9 A3 i' n, H2 e- d# y, M& k2 Y
//获得第一个Person节点。
% |) E6 @1 g3 e; r8 ] TiXmlElement *FirstPerson = RootElement->FirstChildElement();' o) L$ x% _! t+ G
//获得第一个Person的name节点和age节点和ID属性。
8 `2 g3 D7 Q: p TiXmlElement *NameElement = FirstPerson->FirstChildElement();
2 ^- u; ?& i7 n/ P5 D0 @ TiXmlElement *AgeElement = NameElement->NextSiblingElement();
3 l; o8 {5 `, x" ], B0 }1 D" @; ?* r TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();4 ], O! Q% P- U' e& B: C) P2 H1 X
//输出第一个Person的name内容,即周星星;age内容,即;ID属性,即。
) g, N" g7 n. i) C7 C! I) H cout << NameElement->FirstChild()->Value() << endl;
) n9 E" C+ J; M3 P+ M6 n; J& o6 l cout << AgeElement->FirstChild()->Value() << endl;9 y8 A& f$ r: Y: n- F% A; z
cout << IDAttribute->Value()<< endl;9 y/ Y* O' q4 t2 u
}
; P" Y0 Y2 H. J% T$ H# m9 R! }1 ? catch (string& e)3 D' D* q5 ^1 A& u, u( t
{
0 F7 m( Y3 o2 z9 l- g/ _% p8 ? return false;
3 ` U" t. Z/ e' V* |, n }
) H4 h9 s3 t M7 K8 P return true;
3 e3 s4 Y: o! y+ }' V0 j/ ]}6 V" Z* `& c4 M) V, _( e+ N
int main()
* d4 N% L7 }. d2 r{0 a$ z7 q" V( O4 V' ~, X% [' y0 F3 L
string fileName = "info.xml";. i2 O3 Z8 i' T, ^
CreateXmlFile(fileName);
7 D4 C) L0 L" y; J F3 m ReadXmlFile(fileName);7 x9 L, `4 t* o; Y' k& h- X4 u9 A
}
% o! g: V* V' @$ r; a4 f7 B3 P3 [5 E; D; j+ S% Q* h8 a" [
; A; C" x5 |; `7 ]+ A0 \
|