I wish to convert several Json entries to XML.
My Json file looks like this
{
"list": [
{"id":1,"author":"abc","title":"xyz"},
{"id":2,"author":"def","title":"mno"}
]
}
The code I'm using right now :
import json as j
import xml.etree.cElementTree as e
with open('data.json') as data_file:
data = j.load(data_file)
for obj in data['list']:
r = e.Element("sequence")
e.SubElement(r,"id").text = obj["id"]
e.SubElement(r,"submitter").text = obj["submitter"]
e.SubElement(r,"authors").text = str(obj["authors"])
e.SubElement(r,"title").text = str(obj["title"])
a = e.ElementTree(r)
a.write("json_to_xml.xml")
I need something like a.append() instead of a.write() since it's being overwritten every time and I get only 1 result at the end. Is there anything like that?
I need the output to look like this
<sequence>
<id>1</id>
<author>abc</author>
<title>xyz</title>
</sequence>
<sequence>
<id>2</id>
<author>def</author>
<title>mno</title>
</sequence>