-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXMLParser.cs
executable file
·330 lines (306 loc) · 10.1 KB
/
XMLParser.cs
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
/*----------------------------------------------------------------
// Copyright (C) 2015 广州,Lucky Game
//
// 模块名:Xml 解析工具
// 创建者:D.S.Qiu
// 修改者列表:
// 创建日期:February 29 2016
// 模块描述:
//----------------------------------------------------------------*/
#if WP_BUILD
using Hashtable =MarkerMetro.Unity.WinLegacy.Plugin.Collections.Hashtable;
#else
using System.Collections;
using Utility;
using System.Text;
#endif
//todo:转义字符,容错处理
public static class XMLParser
{
private const char LT = '<';
private const char GT = '>';
private const char SQR = ']';
private const char DASH = '-';
private const char SPACE = ' ';
private const char QUOTE = '"';
private const char SLASH = '/';
private const char QMARK = '?';
private const char EQUALS = '=';
private const char EXCLAMATION = '!';
public static XMLNode ParseFile(string filePath)
{
if (FileUtility.Exists(filePath))
return Parse(FileUtility.ReadAllBytes(filePath));
return null;
}
public static XMLNode Parse(byte[] data)
{
string content = Encoding.UTF8.GetString(data);
return Parse(content);
}
public static XMLNode Parse(string content)
{
if (string.IsNullOrEmpty(content))
return null;
var rootNode = new XMLNode();
//trim the situation: <br/>
content = content.Replace ("/>", " />");
rootNode["_text"] = "";
string nodeContents = "";
bool inElement = false;
bool collectNodeName = false;
bool collectAttributeName = false;
bool collectAttributeValue = false;
bool quoted = false;
string attName = "";
string attValue = "";
string nodeName = "";
string textValue = "";
bool inMetaTag = false;
bool inComment = false;
bool inDoctype = false;
bool inCDATA = false;
XMLNodeList parents = new XMLNodeList();
XMLNode currentNode = rootNode;
for (var i = 0; i < content.Length; i++)
{
char c= content[i];
char cn;
char cnn;
char cp;
cn = cnn = cp = '\0';
if ((i + 1) < content.Length) cn = content[i + 1];
if ((i + 2) < content.Length) cnn = content[i + 2];
if (i > 0) cp = content[i - 1];
if (inMetaTag)
{
if (c == QMARK && cn == GT)
{
inMetaTag = false;
i++;
}
continue;
}
else
{
if (!quoted && c == LT && cn == QMARK)
{
inMetaTag = true;
continue;
}
}
if (inDoctype)
{
if (cn == GT)
{
inDoctype = false;
i++;
}
continue;
}
else if (inComment)
{
if (cp == DASH && c == DASH && cn == GT)
{
inComment = false;
i++;
}
continue;
}
else
{
if (!quoted && c == LT && cn == EXCLAMATION)
{
if (content.Length > i + 9 && content.Substring(i, 9) == "<![CDATA[")
{
inCDATA = true;
i += 8;
}
else if (content.Length > i + 9 && content.Substring(i, 9) == "<!DOCTYPE")
{
inDoctype = true;
i += 8;
}
else if (content.Length > i + 2 && content.Substring(i, 4) == "<!--")
{
inComment = true;
i += 3;
}
continue;
}
}
if (inCDATA)
{
if (c == SQR && cn == SQR && cnn == GT)
{
inCDATA = false;
i += 2;
continue;
}
textValue += c;
continue;
}
if (inElement)
{
if (collectNodeName)
{
if (c == SPACE)
{
collectNodeName = false;
}
else if (c == GT)
{
collectNodeName = false;
inElement = false;
}
if (!collectNodeName && nodeName.Length > 0)
{
// close tag
if (nodeName[0] == SLASH)
{
// close tag
if (textValue.Length > 0)
{
currentNode["_text"] += textValue;
}
textValue = "";
nodeName = "";
currentNode = parents.Pop();
}
else
{
if (textValue.Length > 0)
{
currentNode["_text"] += textValue;
}
textValue = "";
XMLNode newNode = new XMLNode();
newNode["_text"] = "";
newNode["_name"] = nodeName;
if (currentNode[nodeName] == null)
{
currentNode[nodeName] = new XMLNodeList();
}
XMLNodeList a = currentNode[nodeName] as XMLNodeList;
a.Push(newNode);
parents.Push(currentNode);
currentNode = newNode;
nodeName = "";
}
}
else
{
nodeName += c;
}
}
else
{
if (!quoted && c == SLASH && cn == GT)
{
inElement = false;
collectAttributeName = false;
collectAttributeValue = false;
if (!string.IsNullOrEmpty(attName))
{
if (!string.IsNullOrEmpty(attValue))
{
currentNode["@" + attName] = attValue;
}
else
{
currentNode["@" + attName] = true;
}
}
i++;
currentNode = parents.Pop();
attName = "";
attValue = "";
}
else if (!quoted && c == GT)
{
inElement = false;
collectAttributeName = false;
collectAttributeValue = false;
if (!string.IsNullOrEmpty(attName))
{
currentNode["@" + attName] = attValue;
}
attName = "";
attValue = "";
}
else {
if (collectAttributeName)
{
if (c == SPACE || c == EQUALS)
{
collectAttributeName = false;
collectAttributeValue = true;
}
else
{
attName += c;
}
}
else if (collectAttributeValue)
{
if (c == QUOTE)
{
if (quoted)
{
collectAttributeValue = false;
currentNode["@" + attName] = attValue;
attValue = "";
attName = "";
quoted = false;
}
else
{
quoted = true;
}
}
else {
if (quoted)
{
attValue += c;
}
else
{
if (c == SPACE)
{
collectAttributeValue = false;
currentNode["@" + attName] = attValue;
attValue = "";
attName = "";
}
}
}
}
else if (c == SPACE)
{
}
else
{
collectAttributeName = true;
attName = "" + c;
attValue = "";
quoted = false;
}
}
}
}
else
{
if (c == LT)
{
inElement = true;
collectNodeName = true;
}
else
{
textValue += c;
}
}
}
return rootNode;
}
}