XML



Treat XML like Python Dictionaries with xmltodict



• Easily work with XML data
• Convert from XML → Dict* and back

pip install xmltodict



>>> import xmltodict
>>> from pprint import pprint

>>> xml_example = open("xml_example.xml").read()
>>> pprint(xml_example)

# Convert XML to Dictionary
>>> xml_dict = xmltodict.parse(xml_example)

>>> int_name = xml_dict["interface"]["name"]
>>> int_name

# Convert Dictionary to XML
>>> xmltodict_unparse(xml_dict)



• xml_example.xml

<?xml version="1.0" encoding="UTF-8" ?>
<interface xmlns="ietf-interfaces">
  <name>GigabitEthernet2</name>
  <description>Wide Area Network</description>
  <enabled>true</enabled>
  <ipv4>
    <address>
      <ip>172.16.0.2</ip>
      <netmask>255.255.255.0</netmask>
    </address>
  </ipv4>
</interface>


• Python inlcudes a native Markup (html/xml) interfaces as well
• More powerful, but also more complex

Index