blob: 57ceebd12857be575de73b475100b13c938a80e1 (
plain)
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
|
import xml.etree.cElementTree as ET
class INLXmlParser:
def __init__(self, lst, path):
self.whitelist = lst
self.xmlpath = path
def clearxml(self):
xmltree = ET.parse(self.xmlpath)
# root == list of records
root = xmltree.getroot()
# create new data
newTreeRoot = ET.Element('data')
# scan the datafields in the records and copy to the new one only the tags in the whitelist
for record in root:
# create new record
newRecord = ET.SubElement(newTreeRoot, 'record')
for field in record:
fieldtag = field.attrib.get('tag')
if fieldtag in self.whitelist:
newFieldTag = fieldtag
# tag 700 and 400 are the same
if newFieldTag == '700':
newFieldTag = '400'
newTag = ET.SubElement(newRecord, 'datafield', {'tag': newFieldTag})
for data in field:
subData = ET.SubElement(newTag, data.tag, data.attrib)
subData.text = data.text
newTree = ET.ElementTree(newTreeRoot)
newTree.write('C:/Users/Ilsar/Documents/datahack/outTest.xml')
return newTree
|