<p>您只需为名称空间(oe)指定一个名称,并从它们的属性中匹配键值对。在</p>
<p>我使用<strong>xml</strong>模块的示例:</p>
<pre><code>import xml.etree.ElementTree as ET
s = '''<?xml version="1.0" encoding="UTF-8"?>
<Environment
xmlns="http://schemas.dmtf.org/ovf/environment/1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:oe="http://schemas.dmtf.org/ovf/environment/1"
xmlns:ve="http://www.vmware.com/schema/ovfenv"
oe:id=""
ve:vCenterId="vm-61">
<PlatformSection>
<Kind>VMware ESXi</Kind>
<Version>5.5.0</Version>
<Vendor>VMware, Inc.</Vendor>
<Locale>en</Locale>
</PlatformSection>
<PropertySection>
<Property oe:key="ppEnv" oe:value="production"/>
<Property oe:key="pphostname" oe:value="coolhostname"/>
</PropertySection>
<ve:EthernetAdapterSection>
<ve:Adapter ve:mac="00:50:56:94:9a:56" ve:network="Service" ve:unitNumber="7"/>
</ve:EthernetAdapterSection>
</Environment>'''
tree = ET.fromstring(s)
oe = '{http://schemas.dmtf.org/ovf/environment/1}'
for node in tree.iter(oe+'Property'):
if node.attrib[oe+'key'] == 'pphostname':
print node.attrib[oe+'value']
</code></pre>
<p>结果:</p>
^{pr2}$