Bart Friederichs
Bart Friederichs

Reputation: 33491

ElementTree cannot find element

I have an XML document, for which I'm including a sufficient subset in the reproducer below, for which tree.find() returns no results:

import xml.etree.ElementTree as ET

xml_str = '''
<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
   <System/>
</Event>
'''

tree = ET.fromstring(xml_str)
system = tree.find('System')              

I expect system to hold the <System> tag now, but it is None. Am I missing something here?

When I used array indices (like tree[0][0]) it did work.

Upvotes: 1

Views: 1508

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295281

Use the namespace in your search:

>>> doc.find('{http://schemas.microsoft.com/win/2004/08/events/event}System')
<Element {http://schemas.microsoft.com/win/2004/08/events/event}System at 0x10167e5a8>

Upvotes: 2

Related Questions