Since ElementTree from stdlib provides only limited xpath support, you can use |
xpath OR operator only if you are using lxml
:
from lxml import etree as ET
data = """<?xml version="1.0"?>
<data>
<item>1</item>
<test>2</test>
</data>"""
tree = ET.fromstring(data)
for element in tree.xpath('//item|//test'):
print element.text
prints:
1
2
In case of xml.etree.ElementTree
you can combine the results of two separate findall()
calls:
for element in tree.findall('.//item') + tree.findall('.//test'):
print element.text
Or, check the tag name inside the loop:
for element in tree.iter():
if element.tag in ('item', 'test'):
print element.text