请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好。 TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。 DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。 如下是一个XML片段:
4 M( w+ j( p- k; D$ p <Persons>
& [" [* v8 Q3 W# J6 N$ q <Person ID="1">4 y1 {- O |4 W
<name>周星星</name>1 D1 J. f; z$ W. [# @# y" Q6 \: f
<age>20</age>
- Y. A u: B O0 D2 [( g </Person>
' x4 P$ X- ]& T$ t; `) m4 S <Person ID="2">
* ^! }4 N2 X1 O$ L9 `7 d2 c& G" r <name>白晶晶</name>2 ^5 E2 c d* t# b, h
<age>18</age>
9 X6 Z$ ]) E( G' }. h6 g </Person>) l' I3 e0 s; F. r
</Persons>7 t$ H" E" ~6 }) k" P1 X; v
" }2 \- E% v ~0 O. C, A I
在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* v, s6 n( k; o<Persons>' Y8 q P4 m$ B4 Y. ?! t: }2 ]# I
<Person ID="1">+ c0 W* w: j) B) N) C
<name>phinecos</name>: `# b7 R( d* t8 w" R
<age>22</age>
: j5 S4 }! a1 u* |) b# u </Person>
( @' i3 i% \9 s) b$ q) c& m</Persons>
' l. w' s) L+ e$ `
9 S; Q5 F7 j# L+ i" t" z6 `! c4 |4 ~4 ?读写XML文件的程序代码:9 H: z+ `( }. a/ w
#include <iostream>
/ y( b, n6 h2 l( v8 q p#include "tinyxml.h"
. z R$ h9 J" C4 I) g4 N, J#include "tinystr.h"
- S" q* O" h: L0 `5 l#include <string>; l3 Q0 a1 Q8 ?" G
#include <windows.h>, | l6 x- C: v v o
#include <atlstr.h>: R! M7 v" l5 b/ D+ v( G+ { Y8 j
using namespace std;1 o E! A- X6 l1 d
6 G; P( X z1 |. O4 s
CString GetAppPath()% s: C8 R4 P. C
{//获取应用程序根目录& \1 g% \5 ~0 u. v# t
TCHAR modulePath[MAX_PATH];% q# H# d; I- w' ~
GetModuleFileName(NULL, modulePath, MAX_PATH);
. J% I0 |; l! y0 R$ ?3 X& t/ m CString strModulePath(modulePath);
" z4 H C; l* Y6 ^2 W9 K strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));
) Q8 W/ x. T$ d5 n5 B. e2 x return strModulePath;8 v; V7 y( y$ K: h* ]: \
}
5 s. o) V- k* @% J* {
( O) j2 ^" z: w5 c& n0 {bool CreateXmlFile(string& szFileName)
+ [$ g% h5 l) V+ r{//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false
( n3 T8 o+ ?6 I/ d% k. j: C. X) n try9 K. f1 u% y7 d( t. l
{% {' e9 U3 {0 d F$ d/ D
//创建一个XML的文档对象。
3 Y9 {& ^+ [. R TiXmlDocument *myDocument = new TiXmlDocument();: I0 g" l- z! u, M) k& b' ?
//创建一个根元素并连接。* |$ u7 o# C6 W5 t n
TiXmlElement *RootElement = new TiXmlElement("Persons");
" t$ M6 |8 o' K8 L myDocument->LinkEndChild(RootElement);' d% J1 K& r5 W! G1 h4 c
//创建一个Person元素并连接。
; D" z- X* y& n4 X TiXmlElement *PersonElement = new TiXmlElement("Person");" I, Y; [9 K7 G* b
RootElement->LinkEndChild(PersonElement);1 M" L5 W# Q2 f* y; \: Z1 y
//设置Person元素的属性。# r6 Y4 K% {# Q3 k/ E }
PersonElement->SetAttribute("ID", "1");
k! r2 |# v3 k9 S2 Q. d. U+ S) q //创建name元素、age元素并连接。" k9 C+ O9 U& o+ Q# ^5 k1 n
TiXmlElement *NameElement = new TiXmlElement("name");
( o5 Z* o. l7 a& n/ U TiXmlElement *AgeElement = new TiXmlElement("age");, q Z7 w* [( t+ h. s2 H$ p, L
PersonElement->LinkEndChild(NameElement);: U2 ]7 D5 G/ K z: ~
PersonElement->LinkEndChild(AgeElement);
. s |1 C- m8 j" r- @: C //设置name元素和age元素的内容并连接。; Z9 d6 B8 j& n5 G- a
TiXmlText *NameContent = new TiXmlText("周星星");
3 ?9 ^9 C' j2 @2 H3 M TiXmlText *AgeContent = new TiXmlText("22");
, {. `; ]- C% ^$ ?1 W NameElement->LinkEndChild(NameContent);
2 z& A, l) O; \ AgeElement->LinkEndChild(AgeContent);- u9 i5 ]0 [' W, x+ x. R. A# G
CString appPath = GetAppPath(); C# s0 ]8 C8 V8 E
string seperator = "\\";. H$ P. X" t: @; i. c
string fullPath = appPath.GetBuffer(0) +seperator+szFileName;0 X/ D d$ u7 \9 k( m' B
myDocument->SaveFile(fullPath.c_str());//保存到文件
% }3 }) ]% W y }
- h7 w" {5 f7 _$ |' W1 n" H5 W) P catch (string& e)! v! o. k+ U" G! ?* I, V/ A
{" F" F9 s3 c: x+ c; j
return false;0 c) j& W* @- u: v
}* h0 f: B v8 v. G1 r. \$ I
return true;
% s& l' }# N2 {9 c% ]: M}
! g H4 [2 C' c7 P0 O; C5 e' }# A E3 F$ \" }6 S) ?( Q
bool ReadXmlFile(string& szFileName)2 K) X7 U. i& @6 ^
{//读取Xml文件,并遍历
) t2 V( n$ j( X* R) \ try
2 h5 b) a9 ~: G; O9 M+ m: T, R7 f {
* Z4 B2 e, Q S$ Q4 B0 C F CString appPath = GetAppPath();
- f) \ `. O6 X% o1 u- L string seperator = "\\";
$ ]/ {5 E W; V( m$ ]* r string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
- \3 M- |4 U- ?+ h //创建一个XML的文档对象。, j3 [6 ^) O; N
TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());5 f0 E+ }) d j9 l$ P' D
myDocument->LoadFile();
0 E+ }5 D v5 ]6 o( M //获得根元素,即Persons。
% @+ h z c1 X2 _) }5 j$ e TiXmlElement *RootElement = myDocument->RootElement();
3 `& b6 e# o3 Q3 J7 Q* x! L/ e //输出根元素名称,即输出Persons。! Y' l2 K; G! k" s
cout << RootElement->Value() << endl;
3 K! g" O7 y9 v7 T //获得第一个Person节点。
& t1 @) P$ _5 A. y8 o TiXmlElement *FirstPerson = RootElement->FirstChildElement();, U" N7 Z) s W- U! s z
//获得第一个Person的name节点和age节点和ID属性。( y* `. t7 c( \# c
TiXmlElement *NameElement = FirstPerson->FirstChildElement();
3 z" ], g3 k! B4 x3 e, g3 x TiXmlElement *AgeElement = NameElement->NextSiblingElement();) u" J0 c" X3 _/ f$ [" h& k
TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();5 ]. j" U4 v$ V
//输出第一个Person的name内容,即周星星;age内容,即;ID属性,即。* X. d( w* ~4 t0 m1 S0 J/ S
cout << NameElement->FirstChild()->Value() << endl;8 N! H2 L8 M: |
cout << AgeElement->FirstChild()->Value() << endl;
2 G3 z' d" ~) r( d cout << IDAttribute->Value()<< endl;
2 ~% t+ D7 F% i: t }
+ V2 B( }+ ~! T/ K$ Z catch (string& e)
6 w" C& I, a! G' R5 @/ l2 ?9 p {6 i# T2 f- I3 v4 O2 M6 z
return false;
f) W! ?1 e4 c6 s6 Z9 ]% D }- y# }9 z% i7 [, X8 F$ R
return true;
9 `( ] v. C4 V( a. s}9 z! X6 z0 y. T! |
int main()
( Z2 P" k6 L7 b3 R9 N6 T{
, _. _' n2 x, l/ Y9 k6 Z% S string fileName = "info.xml";2 u/ w o3 Y' t" L. G- t, @6 C; @
CreateXmlFile(fileName);7 z# s, n, m9 l/ F o, v
ReadXmlFile(fileName);& d H( K" h) d. N5 Y5 K/ v
}
- i/ y& _2 ~4 y
) t. z- W$ l0 q- \0 M! S0 O7 [5 a$ b/ s f# u; K
|