-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlReParse.py
executable file
·300 lines (214 loc) · 9.48 KB
/
xmlReParse.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
"""
XML parsing ... at least initially for N-of-One reports ...
"""
__author__ = "Sheila M Reynolds"
__version__ = "0.0.1"
__status__ = "Prototype"
import argparse
import sys
import time
import uuid
import xml.etree.ElementTree as ET
# ----------------------------------------------------------------------------------------------------
inputFilename = ''
outFh = None
maxDepth = 0
ithGen = 0
genList = []
naStrings = ['[]','{}','na','NA',':','::','[""]',"['']",'EditMe','Please edit me','None','None.','None found','Not determined','Unknown.']
# ----------------------------------------------------------------------------------------------------
def quickStrip(s):
if (str(s) == 'nan'):
return ('')
t = ''
for u in s.splitlines():
if (len(u) > 0):
if (len(t) > 0):
t += ' ' + u.strip()
else:
t += u.strip()
tabSplit = t.split('\t')
t = ''
for u in tabSplit:
if (len(u) > 0):
if (len(t) > 0):
t += ' ' + u.strip()
else:
t += u.strip()
t = t.strip()
t = cleanTag(t)
if ( t.find(',') >= 0 ):
if ( t.find('"') >= 0 ): t = t.replace('"',"'")
if ( t.find('"') < 0 ):
t = '"' + t + '"'
else:
print ( " FATAL ERROR with commas and quotes in a string !!! " )
print ( " <%s> " % t )
return (t)
# ----------------------------------------------------------------------------------------------------
def isContainerNode(node):
## print ( " CCCC : in isContainerNode ... " )
## we'll start with an assumption that it IS just a container node ...
cFlag = True
if ( len(node.attrib) > 0 ):
for a in node.attrib:
textString = quickStrip(str(node.attrib[a]))
if ( len(textString)==0 or textString in naStrings ):
skipFlag = True
else:
cFlag = False
textString = quickStrip(str(node.text))
if ( len(textString)==0 or textString in naStrings ):
skipFlag = True
else:
cFlag = False
## print ( " CCCC : leaving isContainerNode ... ", cFlag )
return ( cFlag )
# ----------------------------------------------------------------------------------------------------
def handleNodeData(node,pID,eID):
## print ( " DDDD : in handleNodeData ... ", pID, eID )
## keep track of whether or not we have written something out for this node ...
outFlag = False
if ( len(node.attrib) > 0 ):
## print ( " found attributes : ", ithGen, node.tag, len(node.attrib), node.attrib )
## need to handle the attributes:
## NB (!!!) attributes need to have their own 'bundle' identifier that is separate
## from the other 'contents' of this node...
for a in node.attrib:
textString = quickStrip(str(node.attrib[a]))
if ( len(textString)==0 or textString in naStrings ):
skipFlag = True
## print ( " DDDD : skipping attribute <%s> " % a )
else:
## write out the 'attrib' contents of this particular node
longLabel = ''
if ( len(genList) > 0 ): longLabel = ' > '.join(genList) + ' > '
longLabel += cleanTag(node.tag) + ' > ' + cleanTag(a)
longLabel = longLabel.strip()
## print ( " DDDD : writing attribute {%s = %s} " % ( longLabel, textString ) )
outString = inputFilename + '\t' + str(ithGen) + '\t' \
+ str(pID) + '\t' + str(eID) + '\t' \
+ longLabel + '\t' \
+ cleanTag(node.tag) + '\t' \
+ cleanTag(a) + '\t' + textString
outFh.write ( "%s\n" % outString )
outFlag = True
textString = quickStrip(str(node.text))
if ( len(textString)==0 or textString in naStrings ):
## if this node has no contents, we will still need to write
## out a line with a 'null' where the text would have been
## so that will be able to track the bundle identifiers
textString = ''
## print ( " DDDD : textString = %s " % textString )
## write out the text contents of this particular node
longLabel = ''
if ( len(genList) > 0 ): longLabel = ' > '.join(genList) + ' > '
longLabel += cleanTag(node.tag)
longLabel = longLabel.strip()
parentLabel = ''
if ( len(genList) > 0 ): parentLabel = genList[-1]
if ( (textString!='') or (not outFlag) ):
## print ( " DDDD : writing text {%s = %s} " % ( longLabel, textString ) )
outString = inputFilename + '\t' + str(ithGen) + '\t' \
+ str(pID) + '\t' + str(eID) + '\t' \
+ longLabel + '\t' \
+ parentLabel + '\t' \
+ cleanTag(node.tag) + '\t' + textString
outFh.write ( "%s\n" % outString )
## print ( " DDDD : leaving handleNodeData ... " )
# ----------------------------------------------------------------------------------------------------
def cleanTag(s):
t = s.strip()
u = t
i1 = t.find('{')
if ( i1 >= 0 ):
i2 = t.find('}')
if ( i2 > i1 ):
u = t[:i1] + t[i2+1:]
if ( 0 ):
if ( len(u) != len(t) ):
print ( " CLEANTAG ... <%s> ... <%s> " % ( t, u ) )
if ( u == "xsd_vexsd_ver" ):
print ( " WAIT UP ... in cleanTag ... <%s> ... <%s> ... <%s> " % ( s, t, u ) )
sys.exit(-1)
return (u)
# ----------------------------------------------------------------------------------------------------
# inspired by:
# https://stackoverflow.com/questions/28194703/recursive-xml-parsing-python-using-elementtree
def handleNextLevel(root,pID):
global ithGen, genList
ithGen += 1
genList += [ cleanTag(root.tag) ]
## genList += [ root.tag.strip() ]
## print ( " RRRR : in handleNextLevel ... ", ithGen, genList )
## loop over all children of the incoming node:
for elem in root:
## is this node just a "container" node with no information???
if ( isContainerNode(elem) ):
if ( ithGen <= maxDepth ):
## print ( " RRRR : moving on to next level ... " )
handleNextLevel(elem,pID)
else:
## generate a new identifier for this node
eID = uuid.uuid4()
## print ( " RRRR : generating new identifier ... ", eID )
handleNodeData(elem,pID,eID)
## if we haven't gone too deep yet...
if ( ithGen <= maxDepth ):
handleNextLevel(elem,eID)
## back up...
ithGen -= 1
genList = genList[:-1]
# ----------------------------------------------------------------------------------------------------
def main(args):
global inputFilename, outFh, maxDepth
## print ( args )
if ( not args.inputFile.endswith(".xml") ):
print ( " is this an XML file ??? <%s> " % args.inputFile )
sys.exit(-1)
inputFilename = args.inputFile
allowedModes = [ 'w', 'w+', 'a', 'a+' ]
if ( args.writeMode not in allowedModes ):
print ( " ERROR: invalid writeMode <%s> " % args.writeMode )
sys.exit(-1)
try:
outFh = open ( args.outputFile, args.writeMode )
except:
print ( " FAILED to open output file <%s> with <%s> " % ( args.outputFile, args.writeMode ) )
sys.exit(-1)
maxDepth = args.maxDepth
if ( args.colLabels.lower().startswith('t') ):
outString = 'inputXMLfile\txmlDepth\tparentID\tbundleID\tfull_label\tparent_label\telement_label\telement_text'
outFh.write ( "%s\n" % outString )
try:
xmlp = ET.XMLParser(encoding="latin-1")
tree = ET.parse(inputFilename,parser=xmlp)
except:
print ( " FAILED to parse input XML file ??? <%s> " % inputFilename )
sys.exit(-1)
## get the root of the tree
root = tree.getroot()
rootID = uuid.uuid4()
## handle any information that exists at the root level ...
handleNodeData(root,None,rootID)
## and then go to the next level (this is the recursive function)
handleNextLevel(root,rootID)
# ----------------------------------------------------------------------------------------------------
if __name__ == '__main__':
t0 = time.time()
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--inputFileName', action='store', help='input XML data file',
required=True, dest='inputFile', type=str)
parser.add_argument('-o', '--outputFileName', action='store', help='output TSV data file',
required=False, dest='outputFile', type=str)
parser.add_argument('-w', '--writeMode', action='store', help='output TSV data file',
required=False, dest='writeMode', type=str, default='w+')
parser.add_argument('-d', '--maxDepth', action='store', help='maximum depth to output TSV',
required=False, dest='maxDepth', type=int, default=99)
parser.add_argument('-l', '--colLabels', action='store', help='write column labels to output TSV',
required=False, dest='colLabels', type=str, default="True")
parser.add_argument('-s', '--schemaFileName', action='store', help='output JSON schema file',
required=False, dest='schemaFile', type=str)
args = parser.parse_args()
main(args)
# ----------------------------------------------------------------------------------------------------