请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好。 TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。 DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。 如下是一个XML片段:" l* ?: b' O1 d7 m1 b8 Z+ k* y
<Persons>6 c" \3 \% v, S# |( Z9 a
<Person ID="1">
6 s% X4 x4 U+ d4 u2 {' _4 _7 N <name>周星星</name>
# \' Z7 S+ i1 @; w, z: @ <age>20</age>
4 i9 n$ y" T2 X' c </Person>7 H0 _1 _+ X1 o" t- I* `
<Person ID="2">6 d* H1 Z+ F3 |7 m2 O/ a7 ~8 K
<name>白晶晶</name>8 h4 }( \7 @# R! p9 A1 `" u% G
<age>18</age>( o7 n4 }, o( g+ R0 x. U; z
</Person>
. S+ N2 S! X- m- A! [ </Persons>
( d( R5 m: X8 P+ `+ Z
* S# M% w" e" f% d 在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文件:* @# }) _5 R- o0 \" R4 R
<Persons>8 ~1 ~, @7 l3 B! G
<Person ID="1">* M" n5 }, m& y8 E i
<name>phinecos</name>
5 K: B1 q' N1 c! | <age>22</age>! s6 s+ @, [9 u1 E
</Person>
9 u0 n- ?6 u* K1 Z* `' v8 |2 Z' i7 o</Persons>( X6 ^3 D4 H* [: R1 m& I7 n8 w
7 @2 N2 e3 }5 c7 d6 y H/ L0 v
读写XML文件的程序代码:9 g; e E- d5 d O
#include <iostream>! C3 L. F% b& v9 D6 }/ ?" K
#include "tinyxml.h"6 h: w$ m* C, D1 B0 [% h m
#include "tinystr.h"+ w& Q; {4 u( r/ j D
#include <string>
8 C6 Q: J% }2 X! l# u#include <windows.h>% h$ u& G: M0 g1 Y' r5 x( C
#include <atlstr.h>+ S `( ?! o2 T# H6 a2 ]2 y$ a
using namespace std;) _* v; U% n3 v) x1 s
( G& Z( g' _2 M ?5 f$ b8 s: QCString GetAppPath()
$ X1 x" v& y* K( [" E0 C5 P{//获取应用程序根目录
1 l. a0 h) t" H% V3 F TCHAR modulePath[MAX_PATH];
: c- _) `. G. F( t8 V GetModuleFileName(NULL, modulePath, MAX_PATH);9 a1 }& C5 I7 ~5 K) {
CString strModulePath(modulePath);
: N# m6 i/ t- B9 M, o) N strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));
3 u' k4 |1 \1 N, c/ u return strModulePath;
* Q$ [8 t, d6 j4 T, b# S}
* o5 P; U6 @6 f5 R O0 a2 n! L4 b7 E+ a! c3 Q
bool CreateXmlFile(string& szFileName)6 c5 n( y# L7 `0 {/ c$ ^
{//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false
0 P/ d. _& f2 ?+ V% O; t try5 g& L. g& D5 ~- S# t* g
{! x4 W: c7 G1 G4 N6 w7 k( j2 ]
//创建一个XML的文档对象。
4 R E; H9 D4 j# @ TiXmlDocument *myDocument = new TiXmlDocument();/ e1 o/ v& t5 {. v+ |9 k$ H0 `* J t
//创建一个根元素并连接。
4 n% s5 |5 Y1 b+ s! l U TiXmlElement *RootElement = new TiXmlElement("Persons");
& P. \$ q8 P# C& Z8 e myDocument->LinkEndChild(RootElement);. y$ E% I, r) @
//创建一个Person元素并连接。0 G* M6 a! z9 m. J
TiXmlElement *PersonElement = new TiXmlElement("Person");
4 z2 B) {* j' A RootElement->LinkEndChild(PersonElement);
( G) }9 \8 u3 {5 N8 A! f+ m //设置Person元素的属性。
+ e' E) N4 P W5 _+ z. t. u- A PersonElement->SetAttribute("ID", "1");) W3 j, |: l2 K( R. j
//创建name元素、age元素并连接。& ^2 V9 @3 ^$ p/ C/ U ~9 D1 J
TiXmlElement *NameElement = new TiXmlElement("name");
! Q& W1 H3 S% j! _6 r2 S! \ TiXmlElement *AgeElement = new TiXmlElement("age");7 g: D3 [; \) c- l
PersonElement->LinkEndChild(NameElement);
# p0 x7 z. W" W" t- @! ~1 w$ D" o# S PersonElement->LinkEndChild(AgeElement);
. a% V0 a6 z1 w5 U //设置name元素和age元素的内容并连接。4 a: @" ~5 A1 o6 J8 T
TiXmlText *NameContent = new TiXmlText("周星星");* a4 I+ L0 U3 V R
TiXmlText *AgeContent = new TiXmlText("22");
% L0 P9 t9 r7 A NameElement->LinkEndChild(NameContent);
: R6 y. L6 f5 N AgeElement->LinkEndChild(AgeContent);
0 B( S# S, s& v) Z2 S A1 ] CString appPath = GetAppPath();
$ B# B2 A8 i6 ~9 g! ^7 @$ E8 d string seperator = "\\";
+ ]5 [/ m7 R) C$ B' f string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
/ e% [: x! J* }# i9 {. I) h myDocument->SaveFile(fullPath.c_str());//保存到文件: j- p4 `$ M2 v& r7 Z$ N
0 E; |% d$ l6 s# b- j/ g6 w; [( }3 c& c 登录/注册后可看大图 }
; J4 A: O+ x1 p6 Q' P/ ` catch (string& e)8 h) \6 K( N. [3 S" t- n8 n
{
( h! c9 v! [* p) t! ~ return false;% K" k* l$ `% Q$ [/ r/ b
}( v( `0 L. ]' O3 }( N
return true;
) T8 p, e' o9 t. ]) o+ O# t- M}
' f6 Y1 ^: d0 p+ @' z! Z% y4 o. ~1 ?$ U( f
bool ReadXmlFile(string& szFileName) _3 `4 F l" ^7 `" r* H
{//读取Xml文件,并遍历7 A& Y* T0 r* p# ?9 h7 r0 A
try
; b9 c1 E$ B9 J% j B6 x1 }! P. ^ {
4 J& ]( P& s8 H2 q$ Q2 N- S CString appPath = GetAppPath();' C5 D" a: D- ~' M9 E
string seperator = "\\";
b* c+ ?) @0 q" u+ H string fullPath = appPath.GetBuffer(0) +seperator+szFileName;0 q4 |6 Q9 J+ @0 J2 R5 M2 d9 R
//创建一个XML的文档对象。
8 k. i( T1 ?8 u2 {/ P) j TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());( m& J# p) O9 L( _
myDocument->LoadFile();6 v* `" |! ~4 J, X7 F
//获得根元素,即Persons。
# ~1 k/ w8 M4 v7 c; p( I6 o TiXmlElement *RootElement = myDocument->RootElement();8 P) s& S# @$ S7 D5 \
//输出根元素名称,即输出Persons。
% b" D( R7 _1 X( x9 ? cout << RootElement->Value() << endl;
' l _! r3 p# |8 z/ f2 R //获得第一个Person节点。: O& K- U5 t0 n6 v {
TiXmlElement *FirstPerson = RootElement->FirstChildElement();
( u) T5 T1 a/ @6 O _. a/ ` //获得第一个Person的name节点和age节点和ID属性。
5 l, |) O( E C7 a U- K/ R+ J4 d$ j TiXmlElement *NameElement = FirstPerson->FirstChildElement();
6 _ q8 R5 M' k0 j5 G# q TiXmlElement *AgeElement = NameElement->NextSiblingElement();
( { h( y' Y, d4 p1 N TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();
: I' ?- {8 k0 I //输出第一个Person的name内容,即周星星;age内容,即;ID属性,即。+ u) J2 z4 e- q, {2 y) N+ z
cout << NameElement->FirstChild()->Value() << endl;
9 c6 ?) Q7 W, w cout << AgeElement->FirstChild()->Value() << endl;
1 {3 b; U- J8 I Y6 {8 ^2 O V+ |: S. h cout << IDAttribute->Value()<< endl;
' |5 `" y; ^8 U+ N8 c7 G }' d$ U) L0 F$ Y/ m
catch (string& e)
- U* h# a/ M. Q: ~ {. B2 j8 B) i! F' y2 s5 s
return false;
% k5 ]+ t0 |# }, d& d }
- n0 d3 ^* C# N- Z return true;. ~. r- o! ?. |* [9 E8 c" Z
}0 w9 L; l1 R2 P/ `1 y; @3 [7 i6 r$ C- a; h
int main()7 o" f8 V4 d* q) |
{9 u7 b' T+ o! B3 d' R+ f
string fileName = "info.xml";2 s8 N( B8 V! L( n" T& B) ^( u$ u
CreateXmlFile(fileName);
' g" \3 D' Y) @9 N0 K; Q ReadXmlFile(fileName);1 L: b" |9 b+ N( \0 U2 ~, J# @
}
- U$ T+ b* @1 i+ ?( i- ?
: P8 s, @% `# e7 M8 K% R0 y' @4 _1 e. ?& n- Z& b% J+ j+ D
|