请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好。 TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。 DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。 如下是一个XML片段:
/ F0 s+ k3 i& M <Persons>
0 h2 K9 h3 M' H3 g5 v/ P <Person ID="1">) C; h: H5 X' h0 ~. A9 ?# y
<name>周星星</name>
: Z5 Y8 K: m+ r9 a" r' Z <age>20</age>
8 S, Y4 c5 ?, S' m+ j </Person>
7 Q$ ]; \. i# `. A5 p <Person ID="2">/ y3 n* `! v) Q$ t& D
<name>白晶晶</name>
8 t. o& P, c, a8 h+ z/ a <age>18</age>
- [. L, U2 O6 p* T+ Z </Person>& R2 D+ i# s. ?5 L# K3 [
</Persons>, N% I6 f( {) q( l
& I% p: H9 f g) w6 Y 在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文件:
- O$ S' M+ z0 f( ?; {<Persons>7 q8 R! Z6 x2 C- G: B! D
<Person ID="1">
% F: }; j9 o+ b8 ~& ^ <name>phinecos</name>7 J p) y3 d0 s- w0 y/ I9 z( e
<age>22</age>
6 U. t4 l% q; M+ v& a" v- ~! F: @ </Person>
. `7 B2 S! }: F# p; `" B: m</Persons>
# E8 k1 F" U; o
, n" a5 {; T, k读写XML文件的程序代码:
' F- k% w' g0 V7 g2 N #include <iostream>
9 Y. V( A P1 \, x4 o: S# m( A& K#include "tinyxml.h"3 Z/ d. g2 j X% W6 n l
#include "tinystr.h"
$ X. |9 J# ?2 _0 F#include <string>( a$ C! g$ G$ _5 |) S L4 R
#include <windows.h>! Z3 E* q) M) {+ c ?. a
#include <atlstr.h>4 U& k8 _' x+ P e
using namespace std;6 c0 ^+ l8 m& x/ [9 p
0 {: D1 x1 e, I9 W8 s' pCString GetAppPath()
! v; D& X% [" i2 S. Q) n( D* \{//获取应用程序根目录7 T( ^6 R* t# Q& B7 }- O2 S0 ^" F
TCHAR modulePath[MAX_PATH];( s+ N% Z1 b) q9 h
GetModuleFileName(NULL, modulePath, MAX_PATH);
s7 z% x* M) I( A7 h, a CString strModulePath(modulePath);% |& [9 T2 Z$ x- O/ O% N x* \
strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));2 a0 m% {4 k, {9 M. G6 ?
return strModulePath;& p; r- w) I: m1 e7 v' N! v7 x
}- c5 u$ D2 U0 T2 o( |& p- w
* [0 R, {0 w8 m7 M5 w8 nbool CreateXmlFile(string& szFileName). R( b; {4 A0 a6 u' k% L
{//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false
! P: a$ O R9 o: m+ ^ try3 p# B$ W: e! J% z" Z' D/ D
{9 ~7 \' v; c4 r
//创建一个XML的文档对象。
# M; G# n* H$ U$ P7 ?) c# E TiXmlDocument *myDocument = new TiXmlDocument();, }! W; ], \# d! s% D! j/ R
//创建一个根元素并连接。
! j' g& K3 `* o) x2 v8 ` TiXmlElement *RootElement = new TiXmlElement("Persons");" T# e# j7 y& {2 |/ p
myDocument->LinkEndChild(RootElement);
% t0 }0 L5 F" m& Y! ` //创建一个Person元素并连接。
8 t z& X* C0 P TiXmlElement *PersonElement = new TiXmlElement("Person");- I5 o; X2 q( R3 O q2 `+ K5 k
RootElement->LinkEndChild(PersonElement);
. _9 ~. H3 D# A: l$ I //设置Person元素的属性。
! ?5 x: l. s6 y0 N/ x PersonElement->SetAttribute("ID", "1");
* g8 z* e$ t& ~! C: \% T //创建name元素、age元素并连接。
* P8 i, k8 B9 u, V7 H TiXmlElement *NameElement = new TiXmlElement("name");
# N) O$ {5 `8 w! n1 P8 {# ` TiXmlElement *AgeElement = new TiXmlElement("age");. t4 }9 i2 _, f& H
PersonElement->LinkEndChild(NameElement);7 \/ a3 b) v; d
PersonElement->LinkEndChild(AgeElement);7 Y/ w* |( |( o7 D9 R5 |: v
//设置name元素和age元素的内容并连接。( y0 t1 }! r5 l; K6 c- e
TiXmlText *NameContent = new TiXmlText("周星星");
2 r# x. G' f1 ^# R3 g2 }' S TiXmlText *AgeContent = new TiXmlText("22");7 Q3 `" L6 d+ c% W3 H! _- M
NameElement->LinkEndChild(NameContent);
; N: h5 z4 p" f" G O6 u8 v0 [ AgeElement->LinkEndChild(AgeContent);
) |# L6 s/ n6 ] j7 i; M CString appPath = GetAppPath();7 Y, [4 S, b7 H" K; W% P
string seperator = "\\";
/ M% z& e' _. x; U# P5 b8 U/ q' ^+ m string fullPath = appPath.GetBuffer(0) +seperator+szFileName;8 Z; G/ x9 H- v8 I' D7 w- C$ h- P
myDocument->SaveFile(fullPath.c_str());//保存到文件! V7 J& D/ }9 E3 ?- R9 ?
}
6 ^" s, h' v8 ^- F! q8 r catch (string& e)% d5 A0 u$ d! q2 G( R7 R5 {
{
1 W* g6 h9 @: \; `) N$ M return false;/ o4 }3 y! T; e# ]
}
$ N) t. ~" X- m' o0 m* E return true;
, `0 S. `, Z9 T n3 c5 V( Q1 R}( L3 K1 C8 A( z# g6 q3 Q
: @6 M* t0 x m) b# Y* d) S- |2 i) Jbool ReadXmlFile(string& szFileName)% V N. J! m4 \! u( [
{//读取Xml文件,并遍历0 B, q B5 J# _# T
try
+ `, Q& X ~% S |. V {
& o3 j# z1 C' X, h CString appPath = GetAppPath();
& _, g* A. ? k& p, z1 X: R5 ] string seperator = "\\";% S1 @0 K ? R3 v
string fullPath = appPath.GetBuffer(0) +seperator+szFileName;/ Z# S1 x* Q% v
//创建一个XML的文档对象。2 C0 ]( g6 C2 k! y9 k% T
TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());
2 Y, Z: p2 f; H: _- u' F" I myDocument->LoadFile();0 z5 p5 N9 D1 _4 L. N" D b
//获得根元素,即Persons。
2 s! e) \( M: f+ B; ?2 ]; v) m TiXmlElement *RootElement = myDocument->RootElement();, M; `6 Q. g# l$ j
//输出根元素名称,即输出Persons。
/ E( `' c' ~ ^ cout << RootElement->Value() << endl;
; I2 u$ J( o' C E, |- z+ y* j! \, q //获得第一个Person节点。
( x K7 }) z- ~ TiXmlElement *FirstPerson = RootElement->FirstChildElement();( N5 d* z2 E- v. O; L) L$ Q- v
//获得第一个Person的name节点和age节点和ID属性。
, y2 q3 i! {- s8 o; C( A F# q) e TiXmlElement *NameElement = FirstPerson->FirstChildElement();% ^0 J+ o1 a4 L8 l7 f8 u% \
TiXmlElement *AgeElement = NameElement->NextSiblingElement();
/ k' @7 w4 d9 V' z3 b5 q' s TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();6 }) |# Z3 v% v) z4 Y
//输出第一个Person的name内容,即周星星;age内容,即;ID属性,即。
8 H* T/ P4 k3 G" `" x0 \% `$ g; Q0 k$ l$ C cout << NameElement->FirstChild()->Value() << endl;
1 b3 r' w2 {7 ^ X G: Q' E8 T cout << AgeElement->FirstChild()->Value() << endl;
. b! |* u0 D( E! G7 i' S cout << IDAttribute->Value()<< endl;
# A% F1 y' M# o2 c9 R1 Y }
7 b, e3 T9 ? ~$ |4 U7 e% h catch (string& e)
2 d1 @" m7 C' _6 a; K8 _! ?4 { {
- p/ C9 M \* k return false;' l- {6 F* L, @3 o# Y# A
}
8 |8 s9 ?4 f" I. }; a3 U return true; O" s8 F( Y! m
}
/ q: G/ K/ _4 P' I/ lint main()
* m- L9 f$ l, g0 W1 V2 H) ?& _{3 {7 n) R$ O4 j
string fileName = "info.xml";! U. C) K4 i6 |4 u. {
CreateXmlFile(fileName);) J. q5 X( `; Z2 Y6 P4 O
ReadXmlFile(fileName);# o8 P/ j, N8 D# i) J
}* j# H. I/ n" K
' f' W8 R. I. _2 O6 U- o3 k
1 s3 f: F4 ?# e |