Linux 下读XML 的类详解及实现代码
Linux 下读XML 的类详解及实现代码
在Linux下写程序,常需要读一些配置文件。现有的XML工具很多,可以方便的编辑和生成XML。
但VC中用的XML解析器在Linux下不能用。只好自已写了个。用了一下,还不错。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
|
#include <stdio.h>
#include <stdlib.h>
// ********************************************************************** //
// XML解析类(honghaier写于2008-11-19)
// ********************************************************************** //
struct SXMLAttrib
{
char mKeyName[100]; //键名
char mValue[100]; //键值
}
;
struct SXMLFrame
{
public :
char mFrameName[100]; //帧名
int mAttrNum; //属性数量
SXMLAttrib* mAttrArray; //属性数组
SXMLFrame* mpSiblFrame; //兄弟结点
SXMLFrame* mpChiFrame; //子结点
SXMLFrame* mpParentFrame; //父结点
public :
SXMLFrame();
~SXMLFrame();
void Release_Depath();
SXMLFrame* GetFrame_Depth( char *szFrameName);
int GetChildNum();
SXMLFrame* GetChildFrame( int Index);
SXMLFrame* GetChildFrame( char *szFrameName);
SXMLFrame* GetSiblFrame();
SXMLFrame* GetParentFrame();
SXMLAttrib* GetAttrib( char *szKeyName);
bool ParseAttrString( char *szXMLString);
}
;
class CXMLFile
{
SXMLFrame mRoot;
SXMLFrame* mpCurrentFrame;
bool mbDepthClose; //闭合
private :
bool ParseFrameString( char *szXMLString);
public :
int pFile;
CXMLFile();
~CXMLFile();
void Close();
void Release();
bool Open( const char * pFileName);
SXMLFrame* GetRoot();
SXMLFrame* GetFrame_Depth( char *szFrameName);
}
;
//====================================================
SXMLFrame::SXMLFrame()
{
memset (mFrameName,0, sizeof (mFrameName));
mAttrNum = 0;
mAttrArray = NULL;
mpSiblFrame = NULL;
mpChiFrame = NULL;
mpParentFrame = NULL;
}
SXMLFrame::~SXMLFrame()
{
Release_Depath();
}
void SXMLFrame::Release_Depath()
{
if (mAttrNum > 0)
{
if (mAttrArray)
{
delete [] mAttrArray;
mAttrArray = NULL;
}
mAttrNum = 0;
}
if (mpChiFrame)
{
mpChiFrame->Release_Depath();
delete mpChiFrame;
mpChiFrame = NULL;
}
if (mpSiblFrame)
{
mpSiblFrame->Release_Depath();
delete mpSiblFrame;
mpSiblFrame = NULL;
}
}
SXMLFrame* SXMLFrame::GetFrame_Depth( char *szFrameName)
{
if ( strcmp (mFrameName,szFrameName)==0)
{
return this ;
}
if (mpChiFrame)
{
SXMLFrame* tResFrame = mpChiFrame->GetFrame_Depth(szFrameName);
if (tResFrame) return tResFrame;
}
if (mpSiblFrame)
{
SXMLFrame* tResFrame = mpSiblFrame->GetFrame_Depth(szFrameName);
if (tResFrame) return tResFrame;
}
return NULL;
}
int SXMLFrame::GetChildNum()
{
int count = 0;
for (SXMLFrame *temp = mpChiFrame ; temp != NULL ;temp = temp->mpSiblFrame)
{
count++;
}
return count;
}
SXMLFrame* SXMLFrame::GetChildFrame( int Index)
{
int count = 0;
for (SXMLFrame *temp = mpChiFrame ; temp != NULL ;temp = temp->mpSiblFrame)
{
if (count == Index) return temp;
count++;
}
return NULL;
}
SXMLFrame* SXMLFrame::GetChildFrame( char *szFrameName)
{
for (SXMLFrame *temp = mpChiFrame ; temp != NULL ;temp = temp->mpSiblFrame)
{
if ( strcmp (temp->mFrameName,szFrameName)==0)
{
return temp;
}
}
return NULL;
}
SXMLFrame* SXMLFrame::GetSiblFrame()
{
return mpSiblFrame;
}
SXMLFrame* SXMLFrame::GetParentFrame()
{
return mpParentFrame;
}
SXMLAttrib* SXMLFrame::GetAttrib( char *szKeyName)
{
for ( int i = 0 ; i < mAttrNum ; i++)
{
if ( strcmp (mAttrArray[i].mKeyName,szKeyName)==0)
{
return &mAttrArray[i];
}
}
return NULL;
}
bool SXMLFrame::ParseAttrString( char *szXMLString)
{
SXMLAttrib AttribArray[100];
int len = strlen (szXMLString);
mAttrNum = 0;
int StrPos = 0;
bool HaveFrameName = false ;
for ( int i = 0 ;i < len ; i++)
{
if (i==(len-1))
{
if ( false == HaveFrameName)
{
memcpy (mFrameName,szXMLString,len);
mFrameName[len]= '/0' ;
HaveFrameName = true ;
}
else
{
if (( len - StrPos-1 )== 0)
{
memset (AttribArray[mAttrNum].mValue,0, sizeof (AttribArray[mAttrNum].mValue));
}
else
{
memcpy (AttribArray[mAttrNum].mValue,szXMLString+StrPos,len-StrPos-1);
AttribArray[mAttrNum].mValue[len-StrPos-1]= '/0' ;
}
mAttrNum++;
StrPos = 0;
}
break ;
}
if (szXMLString[i] == ' ' &&szXMLString[i-1] == ' ' )
{
StrPos = i+1;
continue ;
}
if (szXMLString[i] == ' ' )
{
if ( false == HaveFrameName)
{
memcpy (mFrameName,szXMLString,i);
mFrameName[i]= '/0' ;
HaveFrameName = true ;
StrPos = i+1;
continue ;
}
else
{
if (( i - StrPos-1 )== 0)
{
memset (AttribArray[mAttrNum].mValue,0, sizeof (AttribArray[mAttrNum].mValue));
}
else
{
memcpy (AttribArray[mAttrNum].mValue,szXMLString+StrPos,i-StrPos-1);
AttribArray[mAttrNum].mValue[i-StrPos-1]= '/0' ;
}
mAttrNum++;
StrPos = i+1;
continue ;
}
}
if (szXMLString[i] == '=' )
{
memcpy (AttribArray[mAttrNum].mKeyName,szXMLString+StrPos,i-StrPos);
AttribArray[mAttrNum].mKeyName[i-StrPos]= '/0' ;
i++; //跳过一个"""
StrPos = i+1;
continue ;
}
}
mAttrArray = new SXMLAttrib[mAttrNum];
if (!mAttrArray) return false ;
memcpy (mAttrArray,AttribArray,mAttrNum* sizeof (SXMLAttrib));
return true ;
}
CXMLFile::CXMLFile()
{
pFile = 0;
mpCurrentFrame = NULL;
mbDepthClose = false ;
}
CXMLFile::~CXMLFile()
{
Close();
}
void CXMLFile::Close()
{
if ( pFile>0)
{
int error = close( pFile);
if ( error!=0)
{
perror ( "close file failed" );
} else
{
pFile=-1;
}
Release();
}
}
void CXMLFile::Release()
{
mRoot.Release_Depath();
}
bool CXMLFile::Open( const char * pFileName)
{
pFile =0;
pFile = open( pFileName,O_RDONLY);
if ( pFile==-1)
{
perror (pFileName);
return false ;
}
int num = 0;
char buffer;
bool bReadXMLString = false ;
int XMLStringNum = 0;
char XMLString[1024];
while (num = read(pFile,&buffer,1)>0)
{
if (buffer == '<' )
{
bReadXMLString = true ;
XMLStringNum = 0;
continue ;
}
if (buffer == '>' )
{
XMLString[XMLStringNum]= '/0' ;
if ( false == ParseFrameString(XMLString))
{
printf ( "Read XML error: %s /n" ,XMLString);
return false ;
}
bReadXMLString = false ;
continue ;
}
if ( true == bReadXMLString)
{
XMLString[XMLStringNum++] = buffer;
}
}
mpCurrentFrame = NULL;
mbDepthClose = true ;
return true ;
}
SXMLFrame* CXMLFile::GetRoot()
{
return &mRoot;
}
SXMLFrame* CXMLFile::GetFrame_Depth( char *szFrameName)
{
return mRoot.GetFrame_Depth(szFrameName);
}
bool CXMLFile::ParseFrameString( char *szXMLString)
{
if (szXMLString[0] == '?' ) return true ;
if (szXMLString[0] == '!' ) return true ;
if (szXMLString[0] == '/' )
{
//如果是结束
mpCurrentFrame = mpCurrentFrame->GetParentFrame();
mbDepthClose = true ;
}
else
{
mbDepthClose = false ;
if ( NULL == mpCurrentFrame)
{
mpCurrentFrame = &mRoot;
}
SXMLFrame* tNewFrame = new SXMLFrame;
tNewFrame->ParseAttrString(szXMLString);
if ( false == mbDepthClose)
{
tNewFrame->mpParentFrame = mpCurrentFrame;
if ( NULL == mpCurrentFrame->mpChiFrame)
{
mpCurrentFrame->mpChiFrame = tNewFrame;
}
else
{
for (SXMLFrame *temp = mpCurrentFrame->mpChiFrame ; temp != NULL ;temp = temp->mpSiblFrame)
{
if ( NULL == temp->mpSiblFrame)
{
temp->mpSiblFrame = tNewFrame;
break ;
}
}
}
mpCurrentFrame = tNewFrame;
}
else
{
tNewFrame->mpParentFrame = mpCurrentFrame->GetParentFrame();
mpCurrentFrame->mpSiblFrame = tNewFrame;
mpCurrentFrame = tNewFrame;
}
}
return true ;
}
|
用XML工具做了一个简单的XML文件。
1
2
3
4
5
6
7
8
9
|
<? xml version = "1.0" encoding = "UTF-8" ?>
<!-- edited with XML Explorer v2.0 by Mergesoft (www.mergesoft.com)-->
< root >
< Honghaier Name = "红孩儿" Age = "26" ></ Honghaier >
</ root >
|
在C++代码中
可以这样使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
CXMLFile xmlfile;
xmlfile.Open( "1.xml" );
SXMLFrame* mRootFrame = CXMLFile::GetRoot();
int ChildNum = mRootFrame ->GetChildNum();
for ( int i = 0 ; i < ChildNum ; i++)
{
SXMLFrame* tChileFrame = mRootFrame ->GetChildFrame (i);
SXMLAttrib* tAttrib = tChileFrame->GetAttrib( "Age" );
print( "%s : %s= %s /n" ,mChileFrame ->mFrameName,tAttrib->mKeyName,tAttrib->mValue);
}
|
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
本文由主机测评网发布,不代表主机测评网立场,转载联系作者并注明出处:https://zhuji.jb51.net/linux/5685.html