-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmlparser.py
35 lines (25 loc) · 909 Bytes
/
xmlparser.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
from xml.dom import minidom
def add(_graph, node, neighbour, distance):
try:
_graph[node].update({neighbour: int(distance)})
except KeyError:
_graph[node] = {}
_graph[node].update({neighbour: int(distance)})
def xml_to_dictionary(xml):
table = []
graph = {}
xmldoc = minidom.parse(xml)
rows = xmldoc.getElementsByTagName('Row')
for each in range(0, len(rows)):
table.append([])
for x, row in enumerate(rows):
cells = row.getElementsByTagName('Cell')
for y, cell in enumerate(cells):
value = cell.childNodes[0].nodeValue
table[x].append(str(value))
for i, item in enumerate(table[0]):
for data in range(1, len(table)):
if table[data][i + 1] != '-':
add(graph, item, table[data][0], table[data][i + 1])
return graph
print xml_to_dictionary('table.xml')