请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好。 TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。 DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。 如下是一个XML片段:
; K& ~6 u. c- T <Persons>8 B. `1 x6 S4 [- U& f
<Person ID="1">
5 E; U2 v- D5 n5 w2 E# Z7 ]8 W <name>周星星</name>2 `+ o: M+ f$ e. \( i( T0 ?3 O
<age>20</age>
: o9 x1 P6 v& S* J5 ] </Person>
+ L" A! ~# n; ?* S <Person ID="2">
u% a ?# y; E$ e <name>白晶晶</name>
3 ]5 {" c, c7 N' y <age>18</age>
. W0 H% R0 @. O- u7 O F# v </Person>+ I: g% |: _. s* @. C1 Y' c
</Persons>
: P a$ b8 ~; }! j6 s& R% p* O7 L( Y) v1 y4 [
在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文件:
% l3 M4 ^. _+ I( d<Persons>5 ^% ]8 ~$ Z1 g% w
<Person ID="1">5 R/ k1 x. W! D, [: |
<name>phinecos</name>
7 H. |3 \/ p9 ]& g <age>22</age>, {. h$ w1 t {
</Person>
. ], S+ B' t# S+ r$ J% b6 H</Persons>
! Q0 e# ]8 n# k
% i S' J8 K, V) j) @7 ~7 n读写XML文件的程序代码:- h, }: h' D+ a% V) X
#include <iostream>
! R7 E- X4 w3 \#include "tinyxml.h"
9 c) t; |* \. D8 z#include "tinystr.h"8 ^6 ^1 B/ J7 G. p
#include <string>
Q: q, V- Q& c$ F2 K9 A z#include <windows.h>
3 M! n- U& n2 Y l6 p#include <atlstr.h># s+ }# D' v# c% A% N% Z
using namespace std;
1 ?! C( Q# h* j. Q, u4 V f1 R' D6 E7 a# z0 \ Q
CString GetAppPath()
0 m) p- P0 T" B: _" p w" U{//获取应用程序根目录
2 p4 M( e; n( n8 r+ V2 w TCHAR modulePath[MAX_PATH];
7 N+ {3 C* H$ S- i( d# C& Y GetModuleFileName(NULL, modulePath, MAX_PATH);
) b4 ~' H! a5 a3 M: j# W9 L CString strModulePath(modulePath);
9 F: ~7 b5 b5 E6 j2 _/ U1 d strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));4 E( G' ]1 T+ Y
return strModulePath;& `; b3 ?' K( ^& v
}3 @0 L* n3 {* D+ n/ H% K
& R0 q0 d: @' hbool CreateXmlFile(string& szFileName)( Y- A8 \* Q9 A# \/ t8 [
{//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false
5 v5 }# ^ X1 ^1 H; F% V" D$ w try3 s$ Y, r# z( r; b+ \# F& {3 n1 G% u
{+ R/ o* K5 l+ ]* M. [9 u
//创建一个XML的文档对象。
8 W7 S1 n5 M4 l$ i5 b/ Y, d TiXmlDocument *myDocument = new TiXmlDocument();
" [+ s4 Q& I4 i0 M //创建一个根元素并连接。
' Q8 R# L0 ]0 s I% ^ TiXmlElement *RootElement = new TiXmlElement("Persons");
( J3 n) s7 `" ?) A, `9 ]& x myDocument->LinkEndChild(RootElement);$ p1 w/ U0 K; t _. x. ]
//创建一个Person元素并连接。7 d2 Y9 l, ?7 D7 M7 G# [7 e
TiXmlElement *PersonElement = new TiXmlElement("Person");! `' |9 m4 _' w" Y% w
RootElement->LinkEndChild(PersonElement);$ }1 y, p) @; v E2 c# m
, m/ p. Z4 ]5 S( e9 `+ L( R, }) ?1 ]/ X
登录/注册后可看大图 //设置Person元素的属性。$ K- t, i) V1 |$ i+ I
PersonElement->SetAttribute("ID", "1");# `' \- P9 U( p4 P9 B$ \2 i
//创建name元素、age元素并连接。1 D9 W/ @+ F( r( z! C7 |) b+ a) F
TiXmlElement *NameElement = new TiXmlElement("name");& r0 n; T( y3 [, ~; H4 f5 b
TiXmlElement *AgeElement = new TiXmlElement("age");# w0 Y; X2 }2 Y1 Z# Q8 j
PersonElement->LinkEndChild(NameElement);/ \% e% K) ]$ _4 }/ i; _1 H
PersonElement->LinkEndChild(AgeElement);
- ^7 k+ U4 `6 Q/ G //设置name元素和age元素的内容并连接。( y/ W4 v9 f1 |/ f
TiXmlText *NameContent = new TiXmlText("周星星");
- a8 j# M! g2 f* ~8 ^# A3 P TiXmlText *AgeContent = new TiXmlText("22");
/ F6 l7 c( I E. q0 q5 ] NameElement->LinkEndChild(NameContent);7 ^# ~2 N% m3 V6 |+ N- K A1 X
AgeElement->LinkEndChild(AgeContent);# L% j3 {* Q, P
CString appPath = GetAppPath();
: ^0 F2 g% Z7 f/ ^! g string seperator = "\\";
9 z0 t$ n& q4 h9 S$ ^4 x! y string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
z+ m$ o2 s0 @ myDocument->SaveFile(fullPath.c_str());//保存到文件* ~( F5 \5 T4 b6 Y2 m
}9 f2 y, J. m6 |
catch (string& e)
4 b4 z: t% ]( Y {
+ a8 o9 E# v: |# @' I7 ~ return false;
; ?8 P. k, v, Q6 Z }; j( `$ b% J0 i/ @6 X2 R: F
return true;2 Z: S \+ J _: F
}
5 H3 h& r( h" o+ R0 B5 i; b2 u1 Y3 r% i6 E/ E
bool ReadXmlFile(string& szFileName); Z k. V, S& H
{//读取Xml文件,并遍历) s! S+ ?. x- |+ ]- k6 U3 s
try. s, B. V) {' T4 b
{4 t! g' x* E" R
CString appPath = GetAppPath();2 G6 u( I1 y4 E# \7 j& j+ g) F
string seperator = "\\";7 Y! z" i( q- I; _6 s
string fullPath = appPath.GetBuffer(0) +seperator+szFileName;' {" J" o) |% w/ i \! {
//创建一个XML的文档对象。$ I; E- P. z3 P. I+ {
TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());
* _6 Y0 j j$ E2 Q2 i; _( ~ n myDocument->LoadFile();7 \9 o* ~" q. U. x4 R3 W
//获得根元素,即Persons。; E% R4 l: c4 B6 F
TiXmlElement *RootElement = myDocument->RootElement();
: l/ O8 H1 H8 r% ]4 {& } //输出根元素名称,即输出Persons。
" @& [/ W3 s8 V: v cout << RootElement->Value() << endl;; E+ w6 U, J& M. x
//获得第一个Person节点。 E" N6 E3 K3 x- ]6 {
TiXmlElement *FirstPerson = RootElement->FirstChildElement();9 ]( c: q p* T6 R
//获得第一个Person的name节点和age节点和ID属性。
! t* I V. b" u" O TiXmlElement *NameElement = FirstPerson->FirstChildElement();/ y/ \' C# o5 X" w
TiXmlElement *AgeElement = NameElement->NextSiblingElement();7 D2 y2 D) h |
TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();* G4 a1 y W9 ^0 p2 J5 [
//输出第一个Person的name内容,即周星星;age内容,即;ID属性,即。; s% z% O3 [* s
cout << NameElement->FirstChild()->Value() << endl;
2 ~* Q: f: K) F6 D- [5 i cout << AgeElement->FirstChild()->Value() << endl;- s6 U* t0 s. j/ U' j
cout << IDAttribute->Value()<< endl;
! c) r3 ~, ?& }! P l/ c }8 u- o/ n, l4 B; j8 A9 B8 Q
catch (string& e)
" `4 m( M1 a. ?1 ]; |$ { {
9 E6 M4 f4 Y7 P% }6 }* _8 o- { return false;
( h& k) \7 m' H6 D ? }5 {2 P+ U! G& ]4 P# p
return true;
7 E' U/ w3 A# Y- ]+ H% _}
& e0 ^, a Z" _( U9 |int main()( y- _: J; }# f% P: y
{2 S/ `0 S( A) v" I
string fileName = "info.xml";2 O( u! s9 D0 Q0 W1 i3 z! o
CreateXmlFile(fileName);( i, R7 o# A5 q* z$ P9 L5 @
ReadXmlFile(fileName);
& c! b- K/ b- j$ R}
. G& F+ x, j' C! a( D4 E# ~3 B: q6 G/ @4 D8 B/ m/ u
5 G% b! T) n( ]. C0 E- \ Q% Y |