目前,我有一个XML文件。我想说,如果string是this
,则打印与this
关联的所有子元素。我已经记录了一些我尝试过的代码。我使用内置的元素树。
<commands>
<command name="this" type="out" major="0x1" minor="0x0">
<data bytes="1-0" descrip=" ID"></data>
<data bytes="3-2" descrip=" ID"></data>
<data bytes="5-4" descrip=" ID"></data>
<data bytes="7-6" descrip=" Code"></data>
<data bytes="12-8" descrip=" Revision"></data>
<data bytes="13" descrip=" Version"></data>
<data bytes="14" descrip=" Mask"></data>
<data bytes="15" descrip="Reserved"></data>
<data bytes="17-16" descrip=" Windows"></data>
<data bytes="19-18" descrip=" of Write Flush Addresses"></data>
</command>
</commands>
tree = ET.parse('command_details.xml')
root = tree.getroot()
for child in root:
if child.attrib['major'] == str(hex(int(major_bits[::-1], 2))) and child.attrib['minor'] == str(hex(int(minor_bits[::-1], 2))):
command_name = str(child.attrib['name'])
我基本上想深入了解并打印命令名称的子标签。