-
Notifications
You must be signed in to change notification settings - Fork 9
/
INFO
287 lines (219 loc) · 7.55 KB
/
INFO
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
XML::Stream Tree Building 101
In order to not reinvent the wheel, XML::Stream uses the XML::Parser::Tree
object as the data structure it passes around and stores. Two things need
to be covered in order to understand what the data looks like when you get
it from XML::Stream.
Section 1: What does an XML::Parser::Tree object look like?
The original documentation for XML::Parser::Tree can be a little hard to
understand so we will go over the structure here for completeness. The
that is built is essentially a big nested array. This guarantees that you
see the tags in the order receded from the stream, and that the nesting of
tags is maintained. The actual structure of the tree is complicated so
let's cover an example:
<A n='1>First<B n='2' m='bob'>Second</B>Third<C/></A>
What we are working with is a nested <B/> tag inside the CDATA of <A/>.
There are attributes on both tags that must be stored. To do this we use
an array. The first element of the array is the root tag, or A.
[ 'A' ]
The second element is a list of all the things contained in <A/>.
[ 'A', [ ] ]
That new list is recursively built as you go down the hierarchy, so let's
examine the structure. The first element of that new list is a hash of
key/value pairs that represent the attributes of the tag you are looking
at. In the case of the root tag <A/> the hash would be { 'n' => '1' }. So
adding that to the list we get:
[ 'A', [ { 'n' => '1' } ] ]
Now, the rest of the new list is a set of two elements added at a time.
Either a tag name followed by a list that represents the new tag, or a
"0" (zero) followed by a string. This might be confusing so let's go to
the example. As we parse the <A/> tag we see the string "First". So
according to the rule we add a "0" and "First" to the list:
[ 'A', [ { 'n' => '1' }, 0, "First" ] ]
The next element is the <B/> tag. So the rules says that we add the
tag and then a list that contains that tag:
[ 'A', [ { 'n' => '1' }, 0, "First", 'B', [ ] ] ]
Parsing the <B/> tag we see an attributes n = '2' and m = 'bob. So
those go into a hash and that hash becomes the first element in the list
for B:
[
'A', [ { 'n' => '1' },
0, "First",
'B', [ { 'n' => '2', 'm' => 'bob' } ]
]
]
Next we see that <B/> contains the CDATA "Second" so that goes into
the list for B:
[
'A', [ { 'n' => '1' },
0, "First",
'B', [ { 'n' => '2', 'm' => 'bob' }
0, "Second"
]
]
]
<B/> closes and we leave this list and return to the list for <A/>.
The next element there is CDATA so add a '0' and "Third" onto the list
for A:
[
'A', [ { 'n' => '1' },
0, "First",
'B', [ { 'n' => '2', 'm' => 'bob' }
0, "Second"
]
0, "Third"
]
]
Now we see another tag, <C/>. So we add C and a list onto the A's list:
[
'A', [ { 'n' => '1' },
0, "First",
'B', [ { 'n' => '2', 'm' => 'bob' }
0, "Second"
]
0, "Third",
'C', [ ]
]
]
Parsing <C/> we see that it has no attributes so we add an empty hash
to the list for C:
[
'A', [ { 'n' => '1' },
0, "First",
'B', [ { 'n' => '2', 'm' => 'bob' }
0, "Second"
]
0, "Third",
'C', [ { } ]
]
]
Next we see that <C/> contains no other data and ends in a />. This
means that the tag is finished and contains no data. So close C and go
back to <A/>. There is no other data in A so we close <A/> and we have
our finished tree:
[
'A', [ { 'n' => '1' },
0, "First",
'B', [ { 'n' => '2', 'm' => 'bob' }
0, "Second"
]
0, "Third",
'C', [ { } ]
]
]
Section II: How do we build the XML::Parser::Tree?
For those who are interested in how we build a tree read on, for those
that got enough out of the previous section, read anyway.
Recursion would be too difficult to do in this linear problem so we
looked at the problem and engineered a way to use a single list to build
the structure. Every time a new tag is encountered a new list is added to
end of the main list. When that list closes it is removed from the main
list and then added onto the end of the previous element in the list,
which is usually another list. In other words:
The current list looks like this:
[aaa]
We see a new tag and make a new list:
[aaa], [bbb]
Populate that list and then close it. When we close we remove from the
list and make it the last element in the previous list elements list.
Confused? Watch:
[aaa], [bbb] --> [aaa, [bbb] ]
As we "recurse" the hierarchy and close tags we push the new list back
up to the previous list element and create the proper nesting.
Let's go over the same example from Section I.
<A n='1>First<B n='2' m='bob'>Second</B>Third<C/></A>
We start and push A on the list:
[ 'A' ]
Next we create a new list for the <A/> tag and populate the attribute
hash:
[ 'A',
[ { 'n'=>'1' } ]
]
Now we see the CDATA:
[ 'A',
[ { 'n'=>'1' }, 0, "First" ]
]
Next it's the <B/> tag, so push B on the list and make a new list on
the end of the main list:
[ 'A',
[ { 'n'=>'1' }, 0, "First", 'B' ],
[ ]
]
Parsing the <B/> tag we see that is has attributes and CDATA:
[ 'A',
[ { 'n'=>'1' }, 0, "First", 'B' ],
[ {'n'=>'2','m'=>"bob"}, 0, "Second" ]
]
Now <B/> closes and the magic begins... With the closing of <B/> we
pop the last element off the list. Then we take that element and push it
onto the last element of the main list. So we aren't pushing it onto the
main list, but onto the last element of the main list:
Popped value: [ {'n'=>'2','m'=>"bob"}, 0, "Second" ]
List: [ 'A',
[ { 'n'=>'1' }, 0, "First", 'B' ]
]
Push value on last element of list:
[ 'A',
[ { 'n'=>'1' }, 0, "First", 'B', [ {'n'=>'2','m'=>"bob"}, 0, "Second" ] ]
]
Now we see a CDATA and push that onto the last element in the list:
[ 'A',
[ { 'n'=>'1' },
0, "First",
'B', [ {'n'=>'2','m'=>"bob"},
0, "Second"
],
0, "Third"
]
]
Finally we see the <C/> tag, so a 'C' is pushed onto the list, and then
a new list is created to contain the new tag:
[ 'A',
[ { 'n'=>'1' },
0, "First",
'B', [ {'n'=>'2','m'=>"bob"},
0, "Second"
],
0, "Third",
'C'
],
[ ]
]
<C/> no attributes so an empty hash is pushed onto the list:
[ 'A',
[ { 'n'=>'1' },
0, "First",
'B', [ {'n'=>'2','m'=>"bob"},
0, "Second"
],
0, "Third",
'C'
],
[ { } ]
]
<C/> contains no data so nothing is to be done there. The tag closes
and we do the magic again. Pop the last element off the main list and
push it onto the previous element's list:
[ 'A',
[ { 'n'=>'1' },
0, "First",
'B', [ {'n'=>'2','m'=>"bob"},
0, "Second"
],
0, "Third",
'C', [ { } ]
]
]
Now <A/> closes so we pop the last element off the main list and push
is onto a list with the previous element, which is the string 'A':
[ 'A',
[ { 'n'=>'1' },
0, "First",
'B', [ {'n'=>'2','m'=>"bob"},
0, "Second"
],
0, "Third",
'C', [ { } ]
]
]
And voila! The tree is complete. We now call the callback function,
pass it the tree, and then reset the tree for the next tag to be parsed.