-
Notifications
You must be signed in to change notification settings - Fork 23
/
xmlParse.cmd
469 lines (428 loc) · 11 KB
/
xmlParse.cmd
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: xmlParse.cmd
:: 2012-01-13 Frank P. Westlake
@Goto :main
The body of this message is a script; the statement above permits
this message to be saved and run without concern for this
documentation.
An XML pull and push parser.
xmlParse.cmd [/Q] file [command]
/Q Do not print the result but do set it into the environment.
file An XML file.
command A dot-joined path to the tag's content or to a tag
attribute being queried. This argument should be quoted
because in many cases the quotes will be necessary.
Quotes within the command must be converted to
apostrophes.
XML PUSH
If 'command' is not included this script will become a push-parser.
The parser will print each tag and any attributes it may have on
individual lines. The first word on the line is the name of the tag
prefixed with the text "Tag.". If the entire first word is "Tag."
then the remainder of the line is content. A tag line will appear
similar to this:
Tag.body bgcolor="#000000"
A line of content will appear similar to this:
Tag. How now brown cow?
Those lines should be read from a master script with a statement such
as this:
For /F "tokens=1* delims= " %%a in ('Call xmlParse.cmd "%file%"') Do (
Call :%%a %%b
)
If the tags are being used as subroutine labels as done in the FOR
statement above, then each tag must have a corresponding subroutine.
An example for the two lines shown above:
:Tag.body
REM Begin body of text.
CLS
Goto :EOF
:Tag./body
REM Do nothing.
Goto :EOF
:Tag.
REM Print the content.
Echo(%*
Goto :EOF
XML PULL
If 'command' is included this script becomes a pull-parser; without
this argument it is a push-parser. If a tag should be queried only if
it contains an attribute or attributes of a certain value, those
attributes may be specified in a parenthesized, space-separated list
and appended to the tag name. Each attribute name should begin with
the character '@'. To query an a attribute of a tag, join the
attribute name to the list with a dot and prefix the attribute name
with the character '@'. Examples follow.
A command line will look similar to this:
xmlParse demo.xml "papa.tag(@id='2' @class='A').@name"
TAG CONTENT QUERY
'Content' is the text between an opening tag and it's closing tag and
not including any intermediate tags.
With this XML file
<papa>
<tag>
Content
</tag>
</papa>
obtain the text "Content" with this query command
"papa.tag"
ATTRIBUTE QUERY
An attribute is the value identified by a key within an opening tag.
With this XML file
<papa>
<tag id="1" name="Joe"/>
</papa>
obtain the text "Joe" with this query command
"papa.tag.@name"
The attribute key begins with '@' and is separated from it's tag name
by a period.
IDENTIFYING A TAG BY ATTRIBUTE VALUESS
If there are multiple tags of the same name at the same level then a
specific tag can be selected by including one or more attributes
within parentheses:
With this XML file
<papa>
<tag id="1" name="Jim" "class='A'>James</tag>
<tag id="1" name="Jed" "class='B'/>
<tag id="2" name="Joe" "class='A'/>
<tag id="2" name="Jon" "class='B'/>
</papa>
obtain the text "Joe" with this query command
"papa.tag(@id='2' @class='A').@name"
The parenthesized list is appended to the tag name, attribute keys
begin with '@', and attributes are separated by space.
Frank Westlake
:main
@Echo OFF
SetLocal EnableExtensions EnableDelayedExpansion
For /F "delims==" %%a in ('SET $ 2^>NUL:') Do Set "%%a="
Set "#="
Set "$ME=%~nx0"
Set "$MY=%~dp0"
Set "$TAB= "
For /F "delims= " %%a in ("1%$TAB%") Do If "%%~a" EQU "1" (
(Echo %$ME%: The script's variable '$TAB' must be defined as a tab character.
Set /P "=LINE "<NUL:
FindStr /n /i /c:"$TAB=" "%~f0"|FindStr /v "FindStr")>&2
Goto :EOF
)
Set "$SPACE= "
Set "$find="
Set "$level=0"
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:args
Set "$=%~1"
If /I "!$!" EQU "/F" (
Set "$xml=%~f2"
SHIFT
) Else If /I "!$!" EQU "/Q" (
Set "$quiet= >NUL:"
) Else If /I "!$!" EQU "/EXAMPLES" (
Goto :examples
) Else If /I "!$!" EQU "/QUIET" (
Set "$quiet= >NUL:"
) Else If /I "!$!" EQU "/FILE" (
Set "$xml=%~f2"
SHIFT
) Else If "!$:~0,1!" EQU "/" (
(Echo %$ME%: Aborting, unknown parameter '!$!'.)>&2
Goto :EOF
) Else If DEFINED $xml (
If DEFINED $find (
(Echo !$Me!: Aborting, too many filenames.)>&2
Goto :EOF
)
Set "$find=%~1"
) Else (
Set "$xml=%~f1"
)
Shift
IF "%~1" NEQ "" Goto :args
If DEFINED $find Goto :xmlPull
Goto :xmlPush
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:xmlPush
For /F "delims=" %%a in (%$xml%) Do (
Set "#=!#!%%a"
Call :parseXML
)
If DEFINED $Continue (
Set "$EOF=1"
Call :parseXML
)
EXIT /B 0
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:xmlPull
Set $find=!$find:"='!
Set "$findRemaining=!$find!"
Call :getNextSegment
For /F "delims=" %%a in (%$xml%) Do (
Set "#=!#!%%a"
Call :parseXML
If DEFINED $QUIT Goto :return
)
If DEFINED $Continue (
Set "$EOF=1"
Call :parseXML
)
EXIT /B 1
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:return
EndLocal & Set "%$RETURN%=%$RETURN.value%"
EXIT /B 0
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:parseXML
If DEFINED $Continue (
Set "$Continue="
Call %$Continue%
Goto :EOF
)
If "!#:~0,1!" EQU "<" (
For /F "delims==" %%a in ('SET + 2^>NUL:') Do Set "%%a="
Set "#=!#:~1!"
Set "$tagBody="
Call :getTag
) Else (
Call :getContent
)
If DEFINED # Goto :parseXML
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:getNextSegment
Set "$nextSegment="
Set "$attributes="
For /F "delims==" %%a in ('SET @ 2^>NUL:') Do Set "%%a="
For /F "tokens=1* delims=." %%a in ("!$findRemaining!") Do (
Set "$nextSegment=%%~a"
Set "$findRemaining=%%~b"
If "!$nextSegment:~0,1!" NEQ "@" (
For /F "tokens=1* delims=(" %%c in ("%%~a") Do (
REM For /F "tokens=1* delims=@" %%c in ("%%~a") Do (
Set "$nextSegment=%%~c"
Set "$attributes=%%~d"
)
)
)
If DEFINED $attributes (
For /F "tokens=1* delims=)" %%a in ("!$attributes!") Do (
Set "$attributes=%%a"
)
Call :getAttributes
)
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:getAttributes
Set "$attributes=!$attributes:~0,-1!"
Set $attributes=!$attributes:'="!
Set "@="
For %%a in (!$attributes!) Do (
If NOT DEFINED @ (
Set "@=%%~a"
) Else (
Set "!@!=%%~a"
Set "@="
)
)
If DEFINED @ (If NOT DEFINED !@! (Set "!@!=true"))
Set "@="
Set "$attributes="
:: Rebuilding $attributes but it is currently only used as a flag.
For /F "tokens=1* delims==" %%a in ('Set @ 2^>NUL:') Do Set "$attributes=!$attributes!%%a='%%~b'"
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:processTag
If "!$tag!" EQU "--" (
Goto :EOF
)
If "!$tag:~0,1!" NEQ "/" Set /A "$level+=1"
If NOT DEFINED $findRemaining (
If NOT DEFINED $targetLevel (
Set /A "$targetLevel=$level"
) Else IF !$level! EQU !$targetLevel! (
If DEFINED $pass (
If "!$tag:~0,1!" EQU "/" (
Set "$QUIT=defined"
) Else If DEFINED +/ (
Set "$QUIT=defined"
)
If DEFINED $QUIT (
Call :trim $Content
Set "$RETURN.value=!$Content!"
Echo(!$Content!%$quiet%
EXIT /B 0
)
)
)
)
If "!$tag!" EQU "!$nextSegment!" (
Set "$pass=defined"
If DEFINED $gut Call :parseGut
If DEFINED $attributes (
For /F "delims==" %%a in ('Set @ 2^>NUL:') Do (
Set "$=%%~a"
Call Set "$=%%!$:@=+!%%"
If "!%%~a!" NEQ "!$!" Set "$pass="
)
)
If DEFINED $pass (
If DEFINED $findRemaining (
Call :getNextSegment
If DEFINED $nextSegment (
If "!$nextSegment:~0,1!" EQU "@" (
Set "$=+!$nextSegment:~1!"
Call Set "$=%%!$!%%"
Set "$QUIT=defined"
Set "$RETURN=!$nextSegment!"
Set "$RETURN.value=!$!"
Echo(!$!%$quiet%
EXIT /B 0
)
)
)
If NOT DEFINED $findRemaining (
If NOT DEFINED $targetLevel (
Set /A "$targetLevel=1+$level"
) Else If !$targetLevel! EQU !$level! (
Set "$RETURN=!$nextSegment!"
Set "$saveContent=defined"
)
)
)
)
If "!$tag:~0,1!" EQU "?" (
Set /A "$level-=1"
) Else If "!$tag:~0,1!" EQU "/" (
Set /A "$level-=1"
) Else If DEFINED /+ (
Set /A "$level-=1"
)
::1
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:getTag
Set "#=!#:>=<>!"
For /F "tokens=1* delims=<" %%a in ("!#!") Do (
Set "$tagBody=!$tagBody!%%a "
Set "#=%%b"
)
If DEFINED # (
Set "#=!#:<>=>!"
If "!#:~0,1!" EQU ">" (
Set "#=!#:~1!"
) Else (
Set "$Continue=%0"
Goto :EOF
)
) Else (
Set "$Continue=%0"
Goto :EOF
)
Set "$gut="
Call :splitGut
If DEFINED $find (
Call :processTag
) Else (
Echo(Tag.!$tag! !$gut!
)
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:splitGut
Set "$tag="
Set $tagBody=!$tagBody:"='!
For /F "tokens=1*" %%a in ("!$tagBody!") Do (
Set "$tag=%%a"
Set "$gut=%%b"
)
Call :trim $gut
If "!$gut:~-1!" EQU "/" Set "$gut=!$gut:~0,-1! /"
Set "$tagBody="
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:parseGut
Set "+="
Set $gut=!$gut:'="!
If "!$gut:~-1!" EQU "?" (
Set "+/=defined"
Set "$gut=!$gut:~0,-1!"
) Else If "!$gut:~-1!" EQU "/" (
Set "+/=defined"
Set "$gut=!$gut:~0,-1!"
)
For %%a in (!$gut!) Do (
If NOT DEFINED + (
Set "+=%%~a"
) Else (
Set "+!+!=%%~a"
Set "+="
)
)
Set "+="
Set "$gut="
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:getContent
Set "$thisContent="
For /F "tokens=1* delims=<" %%a in ("!#!") Do (
Set "#=%%b"
If NOT DEFINED $find (
Set "$thisContent=%%a"
) Else If DEFINED $saveContent (
Set "$thisContent=%%a"
)
)
If DEFINED # (Set "#=<!#!")
REM For xmlPush:
If NOT DEFINED $find (
Call :trim $thisContent
If DEFINED $thisContent (
Echo(Tag. !$thisContent!
Set "$thisContent="
)
Goto :EOF
)
If DEFINED $saveContent (
Set "$Content=!$Content!!$thisContent!"
Call :trim $Content
)
If DEFINED $Content (
Set "$Content=!$Content! "
)
Set "$thisContent="
Goto :EOF
:xgetContent
Set "$thisContent="
For /F "tokens=1* delims=<" %%a in ("!#!") Do (
If DEFINED $saveContent (Set "$thisContent=%%a")
Set "#=%%b"
)
If DEFINED # (Set "#=<!#!")
If DEFINED $saveContent (
Set "$Content=!$Content!!$thisContent!"
Call :trim $Content
)
If DEFINED $Content (
Set "$Content=!$Content! "
)
Set "$thisContent="
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:trim varname
If DEFINED %1 (
If "!%1:~0,1!" EQU "!$SPACE!" (
Set "%1=!%1:~1!"
Goto :trim
) Else If "!%1:~0,1!" EQU "!$TAB!" (
Set "%1=!%1:~1!"
Goto :trim
) Else If "!%1:~-1!" EQU "!$SPACE!" (
Set "%1=!%1:~0,-1!"
Goto :trim
) Else If "!%1:~-1!" EQU "!$TAB!" (
Set "%1=!%1:~0,-1!"
Goto :trim
)
)
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::