💻
Code Snippet
  • Overview
  • General
    • Anaconda
  • GUI
    • PyQT
      • Qt Design
  • Pandas
    • Read Data
    • Replace
  • Articles
    • Python. PyQt
    • Offline Payment Wallet With Django
    • Documentation Encrypt File
    • Play With Pillow
  • Fontend
    • Snippet
    • Hugo
    • JavaScript
      • Form Validation
  • Finance
    • Library
      • yfinance
  • Notebook
    • Untitled
    • Snippet
  • Python
    • Download file
    • Date and Time
    • Snippet
    • Compile .exe
    • Overview
    • Google
      • Samples for Google Workspace
      • Drive
      • GoogleSheet
    • Virtual environment
    • Database
      • Pickle()
    • Datatypes
      • Excel
      • Dictionary
        • xmltodict()
    • File Handling
      • shutil()
      • Get the File Name
      • Get the Full Path
      • Check the File Size
      • Get File Creation Date
      • Find All File
        • Untitled
    • Dictionary
      • Convert Two Lists
  • Data Science
    • HTTP requests
  • Google Workspace
    • Overview
    • Apps Script
      • ์Note
      • Overview
      • Snippet
        • HTML Service
        • Fetch API
      • Quickstart
      • Google Sheets
        • Overview
          • Snippet
        • Fundamentals
          • Macros & Custom Functions
          • Spreadsheets, Sheets, and Ranges
          • Working with Data
          • Data Formatting
          • Chart and Present Data
        • Built-in Google Services
        • Fetch and format API data
        • Connected Sheets
  • Git
  • Mini Lab
    • Line
    • Python
  • Function
    • Python
      • Date&Time
  • Database
    • SQLite
      • Example
Powered by GitBook
On this page
  • Read XML data into a Python dictionary
  • Function XML to Dict
  • ศึกษาเพิ่มเติม

Was this helpful?

  1. Python
  2. Datatypes
  3. Dictionary

xmltodict()

PreviousDictionaryNextFile Handling

Last updated 3 years ago

Was this helpful?

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

#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

Function XML to Dict

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
https://www.askpython.com/python-modules/xmltodict-module
https://www.journaldev.com/19392/python-xml-to-json-dict