-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeleteNodes.py
198 lines (156 loc) · 6.09 KB
/
deleteNodes.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
import datetime
from lxml import etree as ET
from pathlib import Path
##############################
### Methode to delete node ###
##############################
def strip_node(element):
text = element.xpath('string()')
# Add tail to text
text += (element.tail or '')
if text is not None:
# get parent node
parent = element.getparent()
# get previous node
previous = element.getprevious()
if previous is not None:
if previous.tail is not None:
previous.tail += text
else:
previous.tail = text
else:
if parent is not None:
parent.text = (parent.text or '') + text
else:
parent.text = text
# removes node
parent.remove(element)
print('Status: Program started, ', datetime.datetime.now())
text = ''
isXML = True
isPath = True
rightChoice = True
deleteByName = False
deleteByAttribute = False
deleteByBoth = False
try:
###########################
### Importpath xml file ###
###########################
# Test if path contains xmlFile
while isXML:
# Import file
importFile = input('Enter path to import file: ')
if '.xml' in str(importFile):
isXML = False
else:
print('No xml File detected, please try again. For further information see README.md.')
###########################
### Exportpath xml file ###
###########################
# Test if folder path or file path
while isPath:
# Export path
exportFolder = Path(input('Enter path to output folder: '))
if '.xml' in str(exportFolder):
print('Please enter a folder path, not a file path. For further information see README.md.')
else:
isPath = False
# Export file
exportFile = input('Enter output filename: ')
# if filename does not contain data format, add .xml
if '.xml' not in exportFile:
exportFile += '.xml'
# Open xml file
tree = ET.parse(importFile)
root = tree.getroot()
except FileNotFoundError as fnfError:
print(fnfError)
except ValueError as valueError:
print(valueError)
else:
try:
while rightChoice:
# Choice if delete node by node name or by attribute
choice = input('Do you want to delete a node by name, by attribute or by both? Enter \'1\' for node, \'2\' for attribute and \'3\' for both: ')
# Delete by name
if choice == '1':
rightChoice = False
deleteByName = True
nodeName = input('Enter node name: ')
addValue = input('Filter by node value? (y/n): ')
if addValue == 'y':
nodeValue = input('Enter node value to filter (part of value possible): ')
# Delete by attribute
elif choice == '2':
rightChoice = False
deleteByAttribute = True
attributeName = input('Enter attribute name: ')
addValue = input('Filter by attribute value? (y/n): ')
if addValue == 'y':
attributeValue = input('Enter attribute value to filter (part of value possible): ')
# Both
elif choice == '3':
rightChoice = False
deleteByBoth = True
nodeName = input('Enter node name: ')
attributeName = input('Enter attribute name: ')
withoutAttr = input('Node with attribute (1) or without the attribute (2): ')
addValue = input('Filter by attribute value? (y/n): ')
if addValue == 'y':
attributeValue = input('Enter attribute value to filter: ')
else:
print('Please enter \'1\', \'2\' or \'3\'. ')
except ValueError as valueError:
print(valueError)
else:
# delete by name
if deleteByName:
for element in tree.xpath('.//' + nodeName):
# attribute name and value
if addValue == 'y':
if nodeValue in element.text:
strip_node(element)
# only node name
else:
strip_node(element)
# delete by attribute
elif deleteByAttribute:
for element in tree.xpath('.//*[@' + attributeName + ']'):
# attribute name and value
if addValue == 'y':
if attributeValue in element.get(attributeName, ''):
strip_node(element)
# only attribute name
else:
strip_node(element)
# delete by name and attribute
elif deleteByBoth:
# delete node by name with attribute
if withoutAttr == '1':
for element in tree.xpath('.//' + nodeName + '[@' + attributeName + ']'):
# attribute with value
if addValue == 'y':
if attributeValue in element.get(attributeName, ''):
strip_node(element)
# attribute without value
else:
strip_node(element)
# delete node by name without attribute
elif withoutAttr == '2':
if addValue == 'y':
for element in tree.xpath('.//' + nodeName + '[not(@' + attributeName + ')]'):
strip_node(element)
for element in tree.xpath('.//' + nodeName + '[not(@' + attributeName + '="' + attributeValue + '")]'):
strip_node(element)
else:
for element in tree.xpath('.//' + nodeName + '[not(@' + attributeName + ')]'):
strip_node(element)
###########################
### Export new xml file ###
###########################
try:
tree.write(str(exportFolder / exportFile))
print('Status: ' + exportFile + ' exported ', datetime.datetime.now())
except FileNotFoundError as fnfError:
print(fnfError)