-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxmlparser.agc
More file actions
305 lines (254 loc) · 9.68 KB
/
xmlparser.agc
File metadata and controls
305 lines (254 loc) · 9.68 KB
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
//////////////////////////////////////////////////////////////////////
// Title: XML Parser
// Author: Phaelax
// Date: March 4, 2017
//
// Commands:
// arr[] = xml_loadDocument(string filename)
// int = xml_GetParentId(dom ref as XML_Element[], elementId as integer)
// int = xml_GetAttributeCount(dom ref as XML_Element[], elementId as integer)
// int = xml_GetAttributeKeyById(dom ref as XML_Element[], elementId as integer, attributeId as integer)
// int = xml_GetAttributeValueById(dom ref as XML_Element[], elementId as integer, attributeId as integer)
// int = xml_GetTagName(dom ref as XML_Element[], elementId as integer)
// int = xml_GetChildCount(dom ref as XML_Element[], elementId as integer)
// string = xml_GetAttributeValueByName(dom ref as XML_Element[], elementId as integer, att as string))
// int = xml_GetAttributeIdByName(dom ref as XML_Element[], elementId as integer, att as string))
// ar[] = xml_GetAttributesArray(dom ref as XML_Element[], elementId as integer)
// string = xml_GetElementValue(dom ref as XML_Element[], elementId as integer)
// int = xml_GetChildIdById(dom ref as XML_Element[], elementId as integer, childId as integer)
// arr[] = xml_GetRootElementsArray(dom ref as XML_Element[])
SetVirtualResolution(1920,1200)
SetWindowSize(1920, 1200, 0)
UseNewDefaultFonts(1)
SetSyncRate(60, 0 )
setPrintSize(20)
Type XML_Attribute
key as string
value as string
EndType
Type XML_Tag
parent as integer
name as string
attributes as XML_Attribute[]
value as string
EndType
Type XML_Element
self as XML_Tag
children as integer[-1]
EndType
dom as XML_Element[]
start = GetMilliseconds()
dom = xml_loadDocument("overworld22.tmx")
finish = getMilliseconds()
// Get the root elements of the dom tree
roots as integer[]
roots = xml_GetRootElementsArray(dom)
do
print(finish-start)
print("")
for i = 0 to roots.length
printElement(dom, roots[i], "")
next i
sync()
loop
function printElement(dom ref as XML_Element[], i as integer, space as string)
name$ = xml_GetTagName(dom, i)
parent = xml_GetParentId(dom, i)
childCount = xml_GetChildCount(dom, i)
att as XML_Attribute[]
att = xml_GetAttributesArray(dom, i)
value$ = xml_GetElementValue(dom, i)
printc(space+str(i)+") "+name$)
printc(" [")
for k = 0 to att.length
printc(att[k].key+" = "+att[k].value+" , ")
next k
printc("]")
printc(" (childCount: "+str(childCount)+")")
print(" parentID: "+str(parent))
for j = 0 to childCount-1
c = xml_GetChildIdById(dom, i, j)
printElement(dom, c, space+" ")
next j
print("")
endfunction
//////////////////////////////////////////////////////////////////////
// Return an array of indices of each element that is part of the dom root
//////////////////////////////////////////////////////////////////////
function xml_GetRootElementsArray(dom ref as XML_Element[])
arr as integer[]
for i = 0 to dom.length
if dom[i].self.parent = 0 then arr.insert(i)
next i
endfunction arr
//////////////////////////////////////////////////////////////////////
// Return the element id of the specified child element
//////////////////////////////////////////////////////////////////////
function xml_GetChildIdById(dom ref as XML_Element[], elementId as integer, childId as integer)
if childId = -1 then exitfunction -1
endfunction dom[elementId].children[childId]
//////////////////////////////////////////////////////////////////////
// Return the element's value (inner content)
//////////////////////////////////////////////////////////////////////
function xml_GetElementValue(dom ref as XML_Element[], elementId as integer)
endfunction dom[elementId].self.value
//////////////////////////////////////////////////////////////////////
// Return an array of XML_Attribute
//////////////////////////////////////////////////////////////////////
function xml_GetAttributesArray(dom ref as XML_Element[], elementId as integer)
endfunction dom[elementId].self.attributes
//////////////////////////////////////////////////////////////////////
// Returns the parent ID of this element
//////////////////////////////////////////////////////////////////////
function xml_GetParentId(dom ref as XML_Element[], elementId as integer)
endfunction dom[elementId].self.parent
//////////////////////////////////////////////////////////////////////
// Returns the key/name of an attribute
//////////////////////////////////////////////////////////////////////
function xml_GetAttributeKeyById(dom ref as XML_Element[], elementId as integer, attributeId as integer)
endfunction dom[elementId].self.attributes[attributeId].key
//////////////////////////////////////////////////////////////////////
// Returns the value of an attribute given it's ID
//////////////////////////////////////////////////////////////////////
function xml_GetAttributeValueById(dom ref as XML_Element[], elementId as integer, attributeId as integer)
endfunction dom[elementId].self.attributes[attributeId].value
//////////////////////////////////////////////////////////////////////
// Returns the value of an attribute, looked up by the attribute name
//////////////////////////////////////////////////////////////////////
function xml_GetAttributeValueByName(dom ref as XML_Element[], elementId as integer, att as string)
for i = 0 to dom[elementId].self.attributes.length
if dom[elementId].self.attributes[i].key = att then exitfunction dom[elementId].self.attributes[i].value
next i
endfunction ""
//////////////////////////////////////////////////////////////////////
// Returns the ID of an attribute of specified element
//////////////////////////////////////////////////////////////////////
function xml_GetAttributeIdByName(dom ref as XML_Element[], elementId as integer, att as string)
for i = 0 to dom[elementId].self.attributes.length
if dom[elementId].self.attributes[i].key = att then exitfunction i
next i
endfunction -1
//////////////////////////////////////////////////////////////////////
// Return the tag name of this element
//////////////////////////////////////////////////////////////////////
function xml_GetTagName(dom ref as XML_Element[], elementId as integer)
endfunction dom[elementId].self.name
//////////////////////////////////////////////////////////////////////
// Returns the number of attributes for this element
//////////////////////////////////////////////////////////////////////
function xml_GetAttributeCount(dom ref as XML_Element[], elementId as integer)
endfunction dom[elementId].self.attributes.length+1
//////////////////////////////////////////////////////////////////////
// Returns the number of direct children under the specified element
//////////////////////////////////////////////////////////////////////
function xml_GetChildCount(dom ref as XML_Element[], elementId as integer)
endfunction dom[elementId].children.length+1
//////////////////////////////////////////////////
// Returns the index in xml array of the first
// occurrence of 'tag'
// Returns -1 if no match found
//////////////////////////////////////////////////
function xml_FindFirstTag(dom as XML_Element[], tag as string)
for i = 0 to dom.length
if dom[i].self.name = tag then exitfunction i
next i
endfunction -1
//////////////////////////////////////////////////
// Loads an XML file into a DOM-like structure
// and returns it as an array of XML_Element
//////////////////////////////////////////////////
function xml_loadDocument(file as string)
elements as XML_Element[]
openTags as integer[0]
f = openToRead(file)
q = 0
repeat
inc q
// find tag
s$ = readLine(f)
L = len(s$)
a1 = 0
findAtts = 0
tagFound = 0
for i = 1 to L
// tag opening
b$ = mid(s$, i, 1)
if b$ = "<"
if mid(s$, i+1, 1) = "/"
openTags.remove()
exit
else
for j = i+1 to L
c$ = mid(s$, j, 1)
if c$ = " " or c$ = ">"
tagFound = 1
tag$ = mid(s$, i+1, j-i-1)
e as XML_Element
e.self.name = tag$
e.self.parent = openTags[openTags.length]
elements.insert(e)
insertedElement = elements.length
if e.self.parent > 0 then elements[e.self.parent].children.insert(insertedElement)
a1 = j+1
if c$ = " "
findAtts = 1
else
// add this tag to open stack
openTags.insert(elements.length)
endif
exit
endif
next j
endif
else
if asc(b$) > 32 and asc(b$) < 127
elements[openTags[openTags.length]].self.value = s$
exit
endif
endif
next i
if findAtts = 1
repeat
n1 = 0 : n2 = 0
v1 = 0 : v2 = 0
insert = 0
c1$ = ''
// get attributes
for i = a1 to L
c$ = mid(s$, i, 1)
// start of attribute name
if c$ <> " " and n1=0 then n1 = i
if c$ = "=" then n2 = i
if c1$ <> ''
if c$ = c1$
v2 = i
a1 = i+1
insert = 1
exit
endif
else
if c$ = "'" or c$ = '"'
c1$ = c$
v1 = i+1
endif
endif
next i
if insert = 1
a as XML_Attribute
a.key = mid(s$, n1, n2-n1)
a.value = mid(s$, v1, v2-v1)
elements[elements.length].self.attributes.insert(a)
endif
until i >= L
// Check next to last character to see if this is a singleton tag
c$ = mid(s$, L-1, 1)
if c$ = "/" or c$ = "?"
// if singleton, do nothing
else
// opening tag, add to stack
openTags.insert(elements.length)
endif
endif
until fileEOF(f)
closeFile(f)
endfunction elements