Reputation: 863
I have some xml that I need to parse, which is supplied in the following format:
<Pres xmlns:abset="titan:arm:params:xml:ns:keyprov:abset">
<Set>
<Key>
<Id>c91e3882-e6f3-41f9-af52-3473a2c4615a</Id>
<abset:Data>
<abset:Tag>
<abset:Value>i need this</abset:Value>
</abset:Tag>
</abset:Data>
</Key>
</Set>
</Pres>
With Python 2.7, I can get at the abset:Value with the following:
xmlstr = '...'
root = ElementTree.fromstring(xmlstr)
ns = {'abset' : 'titan:arm:params:xml:ns:abset'}
keyElement = root.find("./Set/Key")
value = keyElement.find("./abset:Data/abset:Tag/abset:Value", ns).text
But in python 2.6, the find command doesn't support the ns argument.
I've tried ValElement = root.find("./Set/Key/abset:Data/abset:Tag/abset:Value") value = ValElement.text
But the error I get is
keyelement = root.find("./Set/Key/abset:Data/abset:Tag/abset:Value")
File "/usr/lib64/python2.6/xml/etree/ElementTree.py", line 330, in find
return ElementPath.find(self, path)
File "/usr/lib64/python2.6/xml/etree/ElementPath.py", line 186, in find
return _compile(path).find(element)
File "/usr/lib64/python2.6/xml/etree/ElementPath.py", line 176, in _compile
p = Path(path)
File "/usr/lib64/python2.6/xml/etree/ElementPath.py", line 93, in __init__
"expected path separator (%s)" % (op or tag)
SyntaxError: expected path separator (:)
How can I access these elements in python 2.6.6?
Upvotes: 1
Views: 529
Reputation: 213
You would need to specify the namespace in full, so instead of root.find("./Set/Key/abset:Data/abset:Tag/abset:Value")
you should use root.find("./Set/Key/{titan:arm:params:xml:ns}Data/{titan:arm:params:xml:ns}Tag/{titan:arm:params:xml:ns}Value")
Upvotes: 2