xmltodict()
Last updated
Last updated
<?xml version = "1.0" encoding = "utf-8"?>
<!-- xslplane.1.xml -->
<?xml-stylesheet type = "text/xsl" href = "xslplane.1.xsl" ?>
<plane>
<year> 1977 </year>
<make> Cessna </make>
<model> Skyhawk </model>
<color> Light blue and white </color>
</plane>
#import module
import xmltodict
#open the file
fileptr = open("/home/aditya1117/askpython/plane.xml","r")
#read xml content from the file
xml_content= fileptr.read()
print("XML content is:")
print(xml_content)
#change xml format to ordered dict
my_ordered_dict=xmltodict.parse(xml_content)
print("Ordered Dictionary is:")
print(my_ordered_dict)
print("Year of plane is:")
print(my_ordered_dict['plane']['year'])
#Use contents of ordered dict to make python dictionary
my_plane= dict(my_ordered_dict['plane'])
print("Created dictionary data is:")
print(my_plane)
print("Year of plane is")
print(my_plane['year'])
Output
XML content is:
<?xml version = "1.0" encoding = "utf-8"?>
<!-- xslplane.1.xml -->
<?xml-stylesheet type = "text/xsl" href = "xslplane.1.xsl" ?>
<plane>
<year> 1977 </year>
<make> Cessna </make>
<model> Skyhawk </model>
<color> Light blue and white </color>
</plane>
Ordered Dictionary is:
OrderedDict([('plane', OrderedDict([('year', '1977'), ('make', 'Cessna'), ('model', 'Skyhawk'), ('color', 'Light blue and white')]))])
Year of plane is:
1977
Created dictionary data is:
{'year': '1977', 'make': 'Cessna', 'model': 'Skyhawk', 'color': 'Light blue and white'}
Year of plane is
1977
def xml_to_dict(xml, listpaths=None, **kwargs):
listpaths = listpaths or []
opts = dict(
attr_prefix = '',
dict_constructor = dict,
postprocessor = lambda p,k,d: (k.split(':')[1] if ':' in k else k,d)
)
opts.update(kwargs)
content = xmltodict.parse(xml, **opts)
for listpath in listpaths:
pathdata = rget(content, listpath, [])
pathdata = pathdata if isinstance(pathdata, list) else [pathdata]
rset(content, listpath, pathdata)
return content
xml = """
... <mydocument has="an attribute">
... <and>
... <many>elements</many>
... <many>more elements</many>
... </and>
... <plus a="complex">
... element as well
... </plus>
... </mydocument>
... """
xml_to_dict(xml)
output
{'mydocument': {'has': 'an attribute',
'and': {'many': ['elements', 'more elements']},
'plus': {'a': 'complex', '#text': 'element as well'}}}
import xmltodict
import pprint
import json
my_xml = """
<audience>
<id what="attribute">123</id>
<name>Shubham</name>
</audience>
"""
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(json.dumps(xmltodict.parse(my_xml)))
Output
Reference : https://www.journaldev.com/19392/python-xml-to-json-dict