I have Googled my pants off to no avail. What I am trying to do is very simple: I'd like to access the UniqueID value in the following XML contained in a string using ElementTree.
from xml.etree.ElementTree import fromstring
xml_string = """<ListObjectsResponse xmlns='http://www.example.com/dir/'>
<Item>
<UniqueID>abcdefghijklmnopqrstuvwxyz0123456789</UniqueID>
</Item>
</ListObjectsResponse>"""
NS = "http://www.example.com/dir/"
tree = fromstring(xml_string)
I know that I should use the fromstring method to parse the XML string, but I can't seem to identify how to access the UniqueID. I'm not certain how to use the find, findall, or findtext methods with respect to the namespace.
Any help is totally appreciated.
The following should get you going:
>>> tree.findall('*/*')
[<Element '{http://www.example.com/dir/}UniqueID' at 0x10899e450>]
This lists all the elements that are two levels below the root of your tree (the UniqueID element, in your case). You can, alternatively, find only the first element at this level, with tree.find(). You can then directly get the text contents of the UniqueID element:
>>> unique_id_elmt = tree.find('*/*') # First (and only) element two levels below the root
>>> unique_id_elmt
<Element '{http://www.example.com/dir/}UniqueID' at 0x105ec9450>
>>> unique_id_elmt.text # Text contained in UniqueID
'abcdefghijklmnopqrstuvwxyz0123456789'
Alternatively, you can directly find some precise element by specifying its full path:
>>> tree.find('{{{0}}}Item/{{{0}}}UniqueID'.format(NS)) # Tags are prefixed with NS
<Element '{http://www.example.com/dir/}UniqueID' at 0x10899ead0>
As Tomalak indicated, Fredrik Lundh's site might contain useful information; you want to check how prefixes can be handled: there might in fact be a simpler way to handle them than by making explicit the NS path in the method above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With