How to convert XML to JSON in Python?

Posted by Geuis on Stack Overflow See other posts from Stack Overflow or by Geuis
Published on 2009-01-23T05:12:30Z Indexed on 2010/03/27 0:23 UTC
Read the original article Hit count: 836

Filed under:
|
|
|
|

I'm doing some work on App Engine and I need to convert an XML document being retrieved from a remote server into an equivalent JSON object.

I'm using xml.dom.minidom to parse the XML data being returned by urlfetch. I'm also trying to use django.utils.simplejson to convert the parsed XML document into JSON. I'm completely at a loss as to how to hook the two together. Below is the code I more or less have been tinkering with. If anyone can put A & B together, I would be SO greatful. I'm freaking lost.

from xml.dom import minidom
from django.utils import simplejson as json

#pseudo code that returns actual xml data as a string from remote server. 
result = urlfetch.fetch(url,'','get');

dom = minidom.parseString(result.content)
json = simplejson.load(dom)

self.response.out.write(json)

© Stack Overflow or respective owner

Related posts about Xml

Related posts about JSON