请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好。 TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。 DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。 如下是一个XML片段:
- g [/ H, S3 {9 o/ { <Persons>0 F, s Q5 X# F# e$ H; L
<Person ID="1">
5 t/ w. D* V& j1 j( ` <name>周星星</name>
7 D1 L0 U; e9 b4 N7 a <age>20</age> O, C0 q/ P r1 E) n# G: h
</Person>
?& A$ R0 k7 e) I$ L& v4 X <Person ID="2">
- H/ F# \/ g2 p: F7 A: D; E <name>白晶晶</name>* `+ i9 A" a, j% H2 g. H, G3 B9 f
<age>18</age>
3 @) ]( |7 [8 Y </Person>
) y6 ~% o- o0 q </Persons>
, ^& t. c+ v! Y) R
) {7 h6 \ J; M 在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文件:
( G" j- o: ^$ U9 C- Q<Persons>
% p _8 i+ g: |& v" i( p! e <Person ID="1">; A) Y, H- C8 N* m$ `# p
<name>phinecos</name>8 r ^ p( T, o
<age>22</age>
* e6 \" P2 e, i8 k </Person>
. w( [5 }, E. A- l</Persons>3 V& G8 l5 M0 b5 M5 Y
; B7 f2 c' d) Y2 [* _7 T读写XML文件的程序代码:
; L* `- z, A& X #include <iostream>
& t+ K$ h! ?. W o; o#include "tinyxml.h"
: d9 B# W y) w1 G" a#include "tinystr.h", T! g% I+ M } W. b' @
#include <string># O( A! @. V" T$ Q( j
#include <windows.h>
5 \, l) ?: ?4 k#include <atlstr.h>( ^/ y; }% V0 Y4 W8 u- h' r7 q
using namespace std;
0 U. e( v! E9 S6 d/ U: j
! g8 ~1 k( m6 E9 w4 l! R( `CString GetAppPath()
! z2 }/ B8 M" L1 E{//获取应用程序根目录
! [' ~& v( F* k( ], e# `, B TCHAR modulePath[MAX_PATH];8 @3 @7 ?- ?% Y' h' P
GetModuleFileName(NULL, modulePath, MAX_PATH);9 ]( o/ U+ a6 n% o2 k! n3 x& M
CString strModulePath(modulePath);
o: }' k$ l- e8 R$ o1 l strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));
7 Q% M6 N/ r W: g. v return strModulePath;
" O' A9 h$ N' M9 Y}6 {' N z! Q* y1 w5 o
! I$ n( F4 L" B2 u n# H5 fbool CreateXmlFile(string& szFileName)
& v( q2 G5 N2 E{//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false
- x* k3 J+ r9 n try7 k5 t+ Q8 y+ _$ X" F
{
& L( A1 s1 e2 ?. @* I //创建一个XML的文档对象。
% M$ F( s2 y& ~: l- F! L TiXmlDocument *myDocument = new TiXmlDocument();- Z' m3 J5 B# R$ C: q) m
//创建一个根元素并连接。& I, P4 l7 f r( _8 f2 K
TiXmlElement *RootElement = new TiXmlElement("Persons");5 u6 x) H$ _. r4 [
myDocument->LinkEndChild(RootElement);% h: F0 G1 _3 J' }
//创建一个Person元素并连接。
' p$ z$ h, H& u TiXmlElement *PersonElement = new TiXmlElement("Person");! Y7 V, |6 f; B% G" s
RootElement->LinkEndChild(PersonElement);( h0 {# y% @2 w" S& e3 C2 s
//设置Person元素的属性。
7 X0 V/ b. q7 b& j, g1 m PersonElement->SetAttribute("ID", "1");3 \* n9 c! L; C
//创建name元素、age元素并连接。
$ {/ j8 g ~. i+ _9 J . I5 x$ o" j$ ]" F' x& y! n' t# e$ I( W, D
登录/注册后可看大图 TiXmlElement *NameElement = new TiXmlElement("name");
" d# d1 I& o+ d$ U" L- y TiXmlElement *AgeElement = new TiXmlElement("age");
; e2 R! |1 e% h1 h+ _: ^/ \7 r; a/ [ J PersonElement->LinkEndChild(NameElement);
4 e2 W. X0 D: }; [ PersonElement->LinkEndChild(AgeElement);. O8 X( V% E3 i! F/ j
//设置name元素和age元素的内容并连接。
* p! V; J: K1 Z9 p TiXmlText *NameContent = new TiXmlText("周星星");
* h' Z8 A% a% A% z- ?1 O* O TiXmlText *AgeContent = new TiXmlText("22");
" L8 Z' I( v+ q0 W NameElement->LinkEndChild(NameContent);
5 t( a5 d; n+ q! ` AgeElement->LinkEndChild(AgeContent); d) Y0 J& V% \, X7 {0 T6 A6 _
CString appPath = GetAppPath();
$ ?. G# t7 X6 [: f4 i: z- G1 }4 R0 e string seperator = "\\";
+ r% I; D4 I2 r# L* _: C5 y string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
- ^: U& T0 S" V8 J1 B9 ]8 T+ }6 q myDocument->SaveFile(fullPath.c_str());//保存到文件- C9 n4 d9 S" E: d+ r# j+ X! f
}" U5 Q0 c4 E7 a+ W, v0 Q- H, e% q! _
catch (string& e)$ y$ C, C" {: d
{
$ R% z- g D+ j9 P8 ^ return false;1 k7 D+ ^, K. i( S$ j7 {5 z0 ^
}% |2 h% O3 g p3 h7 S1 s
return true;& l$ g5 X- {1 w* U3 f- p
}
3 u: o0 u1 j; Z# g4 R
9 j& \1 W/ a3 k: Q' }+ i! x+ w; _bool ReadXmlFile(string& szFileName)
2 H2 v$ E" \4 k% i4 d" |+ R5 D, I6 ?$ }{//读取Xml文件,并遍历
( a0 Z9 M9 s6 i8 Q try$ `( B$ W* A$ h+ F4 h3 K
{8 L4 x# ]' f8 v/ ?& q/ B& b' y
CString appPath = GetAppPath();
7 A; m, N2 n- s. H2 E8 R7 L string seperator = "\\";8 D7 K# ?) _1 b: |$ P
string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
: r* C0 b* Y7 d+ k! y/ ]% ^ //创建一个XML的文档对象。
, j& x2 H* @9 C6 \/ d, o8 q TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());
4 X8 y& a8 S6 @. Y myDocument->LoadFile();. R9 T" e1 d: V' |1 W, i% Z
//获得根元素,即Persons。
# \2 H* J6 x: a) t9 O TiXmlElement *RootElement = myDocument->RootElement(); q: R8 N: c) V7 ]
//输出根元素名称,即输出Persons。8 x" r( r1 p5 k4 z4 m( e
cout << RootElement->Value() << endl;, T! P! V) L' o# A! h3 M+ E
//获得第一个Person节点。
i+ q- p/ i& _9 I, j TiXmlElement *FirstPerson = RootElement->FirstChildElement();
4 J! b) P! n/ v( |4 Z3 Z5 { //获得第一个Person的name节点和age节点和ID属性。% J% u y+ c. z, H
TiXmlElement *NameElement = FirstPerson->FirstChildElement();8 g* E! }. y: J% R& A
TiXmlElement *AgeElement = NameElement->NextSiblingElement();4 X9 V, C- c8 s- Z+ R1 V
TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();
) H2 }) j0 l. A* Z+ ^ C //输出第一个Person的name内容,即周星星;age内容,即;ID属性,即。
" x! {/ K2 r I" b5 j5 y# j cout << NameElement->FirstChild()->Value() << endl;
# h' n& H4 v* x! Z' @- [ cout << AgeElement->FirstChild()->Value() << endl;2 A/ G( ^3 F% l/ {9 l0 D
cout << IDAttribute->Value()<< endl;
1 v( M+ C1 W$ W. H. G2 z! b# `+ N7 ~* c }
- R$ B- u# D6 v; y9 M catch (string& e)* J. u e y* G
{
7 D' z, B0 q+ H* f x) G return false;6 O% O, Q( b/ z# R6 v+ @( p
}/ c: Q! V$ C' K) U$ u4 B
return true;0 T: k4 Z+ G7 B0 I+ q7 t9 u
}& \* a$ v. j( r8 m/ [* a) q
int main()3 j' a$ F0 G9 }+ x3 q
{. J- A: o8 L6 a
string fileName = "info.xml";0 k R7 q% h2 W7 z1 f
CreateXmlFile(fileName);) R3 ^- K$ a- o. \( V
ReadXmlFile(fileName);
( i# [8 _: W. ^, m$ o}
/ D& T4 V& t% C( x, n( ]
& }2 O/ J" W9 ~) l1 q7 F s& D5 Q1 R8 R
|