请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好。 TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。 DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。 如下是一个XML片段:: E" y0 t% U: \; F4 y' }
<Persons>
% w0 {- M/ c# E <Person ID="1">! x! a" K1 a+ _: X' ]/ {
<name>周星星</name>
( Z% u. h+ C2 n5 a+ F <age>20</age># C. [+ r) }6 z) S) |# V+ y
</Person>
1 c$ j! I% B8 A5 T <Person ID="2">
6 u7 ~6 b7 V1 X+ V7 k/ p( o( c <name>白晶晶</name>% q8 V1 D8 N3 z
<age>18</age>
1 Z/ ]* W% {4 s% K% | </Person>8 R1 e* s7 f% {/ M" T1 @% k6 s9 x0 J
</Persons>
4 m2 d# m6 D+ Y" K5 E
" w+ w, H) K1 } l k. g R 在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文件:
- X; U: D0 `2 q0 x C7 Y5 }<Persons>' a1 p. |" t% J2 m8 o* a1 O2 K
<Person ID="1">/ E3 @# E1 N4 u/ W
<name>phinecos</name>
7 G' I/ L/ a9 @7 ?& S' y <age>22</age>% m0 B; U( {. Y
</Person>
) o7 Y5 J/ V* u</Persons>
6 U; w1 K8 E4 }$ V+ P, \6 ]7 j- d. R0 A. X1 f, `2 m6 s
读写XML文件的程序代码:
3 \# M8 H& V% _" Z #include <iostream>
3 {! `; p' w: o0 d4 _& T#include "tinyxml.h"& L7 L( R4 L: I) W2 s
#include "tinystr.h"3 B7 C3 N8 |- t9 q
#include <string>! m9 `, n; @" ~* O9 E! B$ \
#include <windows.h>
8 k$ i" p ^4 P+ U' Q#include <atlstr.h>8 ?+ C' |: a8 s
using namespace std;
2 s& |# m* Z7 C$ }' V1 T" I- X% \8 p+ {* ?/ \( n; B0 r1 ~
CString GetAppPath()
( ~1 }3 k3 M& k! z& `{//获取应用程序根目录
& T0 r( M" `8 u) A+ z4 h } TCHAR modulePath[MAX_PATH];
3 \# I& ]8 v$ R& v+ M: n) C GetModuleFileName(NULL, modulePath, MAX_PATH);9 v% l8 g2 _( |) G
CString strModulePath(modulePath);
+ i; C$ K! V+ l3 K$ {: N# l strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));
4 j- [& v H, A# g" m return strModulePath;
/ b" E& _- K9 ~! q}8 u* d$ |4 v- ~8 k8 z1 m2 G
# }. `2 N8 g) P5 {bool CreateXmlFile(string& szFileName)2 Z$ D1 U! c. F. }7 M n T* M
{//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false: U4 U9 Q7 k2 b( g' X
try1 ~$ ?6 @2 U0 t5 j) j# I W$ n/ _
{
8 K/ ]( O6 g& s* n$ Z# k4 e //创建一个XML的文档对象。
& y* l4 q G$ ]6 ^0 K0 J6 { TiXmlDocument *myDocument = new TiXmlDocument();
4 h6 ~9 v% M- n' c6 p/ k& K4 |6 D5 x7 g$ } //创建一个根元素并连接。! Q1 H* f, t1 `- K9 x' m
TiXmlElement *RootElement = new TiXmlElement("Persons");) L! t+ f8 \1 C& D' `
myDocument->LinkEndChild(RootElement);/ L6 ] f2 K4 W3 e0 m8 a2 x
//创建一个Person元素并连接。
q# P" ^, s) n4 x7 Q0 B TiXmlElement *PersonElement = new TiXmlElement("Person");5 f: J3 F; C8 q8 Y. d$ ] M
RootElement->LinkEndChild(PersonElement);
. p# j1 c0 [8 m/ i Y //设置Person元素的属性。: u/ F6 c2 @- Y4 S4 ^9 a0 A6 t
PersonElement->SetAttribute("ID", "1");0 q1 m$ m, d8 T, X* T( v
//创建name元素、age元素并连接。- F# ~1 P7 E# t R; p! [" s v
TiXmlElement *NameElement = new TiXmlElement("name");
2 A+ X) U% Y& m* e1 z s TiXmlElement *AgeElement = new TiXmlElement("age");
) e* v% _" A3 o; E H' J PersonElement->LinkEndChild(NameElement);
?9 |1 w- Q$ Z$ w& a6 U PersonElement->LinkEndChild(AgeElement);8 @# y* _: k; I7 t" U
//设置name元素和age元素的内容并连接。& m! R4 }) X1 n- D% _
TiXmlText *NameContent = new TiXmlText("周星星");
/ r) ^& c5 {' a3 Z; M TiXmlText *AgeContent = new TiXmlText("22");
; I* l& M3 W1 F3 q3 T6 S P+ D t3 i. M NameElement->LinkEndChild(NameContent);5 v/ ~2 J7 p5 C
AgeElement->LinkEndChild(AgeContent);& M6 V* p- h) I; G9 v: D
CString appPath = GetAppPath();
% C, i$ J9 e* O. L" N string seperator = "\\";
' W4 z3 V- N1 [. v# v. x; F string fullPath = appPath.GetBuffer(0) +seperator+szFileName;( n# S- L/ t. m! x4 O5 {- [
myDocument->SaveFile(fullPath.c_str());//保存到文件6 y0 |4 H! a/ U! P. x* k
}
6 Q x1 {* u2 y+ Y I9 b catch (string& e)
[; Y& r2 o$ H( D ^4 p3 b {
4 g4 |1 O; l3 F, L `1 v8 r( p return false;" x9 m \/ L' ~( n5 D
}4 K" j3 y& k. H+ C1 z: d' C& h
return true;
1 H0 t* H. t* a1 |}
3 h- Y* G- y+ M" q' b4 _& x H3 u8 Y7 u( {& h- g, T, k) H
bool ReadXmlFile(string& szFileName)/ K$ w/ \2 H5 N+ o, ~
{//读取Xml文件,并遍历
) ~8 ~* r# w: E+ G8 a0 P* V% U& J" r1 \ try% P1 a- H7 u# R& ]! P$ A7 h9 X
{
# H% O# T5 @' {( ~" u( d: z* d CString appPath = GetAppPath();# i" B1 C4 q3 @% ]% I
string seperator = "\\";1 x* r" k# L, \* K! y/ K2 C: s' S
string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
- {6 E. O! \/ P% I% ^ //创建一个XML的文档对象。$ I% c* u7 w, W- Y; A( z
TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());" {1 s0 y5 J( V. q
myDocument->LoadFile();
3 R4 H- p% `- J0 i: Y& j4 E //获得根元素,即Persons。' U- \+ e/ [* z s6 d, ? N& Q
TiXmlElement *RootElement = myDocument->RootElement();2 x8 ]! F8 T# r: F7 b2 w
//输出根元素名称,即输出Persons。
5 R) T* i& V0 f cout << RootElement->Value() << endl;
$ E# b- ^# d: I# h3 A% ? //获得第一个Person节点。
S0 w0 Q1 ?0 H, ~$ n7 C TiXmlElement *FirstPerson = RootElement->FirstChildElement();9 Z0 Y; ^: F( R f4 _# ?3 L
//获得第一个Person的name节点和age节点和ID属性。
+ ]' ]$ v1 m1 J0 } TiXmlElement *NameElement = FirstPerson->FirstChildElement(); V" J0 _* h# P4 O1 @: @9 H( o
TiXmlElement *AgeElement = NameElement->NextSiblingElement();& N& ]! S! Q; w' q; k) M4 Y
TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();
& d6 M' }/ C3 s2 X% { //输出第一个Person的name内容,即周星星;age内容,即;ID属性,即。( O& ?8 J- Q$ ~/ [, L* Y
cout << NameElement->FirstChild()->Value() << endl;$ Y7 A0 ~2 W- O! H0 Q0 p
cout << AgeElement->FirstChild()->Value() << endl;9 v4 H5 T% q) w9 _% W) B' m+ _
cout << IDAttribute->Value()<< endl;0 E% p$ z/ }% u5 R1 w: v$ E
}
0 o( h& J! r. S( v+ s catch (string& e)
$ P8 I5 ]3 u) ~8 g {
; l7 n' Q4 J. Q% U. T- ^ return false;0 f) R _. ^3 u! x) Q4 Q# O" {* d
}
2 x# G+ W# r8 k$ N* H+ C$ A4 p1 Z* u2 F return true;
2 G8 N$ M- E& G' X" k}
8 g {. A( Z0 Iint main()
! u. Y; L2 y1 }/ n7 R# D/ f{% x1 |( q1 ]' B9 A1 N! [" x4 n
string fileName = "info.xml";
& u; X. J) H# i8 H' J" V9 Y CreateXmlFile(fileName);3 l, {( m9 v4 k8 z/ z+ q) V% _( P0 J( F
ReadXmlFile(fileName);
U, d: g2 n+ N# a' [' e7 Z, V}
* A( f' A1 D* O& C% n- g# B$ Y
7 S; M; G1 Y0 ^) G) |- s7 Z0 d
, v; j, `! b. { |