请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好。 TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。 DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。 如下是一个XML片段:/ |% Z$ q1 R; |
<Persons>: i, f- E/ M: F
<Person ID="1">
* F8 W, k. e5 e' Z- w <name>周星星</name>
) M8 H% H- n0 R ]0 m9 R2 |& L2 c <age>20</age>4 F, Y* }: l9 J8 h
</Person>: G; ^- V7 A8 N/ V0 L
<Person ID="2">
7 l/ _( G. J, `) j; t <name>白晶晶</name>
; g Q, @0 i* o" E- ^ <age>18</age>, E. F# ?. Z4 _( w t7 `7 G
</Person>3 Z( j ^% U+ x1 v7 k% N# W% }
</Persons>7 J. k5 U) y; j3 m6 o0 f
- G/ i; f2 j; Q: _ 在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 Q5 j( C; G, ?
<Persons>* B7 y; E9 s R
<Person ID="1">
- L* M" L" t9 R. D: ]6 c <name>phinecos</name>6 ^/ [( }3 f3 `1 _" | }+ X
<age>22</age>3 M/ E2 X5 n, g2 k* A7 x; c
</Person>7 i$ x( W+ n4 y1 N# K
</Persons>
* B$ W. z% Y( a0 c' R* Q% Z- t, R& H' L
读写XML文件的程序代码:% ~, a) `: u' i* D6 }8 {
#include <iostream>
; b4 `& [& h) [9 E& ?#include "tinyxml.h", f6 \* p% ^; G5 f+ z& D7 x
#include "tinystr.h"
! C. ]) O4 t% b6 Z$ c#include <string>
+ M2 A' z* ?; `0 g) O#include <windows.h>
; P0 V# b2 r# T$ v#include <atlstr.h>
# c5 `) p* a- M) y- y% husing namespace std;3 J& ^2 B; H) _( O' W
$ b+ X- U% O- d7 ?$ ]CString GetAppPath()
% M" l. P. ?8 ]; N6 U{//获取应用程序根目录
/ `* ?* K3 R% Q$ ~' L TCHAR modulePath[MAX_PATH];
3 n0 _3 a. \5 k$ n: U. l. L$ }+ N GetModuleFileName(NULL, modulePath, MAX_PATH);
- Q$ J$ S1 _* U CString strModulePath(modulePath);
1 V9 B4 u" L* K$ F1 A; T strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));* }6 R3 \* O4 O7 U7 r# v z
return strModulePath;8 ^2 z( t9 T7 N6 o6 `/ d
}. w' K$ q) E# W3 F4 s# q7 q! x
0 `3 o7 _: a* o2 m9 qbool CreateXmlFile(string& szFileName): b3 n( N" l; t% h3 V4 R
{//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false3 n$ E2 [3 o1 Z5 b
try
1 L( P, W V( u; Z! g5 ` {) o4 t' S5 l( d. d/ t+ J' X* b
//创建一个XML的文档对象。
/ r# [* D# Q4 C. ]# M8 u9 L3 B; Y9 C TiXmlDocument *myDocument = new TiXmlDocument();% P, \: W4 D- f3 ]- |
//创建一个根元素并连接。
( r6 Q" Y8 \6 t+ [2 C9 w: V. f5 b! L TiXmlElement *RootElement = new TiXmlElement("Persons");; V/ f% c* U4 H& D9 j6 P$ h
myDocument->LinkEndChild(RootElement);
' D4 u) Z. U2 Z' I //创建一个Person元素并连接。% m# r2 D% s8 G# [5 c7 ?: O& R
TiXmlElement *PersonElement = new TiXmlElement("Person");
( N% E; M# \9 A K2 k7 P' O RootElement->LinkEndChild(PersonElement);
/ M3 d! |. A4 _ g$ I //设置Person元素的属性。- |% Z: l' f0 g. v: u
PersonElement->SetAttribute("ID", "1");& q' C4 D$ x. }
//创建name元素、age元素并连接。
; E+ @1 P* y8 K9 r5 g TiXmlElement *NameElement = new TiXmlElement("name");" ^ V! A: D( z6 [7 i
TiXmlElement *AgeElement = new TiXmlElement("age");% J- K% f! n7 V7 k
PersonElement->LinkEndChild(NameElement);! L3 e0 V* _+ g- \: z
PersonElement->LinkEndChild(AgeElement);- h9 s B; \5 V$ Z( K0 F: p
//设置name元素和age元素的内容并连接。
; \) j5 f& ~! i3 h- v: ?5 c TiXmlText *NameContent = new TiXmlText("周星星");; H: Q4 @& p( ^6 E
TiXmlText *AgeContent = new TiXmlText("22");8 U# Q# g( q; E5 X- b. ?
NameElement->LinkEndChild(NameContent);3 r5 g- o3 \7 s- c" h; f) P' c
AgeElement->LinkEndChild(AgeContent);
2 W( ]8 S/ M( G- @* { CString appPath = GetAppPath();
0 y* y1 m! L9 o% x) ^ string seperator = "\\";
- M* A, O( C4 J6 U2 u string fullPath = appPath.GetBuffer(0) +seperator+szFileName;8 s) L3 P/ H) X3 [6 v/ ^* m: i
myDocument->SaveFile(fullPath.c_str());//保存到文件
& z1 c, ]! \# @0 ^& \9 X( d& [* N }% b% a2 w( e; ^0 h! D! F
catch (string& e)
! G. d! q' q* c0 i# O+ f ) {6 y# I8 ]3 w* q! q! ?7 u- l9 H0 A1 Z
登录/注册后可看大图 {0 B1 r1 c, v) t3 v! B. \0 D- S
return false; f }& ~7 P% I6 J
}
6 w# ?' a' i' f% O/ ~ return true;) A ~& i( {5 X
}
2 x# e: _* U4 C9 H8 i& S: E, @% l+ R* b5 F4 g# Z, t" L# U
bool ReadXmlFile(string& szFileName)2 I$ {9 h; |7 |5 R Q. }$ X6 T
{//读取Xml文件,并遍历2 V3 B1 {4 I4 [) \
try1 D# o/ W+ T' N7 K
{, ~2 S, S4 t' R
CString appPath = GetAppPath();
6 Q, _5 n/ p. H+ g5 o* e9 p string seperator = "\\";- ]/ {' o5 ?2 D2 p# ?" @' G1 u
string fullPath = appPath.GetBuffer(0) +seperator+szFileName;6 m+ o6 k4 N8 v* D2 ?
//创建一个XML的文档对象。
! }, _5 A& q/ I" A6 W* [' R# U TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());$ _( g5 E. R2 U% m7 W& T
myDocument->LoadFile();
$ P3 A0 e+ k+ U- l //获得根元素,即Persons。# m5 U" n6 e- a0 M$ x
TiXmlElement *RootElement = myDocument->RootElement();+ z; P: F# z A! P
//输出根元素名称,即输出Persons。
" b, T4 D. G& n cout << RootElement->Value() << endl;
' G! Y9 I) r/ F* h, t //获得第一个Person节点。
$ C. |9 F3 r% L" |9 h" T TiXmlElement *FirstPerson = RootElement->FirstChildElement();
/ \# B p* r3 l& H2 ?5 x' M5 R- V //获得第一个Person的name节点和age节点和ID属性。
9 [5 ?- ~. E0 v" r& Y" A3 L8 g9 c TiXmlElement *NameElement = FirstPerson->FirstChildElement();
) D" M8 l9 _6 D9 S7 p0 Y TiXmlElement *AgeElement = NameElement->NextSiblingElement();/ @- B0 L9 y' F: j; _$ A9 J* C
TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();
! \4 w( K! x! q$ Y) f2 p //输出第一个Person的name内容,即周星星;age内容,即;ID属性,即。% l' a5 Y5 O' e6 C( H
cout << NameElement->FirstChild()->Value() << endl;- [. m. W3 P/ ^3 J O* Z
cout << AgeElement->FirstChild()->Value() << endl;6 I# C! l( C) R8 A/ E3 W9 Y
cout << IDAttribute->Value()<< endl;' _7 _, ` _( Z5 u/ e
}
5 I' G7 g! M$ I5 v4 d3 e9 h catch (string& e)8 g5 o/ O7 T2 Q# m& i( {5 |8 N
{$ Z! J' T1 u# w& A
return false;
$ p, p/ k. Z5 A+ a }6 B* o$ m) @+ Q& g( X1 H
return true;
6 |# ^8 O# j' y}
1 f1 ~* v3 @" eint main()" n' s: \, c7 [
{
' B/ g1 Y! |$ Q0 S/ a7 q string fileName = "info.xml";5 y1 K+ v" E' B& k7 i( ~& ?! F2 l
CreateXmlFile(fileName);
% m* U% j- B; P! {# ]6 }5 P( v ReadXmlFile(fileName);
& E3 D! E$ k. p% a) d$ ^}
& l) P2 L/ z4 Q; E( [; b3 ?# E" ^6 O3 e6 m" x# k3 x1 l( Q
8 Q; c$ O9 }4 }4 [+ T o6 O8 ~ |