pip install python-xml2dict
import xml2dict
The parse
function takes a string or a file as a parameter. All other
parameters are optional.
xml = '''
<list>
<items>
<item id="1">A</item>
<item id="2">B</item>
<item id="3">C</item>
<item id="4">D</item>
</items>
<date>1.1.2019</date>
</list>
'''
print(json.dumps(xml2dict.parse(xml), indent=4))
{
"list": {
"items": {
"item": [
{
"@id": "1",
"#text": "A"
},
{
"@id": "2",
"#text": "B"
},
{
"@id": "3",
"#text": "C"
},
{
"@id": "4",
"#text": "D"
}
]
},
"date": "1.1.2019"
}
}
Behaviour can be modified by defining custom processing functions, which may be applicable to one, more or all XML elements.
def get_key(obj, name, val, arg):
if name == 'id':
arg['key'] = val
else:
obj[tag] = val
def add_item(obj, tag, val, arg):
if tag not in obj:
obj[tag] = {arg['key']: val}
else:
obj[tag][arg['key']] = val
schema = [
{"elem": ["item"], "attr": get_key, "add": add_item}
]
print(json.dumps(xml2dict.parse(xml, schema), indent=4))
{
"list": {
"items": {
"item": {
"1": "A",
"2": "B",
"3": "C",
"4": "D"
}
},
"date": "1.1.2019"
}
}
If the processing function does not store the value into a dict, only the remaining items will be returned.
def print_item(obj, tag, val, arg):
print("ITEM", tag, val)
schema = [
{"elem": ["item"], "add": print_item}
]
print(json.dumps(xml2dict.parse(xml, schema), indent=4))
ITEM item {'@id': '1', '#text': 'A'}
ITEM item {'@id': '2', '#text': 'B'}
ITEM item {'@id': '3', '#text': 'C'}
ITEM item {'@id': '4', '#text': 'D'}
{
"list": {
"items": null,
"date": "1.1.2019"
}
}