2

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?

1 Answer 1

1

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.

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

1 Comment

Thanks for your answer. It works for me. CHMasterbook is a typo, and should be CHMasterBook.

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.