# xmltodict()

![](https://827126969-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MeBc39FMasExFe0Z9Sx%2F-MezjP9LlmEYUGlMLOfE%2F-Mezqnq2REJHd5iOD8qg%2Fimage.png?alt=media\&token=1606abca-b1c2-4661-a32e-abf51f9eee73)

```markup
<?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>
```

### Read XML data into a Python dictionary

```python
#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

```markup
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
```

### Function XML to Dict

```python
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 
```

```python
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

```bash
{'mydocument': {'has': 'an attribute',
  'and': {'many': ['elements', 'more elements']},
  'plus': {'a': 'complex', '#text': 'element as well'}}}
```

```bash
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

![](https://827126969-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MeBc39FMasExFe0Z9Sx%2F-MezyqG87NAiYGIZbaBd%2F-Mf-2zHoPKOgVgoKQ_3-%2Fimage.png?alt=media\&token=c39444bb-1abd-42b9-845c-04ab7690a5d6)

Reference : <https://www.journaldev.com/19392/python-xml-to-json-dict>

### ศึกษาเพิ่มเติม

* <https://www.askpython.com/python-modules/xmltodict-module>
* <https://www.journaldev.com/19392/python-xml-to-json-dict>
