untangle is a tiny Python library for parsing XML documents, converting them into easy-to-use Python objects. It simplifies accessing data in XML files using dot notation for elements and dictionary-like access for attributes. The current version is 1.2.1, released in July 2022, and it has an infrequent release cadence.
pip install untangleVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to parse an XML string and access elements, attributes (using dictionary-like syntax), and text content (cdata) using dot notation. Note that sibling elements with the same tag are automatically grouped into a list.
Upgrade to untangle version 1.2.1 or higher. This version includes patches to prevent these SAX vulnerabilities.
Ensure your Python environment is 3.7 or newer. If you must use an older Python version (3.4-3.6), you will need to stick to `untangle` versions <= 1.1.1, but be aware of potential security vulnerabilities in those older releases.
Be mindful of this transformation when constructing your access paths. Test your expected access patterns carefully to avoid `AttributeError`.
Always append `.cdata` to the element object when you intend to retrieve its inner text content.
When dealing with elements that might appear multiple times, always be prepared to handle a list (e.g., `for item in parent.item:`). If you expect a single element, direct access (`parent.item`) works, but if the XML structure can vary, consider defensive coding (e.g., checking `isinstance(parent.item, list)` or using `parent.item[0]` if you only want the first).