I made a Python function that takes an XML file as a parameter and returns a JSON. My goal is to convert some XML get from an old API to convert into Restful API.
This function works, but it is really ugly. I try to clean everything, without success.
Here are my tests:
def test_one_node_with_more_than_two_children(self):
xml = '<a id="0" name="foo"><b id="00" name="moo" /><b id="01" name="goo" /><b id="02" name="too" /></a>'
expected_output = {
"a": {
"@id": "0",
"@name": "foo",
"b": [
{
"@id": "00",
"@name": "moo"
},
{
"@id": "01",
"@name": "goo"
},
{
"@id": "02",
"@name": "too"
}
]
}
}
self.assertEqual(expected_output, self.parser.convertxml2json(xml))
And my function:
from xml.dom.minidom import parseString, Node
class Parser(object):
def get_attributes_from_node(self, node):
attributes = {}
for attrName, attrValue in node.attributes.items():
attributes["@" + attrName] = attrValue
return attributes
def convertxml2json(self, xml):
parsedXml = parseString(xml)
return self.recursing_xml_to_json(parsedXml)
def recursing_xml_to_json(self, parsedXml):
output_json = {}
for node in parsedXml.childNodes:
attributes = ""
if node.nodeType == Node.ELEMENT_NODE:
if node.hasAttributes():
attributes = self.get_attributes_from_node(node)
if node.hasChildNodes():
attributes[node.firstChild.nodeName] = self.recursing_xml_to_json(node)[node.firstChild.nodeName]
if node.nodeName in output_json:
if type(output_json[node.nodeName]) == dict:
output_json[node.nodeName] = [output_json[node.nodeName]] + [attributes]
else:
output_json[node.nodeName] = [x for x in output_json[node.nodeName]] + [attributes]
else:
output_json[node.nodeName] = attributes
return output_json
Can someone give me some tips to improve this code?
def recursing_xml_to_json(self, parsedXml)
is really bad.
I am ashamed to produce it :)