A simple function to convert a headerless XML string into a dictionary using only simplejson and re.
1 2 3 4 5 6 7 8 9 10 11 12 | import re
import simplejson as json
def xml2dict(xml, jsonString = ''):
tags, keys = re.findall('</?[A-Za-z0-9]+>',xml), []
for tag in tags: keys.append(re.sub('[</>]+','',tag))
for index in range(len(tags)-1):
jsonString += {'<><>': '"'+keys[index]+'": {',
'<></>': '"'+keys[index]+'": "'+xml[xml.find(tags[index])+len(tags[index]):xml.find(tags[index+1])]+'"',
'</><>': ', ',
'</></>': '}'}[tags[index].replace(keys[index],'')+tags[index+1].replace(keys[index+1],'')]
return json.loads('{%s}' % jsonString)
|
Either I don't get how this is supposed to work, or it simply doesn't work:
I'd expect something like {'a': 'foo\nbar'}. It cannot handle attributes either:
I'd use ElementTree instead.