Reputation: 507
I'm using Python2.5, ElementTree 1.2 to parse XML document, which looks like:
<cm:CompositeMessage xmlns:cm="http://www.xyz.com">
<cm:Message>
<cm:Body format="text/xml">
<CHMasterbook >
<event>
<eventName>Snapshot</eventName>
<date>2013-10-25</date>
<time>20:59:02</time>
</event>
</CHMasterbook>
</cm:Body>
</cm:Message>
</cm:CompositeMessage>
After I register the namespace
ET._namespace_map['http://www.xyz.com'] = 'cm'
I can parse the XMLdocument and locate the 'event' node
tree = ElementTree(fromstring(xml))
tree.findall('./{http://www.xyz.com}Message/{http://www.xyz.com}Body/CHMasterBook/event')
But if 'CHMasterbook' node has namespaces like
<CHMasterbook xmlns="http://uri.xyz.com/Chorus/Message" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://uri.xyz.com/Chorus/Message ../schema/chorus-master-book-msg.xsd">
tree.findall only returns empty list and it can no longer locate 'event' node. I also tried to register those namespaces like:
ET._namespace_map['http://uri.xyz.com/Chorus/Message'] = 'xmlns'
ET._namespace_map['http://www.w3.org/2001/XMLSchema-instance'] = 'xmlns:xsi'
ET._namespace_map['http://uri.xyz.com/Chorus/Message ../schema/chorus-master-book-msg.xsd'] = 'xsi:schemaLocationi'
But it didn't help.
I can only use Python 2.5 and ElementTree 1.2 (can't use lxml). Does anyone know how to locate the 'event' node with 'CHMasterbook' having those namespaces?
Upvotes: 2
Views: 340
Reputation: 20748
Try this:
tree = ElementTree(fromstring(xml))
tree.findall('./{http://www.xyz.com}Message'
'/{http://www.xyz.com}Body'
'/{http://uri.xyz.com/Chorus/Message}CHMasterbook'
'/{http://uri.xyz.com/Chorus/Message}event')
In your example, you use CHMasterbook
and sometimes CHMasterBook
. Remember case is important in XML.
Upvotes: 1