untangle has a very simple API. You just need to call the parse function to get back a Python object. The parameter can be:
- a string
- a filename
- a URL
Interprets the given string as a filename, URL or XML data string, parses it and returns a Python object which represents the given document.
ValueErrorif the argument is None / empty string.
xml.sax.SAXParseExceptionif something goes wrong during parsing.
If you are looking for information on a specific function, class or method, this part of the documentation is for you.
The object you get back represents the complete XML document. Child elements can be accessed with
parent.child, attributes with
element['attribute']. Siblings with similar names are grouped into a list.
Considering this XML document:
<?xml version="1.0"?> <root> <child name="child1"/> </root>
and assuming it’s available in a variable called xml, we could use untangle like this:
doc = untangle.parse(xml) child_name = doc.root.child['name'] # 'child1'
For text/data inbetween tags, this is described as cdata. After specifying the relevant element as explained above, the data/cdata can be accessed by adding ”.cdata” (without the quotes) to the end of your dictionary call.
For more examples, have a look at (and launch) examples.py.
It is recommended to use pip, which will always download the latest stable release:
pip install untangle
untangle works with Python versions 2.4, 2.5, 2.6, 2.7, 3.0, 3.1, 3.2 and pypy
untangle is available for that use case, where you have a 20-line XML file you got back from an API and you just need to extract some values out of it. You might not want to use regular expressions, but just as well you might not want to install a complex libxml2-based solution (and look up its terse API).
Performance and memory usage might be bad, but these tradeoffs were made in order to allow a simple API and no external dependencies. See also: Limitations.
untangle trades features for a simple API, which is why untangle substitutes
<foobar><foo-bar/></foobar>can be accessed with
<foo.bar.baz/>can be accessed with
<foo:bar><foo:baz/></foo:bar>can be accessed with
Be aware that with certain characters or maybe also depending on the python version you might get an error on accessing specific attributes, such as
UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 385: ordinal not in range(128)
In most cases it should be enough to import the sys module, and set utf-8 as encoding, with:
import sys reload(sys) # just to be sure sys.setdefaultencoding('utf-8')