-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml_parser.py
More file actions
45 lines (43 loc) · 1.34 KB
/
xml_parser.py
File metadata and controls
45 lines (43 loc) · 1.34 KB
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
## XML Parser using ElementTree-Api in Python
# TO run this code use : python3 filenname
import xml.etree.ElementTree as ET
import subprocess as SP
path = SP.getoutput('pwd')
tree = ET.parse(path+'/test/country_data.xml')
root = tree.getroot()
dict={}
val = "0" # index value
tmp= str(root.tag) # tmp making indexes unqiue
dict[tmp]="0" # stroing all indexes in dicitonary
element_list=[]
#print (root.tag,root.attrib)
def parse_xml (root,val,tmp): #Recursive Function Print out elements and indexes
if root is None :
return
val+= ".0"
count = 0
for child in root:
if child.tag is not None:
if count == 0:
tmp+="_"
tmp+=str(child.tag)
else:
ind = tmp.rfind('_')
len_del = len(tmp)-ind-1
tmp =tmp[:-len_del]
tmp+=str(child.tag)
tmp+=str(count)
dict[tmp]=val
print(child.tag,child.attrib)
parse_xml(child,val,tmp)
count=count+1
ind=val.rfind('.')
len_del = len(val)-ind-1
val=val[:-len_del]
val+=str(count)
#if child.attrib is not None:
# print(child.attrib,end='')
#if child.text is not None:
# print(child.text,end='')
parse_xml(root,val,tmp)
print(dict)