简体   繁体   中英

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM