Computer >> Computer tutorials >  >> Programming >> Python

How to create Python objects based on an XML file?


Untangle

We can use untangle to create Python objects based on an XML file

untangle is a simple library which takes an XML document and returns a Python object which mirrors the nodes and attributes in its structure.

For example, an XML file like this −

<?xml version="1.0"?>
<root>
    <child name="child1">
</root>

can be loaded like this −

import untangle
obj = untangle.parse('path/to/file.xml')

and then you can get the child elements name like this −

obj.root.child['name']

untangle also supports loading XML from a string or an URL.