Xml-To-Dict
Sat 17 May 2025
title: "XML to Dictionary" author: "Rj" date: 2019-04-20 description: "List Test" type: technical_note draft: false
import xmltodict
import pprint
import json
xml_content = """
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
"""
xml_content
"\n <note>\n <to>Tove</to>\n <from>Jani</from>\n <heading>Reminder</heading>\n <body>Don't forget me this weekend!</body>\n </note>\n"
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(json.dumps(xmltodict.parse(xml_content)))
('{"note": {"to": "Tove", "from": "Jani", "heading": "Reminder", "body": '
'"Don\'t forget me this weekend!"}}')
dict = xmltodict.parse(xml_content)
dict
OrderedDict([('note',
OrderedDict([('to', 'Tove'),
('from', 'Jani'),
('heading', 'Reminder'),
('body', "Don't forget me this weekend!")]))])
dict['note']
OrderedDict([('to', 'Tove'),
('from', 'Jani'),
('heading', 'Reminder'),
('body', "Don't forget me this weekend!")])
dict['note']['to']
'Tove'
Score: 10
Category: basics