1

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?

1 Answer 1

2

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")

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.