-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse-xml.py
374 lines (374 loc) · 15.2 KB
/
parse-xml.py
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
#!/usr/bin/python3
#
# SCRIPT NAME: parse-xml.py
#
# PURPOSE: To extact data from a Zerto VPG export XML file into a legible CSV file
#
# PARAMETER FILE: N/A
#
# INPUT FILE: N/A
#
# CALLED SCRIPTS: N/A
#
# IMPORTED MODULES: xml_elements_long.py
#
# SCRIPT USAGE: parse-xml.py ExportedSettings_date_time.xml
#
# UPDATE HISTORY:
# DATE AUTHOR UPDATE DESCRIPTION
# 05.09.2019 Lou Sassani Initial script creation
# 30.09.2019 Lou Sassani Hit an issue with a different XML export file which was causing
# the ProcessXMLfile sub-routine to prematurely close the temp file.
# I've removed the file close function in the sub-routine ProcessXMLfile
#
#
# Written By: LOU SASSANI,
# Systems Engineer
# AsiaPacific,
# Copyright Zerto Corporation
# Email: lou.sassani@zerto.com
# Legal Disclaimer:
#
# ----------------------
# This script is an example script and is not supported under any Zerto support program or service.
#
# The author and Zerto further disclaim all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for
# a particular purpose.
#
# In no event shall Zerto, its authors or anyone else involved in the creation, production or delivery of the scripts be liable for any damages whatsoever
# (including, withoutlimitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss)
# arising out of the use of or the inability to use the sample scripts or documentation, even if the author or Zerto has been advised of the possibility
# of such damages. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you.
#
## -- Set Global Veriables
#
import sys, os, re
Script_Version = "0.0.002"
#Script_Name = sys.argv[0]
Script_Name = os.path.basename(__file__)
global returncode
returncode = 0
#
## - Process filename passed
#
def ProcessPARAMETERS():
global infile
parmlen = len(sys.argv)
if parmlen == 2:
infile = sys.argv[1]
else:
print ("Script requires an XML Filename to process")
print ("Exmaple:")
print (Script_Name, "(Filename to process)")
exit(4)
#
## - This sub-routine will generate the XML data in a raw CSV appened format
#
def ProcessXMLfileCSVRaw():
#
## - Import known schemas
#
import xml_elements_long as EL
#
## - import XML Element tree structure
#
import xml.etree.ElementTree as ET
#
## - Zerto VPG export XML file
#
tree = ET.parse(infile)
root = tree.getroot()
#
## - Root element
#
for S in root.iter(EL.S006):
for C0 in S.getchildren():
cc=str(C0)
s0,s1 = cc.split("}")
s0,S1 = s1.split("'")
print ("\n","Root->", s0,",",C0.text,",", end="")
#
## - First Child Element
#
for C1 in C0.getchildren():
cc=str(C1)
s0,s1 = cc.split("}")
s0,S1 = s1.split("'")
print (s0,",",C1.text,",", end="")
#
## - First Child Element
#
for C2 in C1.getchildren():
cc=str(C2)
s0,s1 = cc.split("}")
s0,S1 = s1.split("'")
print (s0,",",C2.text,",", end="")
#
## - Second Child Element
#
for C3 in C2.getchildren():
cc=str(C3)
s0,s1 = cc.split("}")
s0,S1 = s1.split("'")
print (s0,",",C3.text,",", end="")
#
## - Third Child Element
#
for C4 in C3.getchildren():
cc=str(C4)
s0,s1 = cc.split("}")
s0,S1 = s1.split("'")
print (s0,",",C4.text,",", end="")
#
## - Forth Child Element
#
for C5 in C4.getchildren():
cc=str(C5)
s0,s1 = cc.split("}")
s0,S1 = s1.split("'")
print (s0,",",C5.text,",", end="")
#
## - Fifth Child Element
#
for C6 in C5.getchildren():
cc=str(C6)
s0,s1 = cc.split("}")
s0,S1 = s1.split("'")
print (s0,",",C6.text,",", end="")
#
## - Sixth Child Element
#
for C7 in C6.getchildren():
cc=str(C7)
s0,s1 = cc.split("}")
s0,S1 = s1.split("'")
print (s0,",",C7.text,",", end="")
#
## - Seventh Child Element
#
for C8 in C7.getchildren():
cc=str(C8)
s0,s1 = cc.split("}")
s0,S1 = s1.split("'")
print (s0,",",C8.text,",", end="")
#
## - Eighth Child Element
#
for C9 in C8.getchildren():
cc=str(C9)
s0,s1 = cc.split("}")
s0,S1 = s1.split("'")
print (s0,",",C9.text,",", end="")
#
## - Ninth Child Element
#
for C10 in C9.getchildren():
cc=str(C10)
s0,s1 = cc.split("}")
s0,S1 = s1.split("'")
print (s0,",",C10.text,",", end="")
#
## - This sub-routine will generate the XML data in formatted CSV format
#
def ProcessXMLfile():
#
## - Generate a temp file name
#
import tempfile
global VMFtemp
VMFtemp = tempfile.NamedTemporaryFile(prefix="vpg")
#
## - Import known schemas
#
import xml_elements_long as EL
#
## - import XML Element tree structure
#
import xml.etree.ElementTree as ET
#
## - Zerto VPG export XML file
#
tree = ET.parse(infile)
root = tree.getroot()
#
## - Root element
#
global VPGdata
VWF = open(VMFtemp.name,"w+")
VPGcount=0
VPGdata=[]
for S in root.iter(EL.S006):
for C0 in S.getchildren():
cc=str(C0)
s0,s1 = cc.split("}")
s0,S1 = s1.split("'")
VPGcount+=1
VPGdata=("VPGN"+str(VPGcount),str(s0),str(C0.text))
VWF.write(str(VPGdata))
VWF.write("\n")
#
## - First Child Element
#
for C1 in C0.getchildren():
cc=str(C1)
s0,s1 = cc.split("}")
s0,S1 = s1.split("'")
VPGdata=("VPGN"+str(VPGcount),str(s0),str(C1.text))
VWF.write(str(VPGdata))
VWF.write("\n")
#
## - First Child Element
#
for C2 in C1.getchildren():
cc=str(C2)
s0,s1 = cc.split("}")
s0,S1 = s1.split("'")
VPGdata=("VPGN"+str(VPGcount),str(s0),str(C2.text))
VWF.write(str(VPGdata))
VWF.write("\n")
#
## - Second Child Element
#
for C3 in C2.getchildren():
cc=str(C3)
s0,s1 = cc.split("}")
s0,S1 = s1.split("'")
VPGdata=("VPGN"+str(VPGcount),str(s0),str(C3.text))
VWF.write(str(VPGdata))
VWF.write("\n")
if s0 == "Name":
VPGdata = ("VPGN"+str(VPGcount),"VPG-Name=", str(C3.text))
VWF.write(str(VPGdata))
VWF.write("\n")
#
## - Third Child Element
#
for C4 in C3.getchildren():
cc=str(C4)
s0,s1 = cc.split("}")
s0,S1 = s1.split("'")
VPGdata=("VPGN"+str(VPGcount),str(s0),str(C4.text))
VWF.write(str(VPGdata))
VWF.write("\n")
#
## - Forth Child Element
#
for C5 in C4.getchildren():
cc=str(C5)
s0,s1 = cc.split("}")
s0,S1 = s1.split("'")
VPGdata=("VPGN"+str(VPGcount),str(s0),str(C5.text))
VWF.write(str(VPGdata))
VWF.write("\n")
#
## - Fifth Child Element
#
for C6 in C5.getchildren():
cc=str(C6)
s0,s1 = cc.split("}")
s0,S1 = s1.split("'")
VPGdata=("VPGN"+str(VPGcount),str(s0),str(C6.text))
VWF.write(str(VPGdata))
VWF.write("\n")
#
## - Sixth Child Element
#
for C7 in C6.getchildren():
cc=str(C7)
s0,s1 = cc.split("}")
s0,S1 = s1.split("'")
VPGdata=("VPGN"+str(VPGcount),str(s0),str(C7.text))
VWF.write(str(VPGdata))
VWF.write("\n")
#
## - Seventh Child Element
#
for C8 in C7.getchildren():
cc=str(C8)
s0,s1 = cc.split("}")
s0,S1 = s1.split("'")
VPGdata=("VPGN"+str(VPGcount),str(s0),str(C8.text))
VWF.write(str(VPGdata))
VWF.write("\n")
#
## - Eighth Child Element
#
for C9 in C8.getchildren():
cc=str(C9)
s0,s1 = cc.split("}")
s0,S1 = s1.split("'")
VPGdata=("VPGN"+str(VPGcount),str(s0),str(C9.text))
VWF.write(str(VPGdata))
VWF.write("\n")
#
## - Ninth Child Element
#
for C10 in C9.getchildren():
cc=str(C10)
s0,s1 = cc.split("}")
s0,S1 = s1.split("'")
VPGdata=("VPGN"+str(VPGcount),str(s0),str(C10.text))
VWF.write(str(VPGdata))
VWF.write("\n")
#
## - Close File
#
#VWF.close()
#
## - This sub-routine will Process the XML data into a formatted CSV file
#
def ProcessXMLfileCSV():
#
## - Create Array Name
#
VPGNameArray = []
#
## - Generate CSV name
#
global CSVFileName
CSVFileName = (str(infile)+".csv")
CSV = open(CSVFileName,"w")
#
## - Open XML data extra for reading
#
VWF = open(VMFtemp.name,"rt")
VPGDetails = VWF.readlines()
for VPGlines in VPGDetails:
VPGnum, VPGfield, FieldDetails = VPGlines.split(",")
if "VPG-Name" in VPGfield:
VPGnum = VPGnum.replace("'","")
VPGnum = VPGnum.replace("(","")
FieldDetails = FieldDetails.replace("'","")
VPGAddToArray = (VPGnum+":"+FieldDetails)
VPGNameArray.append(VPGAddToArray)
VPGnameToProcess = VPGNameArray
CSVdata=("VPGname,"+"VPGfield,"+"FieldDetails")
CSV.write(str(CSVdata))
CSV.write("\n")
for VPGnames in VPGnameToProcess:
global VPGname
VPGnumToFind, VPGname = VPGnames.split(":")
VPGname, x = VPGname.split(")")
VWF.seek(0,0)
VPGDetails = VWF.readlines()
for CSVBuild in VPGDetails:
VPGnum, VPGfield, FieldDetails = CSVBuild.split(",")
if VPGnumToFind in VPGnum:
FieldDetails = FieldDetails.replace(")","")
CSVdata=(VPGname+","+VPGfield+","+FieldDetails)
if "VPG-Name" not in VPGfield:
CSV.write(str(CSVdata))
#
## - Close File
#
VWF.close()
#
##
### -- Mainline Processing
##
#
ProcessPARAMETERS()
ProcessXMLfile()
ProcessXMLfileCSV()
#ProcessXMLfileCSVRaw()
print ("CSV filename", CSVFileName, "Created")