仅当其父属性在中为“ enabled = true”时,我才想从的文本中提取名称。我无法对此条件进行编码。 帮助将不胜感激。谢谢。
我正在使用
从“ Argument.name”中提取名称-用于root.findall('.// stringProp [@ name =“ Argument.name”]')
我正在使用以下方法从“ Argument.value”中提取值 -适用于root.findall('.// stringProp [@ name =“ Argument.name”] .. stringProp [@ name =“ Argument.value”]')
中的b我尝试过这样的事情: for root.findall('.//*[@ enabled =“ true] ..// stringProp [@ name =” Argument.value“]')
中的b
<Arguments enabled="true" guiclass="ArgumentsPanel" testclass="Arguments" testname="Network Variables">
<collectionProp name="Arguments.arguments">
<elementProp elementType="Argument" name="ir-ip-address">
<stringProp name="Argument.name">ir-ip-address</stringProp>
<stringProp name="Argument.value">10.97.57.164</stringProp>
<stringProp name="Argument.desc">ISP IP address</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
<elementProp elementType="Argument" name="ir-gui-port">
<stringProp name="Argument.name">ir-gui-port</stringProp>
<stringProp name="Argument.value">8001</stringProp>
<stringProp name="Argument.desc">ISP GUI port</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
</collectionProp>
</Arguments>
<Arguments enabled="false" guiclass="ArgumentsPanel" testclass="Arguments" testname="Network Variables">
<collectionProp name="Arguments.arguments">
<elementProp elementType="Argument" name="ir-ip-address">
<stringProp name="Argument.name">ir-ip-address</stringProp>
<stringProp name="Argument.value">10.97.57.164</stringProp>
<stringProp name="Argument.desc">ISP IP address</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
<elementProp elementType="Argument" name="ir-gui-port">
<stringProp name="Argument.name">ir-gui-port</stringProp>
<stringProp name="Argument.value">8001</stringProp>
<stringProp name="Argument.desc">ISP GUI port</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
</collectionProp>
</Arguments>
答案 0 :(得分:2)
您在这里:
(请注意,我已将<Argument>
包裹在<root>
中)
from xml.etree import ElementTree as ET
from xml.etree.ElementTree import ElementTree
if __name__ == '__main__':
raw_xml = '''
<root>
<Arguments enabled="true" guiclass="ArgumentsPanel" testclass="Arguments" testname="Network Variables">
<collectionProp name="Arguments.arguments">
<elementProp elementType="Argument" name="ir-ip-address">
<stringProp name="Argument.name">ir-ip-address</stringProp>
<stringProp name="Argument.value">10.97.57.164</stringProp>
<stringProp name="Argument.desc">ISP IP address</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
<elementProp elementType="Argument" name="ir-gui-port">
<stringProp name="Argument.name">ir-gui-port</stringProp>
<stringProp name="Argument.value">8001</stringProp>
<stringProp name="Argument.desc">ISP GUI port</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
</collectionProp>
</Arguments>
<Arguments enabled="false" guiclass="ArgumentsPanel" testclass="Arguments" testname="Network Variables">
<collectionProp name="Arguments.arguments">
<elementProp elementType="Argument" name="ir-ip-address">
<stringProp name="Argument.name">ir-ip-address</stringProp>
<stringProp name="Argument.value">10.97.57.164</stringProp>
<stringProp name="Argument.desc">ISP IP address</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
<elementProp elementType="Argument" name="ir-gui-port">
<stringProp name="Argument.name">ir-gui-port</stringProp>
<stringProp name="Argument.value">8001</stringProp>
<stringProp name="Argument.desc">ISP GUI port</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
</collectionProp>
</Arguments>
</root>
'''
root: ElementTree = ET.fromstring(raw_xml)
for arg in root.findall('Arguments[@enabled="true"]'):
argname = arg.find('.//stringProp[@name="Argument.name"]').text
argval = arg.find('.//stringProp[@name="Argument.value"]').text
print(argname, argval)
打印:
ir-ip-address 10.97.57.164