How to convert xml content into json using xmltodict

xmltodict is a python package which is used to convert XML data into JSON and vice versa.

Within a less span of time, we can also process large amounts of data and get the desired results.

Installation:

    pip install xmltodict

It supports both python2.7, 3.4 versions

Advantages over other packages:

    1. No need to process each node in XML document to get the results
    2. Easy to convert complex XML data into JSON
    3. It's user-friendly

Converting XML to a dictionary:

>>> import xmltodict

>>> import json

>>> json.loads(json.dumps(xmltodict.parse('''

     <root>

       <persons city="hyderabad">

         <person name="abc">

           <name age="50" mobile="789" />

         </person>

       </persons>

       <persons city="vizag">

            <username></username>

         <person name="xyz">

           <name age="70" mobile="123" />

         </person>

       </persons>

     </root>

     ''')))

{u'root': {u'persons': [{u'@city': u'hyderabad', u'person': {u'@name': u'abc', u'name': {u'@mobile': u'789', u'@age': u'50'}}}, {u'@city': u'vizag', u'person': {u'@name': u'xyz', u'name': {u'@mobile': u'123', u'@age': u'70'}}}]}}

Converting dictionary to XML:

>>> xmltodict.unparse({u'root': {u'persons': [{u'@city': u'hyderabad', u'person': {u'@name': u'abc', u'name': {u'@mobile': u'789', u'@age': u'50'}}}, {u'@city': u'vizag', u'person': {u'@name': u'xyz', u'name': {u'@mobile': u'123', u'@age': u'70'}}}]}}

)

u'<?xml version="1.0" encoding="utf-8"?>\n<root><persons city="hyderabad"><person name="abc"><name mobile="789" age="50"></name></person></persons><persons city="vizag"><person name="xyz"><name mobile="123" age="70"></name></person></persons></root>'

Text values for nodes can be specified with the `cdata_key` key in the python dict, while node properties can be specified with the `attr_prefix` prefixed to the key name in the python dict. The default value for `attr_prefix` is `@` and the default value for `cdata_key` is `#text`.


By passing `process_namespaces=True` will make it expand namespaces for xml data in json data.

>>> import xmltodict

>>> import json

>>> json.loads(json.dumps(xmltodict.parse('''

     <root>

       <persons city="hyderabad">

         <person name="abc">

           <name age="50" mobile="789" />

         </person>

       </persons>

       <persons city="vizag">

         <person name="xyz">

           <name age="70" mobile="123" />

         </person>

       </persons>

     </root>

     ''', process_namespaces=True)))

>>> mydict = {u'root': {u'persons': [{u'@city': u'hyderabad', u'person': {u'@name': u'abc', u'name': {u'@mobile': u'789', u'@age': u'50'}}}, {u'@city': u'vizag', u'person': {u'@name': u'xyz', u'name': {u'@mobile': u'123', u'@age': u'70'}}}]}}

>>> print xmltodict.unparse(mydict, pretty=True)

To Know more about our Django CRM(Customer Relationship Management) Open Source Package. Check Code

Posted On 08 May 2017 By MicroPyramid


Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Understanding Django model formsets in detail and their advanced usage.

Silmilar to the regular formsets, django also provide model formset that make it easy to work with django models. Django model formsets provide a way ...

Continue Reading...
Django efficient implementation of Amazon s3 and Cloudfront CDN for faster loading.

Django by default to store the files in your local file system. To make your files load quickly and secure we need to go for ...

Continue Reading...
Introduction to API development using Django REST framework with Example

Introduction to API development with Django REST framework. You can build the API for any Django application. Pre-requisites are Django and OOPS(object oriented programming concepts) ...

Continue Reading...
open source packages

Subscribe To our news letter

Subscribe and Stay Updated about our Webinars, news and articles on Django, Python, Machine Learning, Amazon Web Services, DevOps, Salesforce, ReactJS, AngularJS, React Native.
* We don't provide your email contact details to any third parties