1

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.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • It's not a `System` tag, it's a `{http://schemas.microsoft.com/win/2004/08/events/event}System` tag. Namespaces matter. – Charles Duffy Jul 12 '16 at 14:10
  • Likely duplicative of http://stackoverflow.com/questions/21127119/find-an-element-in-an-xml-tree-using-elementtree – Charles Duffy Jul 12 '16 at 14:15
  • ...edited the reproducer to make it fully copy/paste friendly (previously, the documented needed to be fixed up before it would parse, thus not being verifiable). – Charles Duffy Jul 12 '16 at 14:19

1 Answers1

2

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>
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441