-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml_to_json_parser.py
More file actions
35 lines (26 loc) · 1.12 KB
/
Copy pathxml_to_json_parser.py
File metadata and controls
35 lines (26 loc) · 1.12 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
import xml.etree.ElementTree as ET
class XmlToJsonParser:
def ConvertXmlToJsonUtil(self,node,res):
is_list = False
if node.text and node.text.strip() not in ['', '\n', [], {}]:
res[node.tag] = node.text
elif node.tag in res:
is_list = True
#This condition is hit when 2nd element of list node is processed
#Hence converting it to list
if type(res[node.tag])== dict:
res[node.tag] = [res[node.tag]]
#Empty dictionary is inserted for the resultant node to be filled using recursion
res[node.tag].insert(0,{})
else:
res[node.tag] = {}
for c in node.getchildren():
if is_list:
self.ConvertXmlToJsonUtil(c,res[node.tag][0])
else:
self.ConvertXmlToJsonUtil(c,res[node.tag])
return res
def ConvertXmlToJson(self,xml_string):
xml_node_obj = ET.fromstring(xml_string)
json_output = self.ConvertXmlToJsonUtil(xml_node_obj, {})
return json_output