包xml.etree是否可以找到孩子的父母?例如:
<ELEMENTS>
<CONSTANT-SPECIFICATION>
</CONSTANT-SPECIFICATION>
</ELEMENTS>
<ELEMENTS>
<DATA-SPECIFICATION>
</DATA-SPECIFICATION>
</ELEMENTS>
我搜索包含Child“CONSTANT-SPECIFICATION”的对象“ELEMENTS”。
答案 0 :(得分:0)
您可以使用.//ELEMENTS[CONSTANT-SPECIFICATION]
XPath表达式,例如:
import xml.etree.ElementTree as ET
data = """<?xml version="1.0" encoding="ISO-8859-1"?>
<ROOT>
<ELEMENTS>
<CONSTANT-SPECIFICATION>
</CONSTANT-SPECIFICATION>
</ELEMENTS>
<ELEMENTS>
<DATA-SPECIFICATION>
</DATA-SPECIFICATION>
</ELEMENTS>
</ROOT>
"""
root = ET.fromstring(data)
print root.find('.//elements[constant-specification]')