请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好。 TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。 DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。 如下是一个XML片段:
, k' A/ N) ]2 ~% {" Z/ v <Persons>
7 f0 c2 I9 S$ n* C8 ] <Person ID="1">5 a; f& v9 Q/ Q$ e" x0 o& T
<name>周星星</name>
@7 E) \ t' R( F; n, z# @ <age>20</age>. Q& I5 @2 K; _. r7 U) b
</Person>. E* h7 v& C1 a. U
<Person ID="2">% P6 ?+ {1 t3 Z( Z
<name>白晶晶</name>
6 b& [, p' |0 g- B" h9 J <age>18</age>9 W" {, n q/ h' C5 g1 L
</Person>
' M+ E- ~2 s6 f: o </Persons>) K- z" x$ C" Z1 ` z3 e
! S6 `% m4 }6 ?
在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 V7 ~! D. `6 Y& A; x5 |
<Persons>4 Q) A; S/ g0 r' E I% Z$ K, g1 A
<Person ID="1">* k5 z( ^" O5 T" n
<name>phinecos</name>! K7 D% D& u' r/ T- u' V* Y4 `0 S
<age>22</age>
0 d3 F, P* Z$ E& o, ^ </Person>) a% W" k t2 h1 m) V! k
</Persons>) S" B% t" J; I K! f
4 ?! b* i0 I6 t# |; o
读写XML文件的程序代码:) O% ]' r- p- C. Q# i$ |) K
#include <iostream>
3 |! M# O& n8 T2 A, E9 y+ D# X#include "tinyxml.h"
) u2 i/ K7 ` ~' R#include "tinystr.h"6 u7 r" h+ {6 C2 y5 X
#include <string>
3 N' ^" H# u7 c( q3 V. G& I, {. k8 X#include <windows.h>0 Z ?8 w2 ~" w% C6 l& e+ X! W
#include <atlstr.h>% [8 Y# [8 m7 K4 N- {" i7 k' F
using namespace std;
6 [, ^# E4 |1 y2 u% k: _
, w7 b$ ~6 ?+ a) c/ H/ L J/ CCString GetAppPath()
2 q4 R0 x" ?" ~0 g% c! W' x{//获取应用程序根目录/ Y3 W9 [& u `; {8 F0 y, h
TCHAR modulePath[MAX_PATH];1 l; c9 X. z2 d
GetModuleFileName(NULL, modulePath, MAX_PATH);. Y& x' {, D0 r; \ i) p2 {
CString strModulePath(modulePath);: E. x: j: i5 U
strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));
! o* s4 a' M3 z9 I+ ~; } return strModulePath;
, q8 P5 k/ c: O( w! M. R# k' [}7 K1 U$ w0 h, {( y
. ~: h+ O7 [: C, nbool CreateXmlFile(string& szFileName)
1 q% I c9 e; {{//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false' o% j% W" t1 m6 }' [+ `; w
try$ d" p! k1 V4 h+ A5 [+ O
{
$ Q, l+ _0 P# y G) w //创建一个XML的文档对象。
; a0 n% o1 ]4 j5 z2 }/ ~ TiXmlDocument *myDocument = new TiXmlDocument();
) i# K, C/ f! ] f3 Q9 s //创建一个根元素并连接。% x* |; K+ o! o2 W! |/ u
TiXmlElement *RootElement = new TiXmlElement("Persons");
7 m4 w# _' }3 F7 u( i myDocument->LinkEndChild(RootElement);+ ^1 q ^7 J' J
//创建一个Person元素并连接。& U, a) t; O3 U/ _; z
TiXmlElement *PersonElement = new TiXmlElement("Person");
* k: H: z; q9 F$ T5 l, A6 S RootElement->LinkEndChild(PersonElement);
9 V3 W# z& t2 o, P: a# d //设置Person元素的属性。# w) e; ^/ H( B1 o1 n# a' u; p
PersonElement->SetAttribute("ID", "1");
! Z# C+ y7 {# \8 w, _ //创建name元素、age元素并连接。/ ^7 M3 I0 b( K: O& Z* q& r7 j) R; j
TiXmlElement *NameElement = new TiXmlElement("name");
' j8 @4 m7 w. d& J4 ^ TiXmlElement *AgeElement = new TiXmlElement("age");
4 T% n- K6 h2 o& O PersonElement->LinkEndChild(NameElement);, F1 \: q: Z f7 P
PersonElement->LinkEndChild(AgeElement);
4 m5 a4 h8 W; @' B+ D5 A! X; _3 n //设置name元素和age元素的内容并连接。/ j7 N/ Q( K' J8 i' M
TiXmlText *NameContent = new TiXmlText("周星星");! e7 `* W& b( h& q4 J6 H
TiXmlText *AgeContent = new TiXmlText("22");
0 \; l2 ]# t8 y& z7 Z; w NameElement->LinkEndChild(NameContent);+ Y" k3 E. B5 J
AgeElement->LinkEndChild(AgeContent);
, c) ?4 ] r! n$ l CString appPath = GetAppPath();2 g: o; U7 L% S# E9 R+ X4 O
string seperator = "\\";2 \3 l* ~+ w- U$ n
string fullPath = appPath.GetBuffer(0) +seperator+szFileName;+ t) P! z; A- h* B$ F
myDocument->SaveFile(fullPath.c_str());//保存到文件
& A5 ]4 v* X/ O }
9 y+ f) b! M | catch (string& e), [* E. h# T2 L# w7 V: V0 N3 [7 j
{
c8 ]/ }* l' j' s! f& O return false;: o, ?1 \* m2 B* D
}( [2 N% S1 d6 ^% P" F
return true;. }/ N2 }1 K0 E* ]: X
}
# Z" J3 g X; C' ~! p
4 B7 k9 P: M! y( [/ ^bool ReadXmlFile(string& szFileName)
* T+ u* J8 s: R* y% Z{//读取Xml文件,并遍历
& a2 z4 ?/ q0 \5 W- Y; I* |& A try- E* c$ P& p$ c! c0 Y
{% m9 T$ |. B+ s; }
CString appPath = GetAppPath();0 z; H" w, _! R+ u
string seperator = "\\";" ?, ^; [% ?6 U; h5 k( A* c# i
string fullPath = appPath.GetBuffer(0) +seperator+szFileName;' I+ X" }. q+ X' p% K" g/ u
//创建一个XML的文档对象。9 ~! G; B+ C8 ?! h0 p
TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());0 G6 ]( Y' N) }6 {0 ^
myDocument->LoadFile();
. k b# Z. l7 U4 ~3 l ^6 B' B //获得根元素,即Persons。
7 }1 ]$ l5 ]* {% `( f. Y TiXmlElement *RootElement = myDocument->RootElement();7 V; v. f2 g* x- ~2 f0 v! Z
//输出根元素名称,即输出Persons。6 m- Y& [: l3 |6 J4 C: n
cout << RootElement->Value() << endl;
. a2 [6 ~1 e5 n! U! i //获得第一个Person节点。
4 q5 ~ @9 f/ {. i4 I7 K& e TiXmlElement *FirstPerson = RootElement->FirstChildElement();
! t. |- n7 b: y# L7 o. h //获得第一个Person的name节点和age节点和ID属性。% @0 ?. A# W6 C& x2 N
TiXmlElement *NameElement = FirstPerson->FirstChildElement();9 \) k# ?, X5 ?8 R1 n# d6 h3 g2 ~
TiXmlElement *AgeElement = NameElement->NextSiblingElement();
2 G8 p ^! s. W0 m$ W4 Y TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();6 T) O) T; E* J- ^% b
//输出第一个Person的name内容,即周星星;age内容,即;ID属性,即。# L( d+ M! H m' _, D
cout << NameElement->FirstChild()->Value() << endl;
! x& `! C' r+ ]: N cout << AgeElement->FirstChild()->Value() << endl;) y% D/ g9 G2 m9 G
cout << IDAttribute->Value()<< endl;
1 @' s1 U7 R7 E* | }
9 X4 Q, K. O8 G* B catch (string& e)- _, i0 j r. ], K6 F1 ?! Z. t
{. |2 H0 ]! j q! {# d
return false;
# \ i' t, t* {5 {* x9 H! s }3 d. F& O0 n. F' N4 [$ M( X5 C$ I
return true;
' t5 a: G( p; y! z9 a0 b' z$ }}6 g6 K, ]6 B X1 X
int main()
: a% `$ V& c D0 e{
6 x4 |$ [0 n! _ string fileName = "info.xml";: {6 x: b$ H% g' ~
CreateXmlFile(fileName);
/ v% h$ I6 v% L6 G$ t ReadXmlFile(fileName);3 H7 Z }' Q) W7 d+ ]
}
- R, z) ^: [, { z% q- Z1 r2 g( z/ W) M: E8 b' B
! _% |2 N) u4 E( P
|