擅长:python、mysql、java
<p>您应该能够通过使用xpath <code>field[@key='TempF']</code>(当前元素上下文为<code>device</code>)来选择具有值为<code>TempF</code>的<code>field</code>属性的<code>key</code>元素。你知道吗</p>
<p>示例(将<code>feed</code>更改回urllib调用)。。。你知道吗</p>
<pre><code>def main():
feed = "test.xml" # Used an external file for testing.
try:
tree = ET.parse(feed)
root = tree.getroot()
devices = root.findall("devices/device")
for device in devices:
print device.get("name")
print device.find("field[@key='TempF']").get("value")
except Exception, inst:
print "Error: %s" % inst
</code></pre>
<p>这将打印:</p>
<pre class="lang-none prettyprint-override"><code>WeatherGoose
68.99
</code></pre>
<p>注意:如果您有多个<code>device</code>元素,这将迭代每个元素。你知道吗</p>