import xml.etree.ElementTree as ET
from StringIO import StringIO
# sample xml
s = '<root><mytag1>hello</mytag1><mytag2>world!</mytag2><tag3>nothing</tag3></root>'
tree = ET.parse(StringIO(s))
root = tree.getroot()
# using getchildren() within root and check if tag starts with keyword
print [node.text for node in root.getchildren() if node.tag.startswith('mytag')]
由于xpath支持在xml中相当有限,一种替代方法是使用getchildren()并返回带有标记startswith的节点:
结果:
^{pr2}$相关问题 更多 >
编程相关推荐