使用python如何获取ElementTree中的所有特定属性
我试图从XML文件中获取所有使用python如何获取ElementTree中的所有特定属性,python,attributes,elementtree,Python,Attributes,Elementtree,我试图从XML文件中获取所有extWorkEquipmentId和TYPE属性。使用属性TYPE我可以通过函数元素.findall获得它。但我不知道如何将此应用于extWorkEquipmentd属性 我的xml文件: <?xml version="1.0" encoding="UTF-8"?> <asset:operation xmlns:asset="http://vds.com/assetTypes" timestamp="2015-06-23 08:36:54.129
extWorkEquipmentId
和TYPE
属性。使用属性TYPE
我可以通过函数元素.findall
获得它。但我不知道如何将此应用于extWorkEquipmentd
属性
我的xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<asset:operation xmlns:asset="http://vds.com/assetTypes" timestamp="2015-06-23 08:36:54.129">
<asset:modelref id="VDS_TIDWORTH">
<asset:request type="INSERT">
<asset:asset xmlns:asset="http://vds.com/assetTypes" extWorkEquipmentId="7663" extWorkEquipmentType="Esri.wHydrant" assetLabel="Hydrant #PI 4850">
<asset:relations>
<asset:relation relationName="ISA95_EquipmentRelationship.ContainedWithin" targetExtWorkEquipmentId="17" targetExtWorkEquipmentType="Esri.wPressureZone"/>
</asset:relations>
<asset:measureTypes>
<asset:measureType name="ENABLED" unit="">
<asset:threshold level="0" range="-0.5/0.5"/>
<asset:threshold level="1" range="0.5/1.5"/>
<asset:threshold level="2" range="1.5/2.5"/>e
</asset:measureType>
</asset:measureTypes>
<asset:attribute name="PRESSUREZONE" type="string">
<asset:value>E14</asset:value>
</asset:attribute>
<asset:attribute name="INSTALLDATE" type="string">
<asset:value>1986</asset:value>
</asset:attribute>
<asset:attribute name="TYPE" type="string">
<asset:value>Poteau incendie 100</asset:value>
</asset:attribute>
<asset:attribute name="DIAMETER" type="string">
<asset:value>100</asset:value>
<asset:unit>cm</asset:unit>
</asset:attribute>
<asset:attribute name="EXTERNALID" type="string">
<asset:value>PI 4850</asset:value>
</asset:attribute>
<asset:attribute name="DYNPRESSURE" type="string">
<asset:value/>
<asset:unit>bar</asset:unit>
</asset:attribute>
<asset:attribute name="STATICPRESSURE" type="string">
<asset:value/>
<asset:unit>bar</asset:unit>
</asset:attribute>
<asset:attribute name="ENABLED" type="string">
<asset:value>1</asset:value>
</asset:attribute>
</asset:asset>
<asset:asset xmlns:asset="http://vds.com/assetTypes" extWorkEquipmentId="7664" extWorkEquipmentType="Esri.wHydrant" assetLabel="Hydrant #PI 4850">
<asset:relations>
<asset:relation relationName="ISA95_EquipmentRelationship.ContainedWithin" targetExtWorkEquipmentId="17" targetExtWorkEquipmentType="Esri.wPressureZone"/>
</asset:relations>
<asset:measureTypes>
<asset:measureType name="ENABLED" unit="">
<asset:threshold level="0" range="-0.5/0.5"/>
<asset:threshold level="1" range="0.5/1.5"/>
<asset:threshold level="2" range="1.5/2.5"/>
</asset:measureType>
</asset:measureTypes>
<asset:attribute name="PRESSUREZONE" type="string">
<asset:value>E14</asset:value>
</asset:attribute>
<asset:attribute name="INSTALLDATE" type="string">
<asset:value>1986</asset:value>
</asset:attribute>
<asset:attribute name="TYPE" type="string">
<asset:value>Poteau incendie 101</asset:value>
</asset:attribute>
<asset:attribute name="DIAMETER" type="string">
<asset:value>100</asset:value>
<asset:unit>cm</asset:unit>
</asset:attribute>
<asset:attribute name="EXTERNALID" type="string">
<asset:value>PI 4850</asset:value>
</asset:attribute>
<asset:attribute name="DYNPRESSURE" type="string">
<asset:value/>
<asset:unit>bar</asset:unit>
</asset:attribute>
<asset:attribute name="STATICPRESSURE" type="string">
<asset:value/>
<asset:unit>bar</asset:unit>
</asset:attribute>
<asset:attribute name="ENABLED" type="string">
<asset:value>1</asset:value>
</asset:attribute>
</asset:asset>
<asset:asset xmlns:asset="http://vds.com/assetTypes" ex:q!
extWorkEquipmentType="Esri.wHydrant" assetLabel="Hydrant #PI 4850">
<asset:relations>
<asset:relation relationName="ISA95_EquipmentRelationship.ContainedWithin" targetExtWorkEquipmentId="17" targetExtWorkEquipmentType="Esri.wPressureZone"/>
</asset:relations>
<asset:measureTypes>
<asset:measureType name="ENABLED" unit="">
<asset:threshold level="0" range="-0.5/0.5"/>
<asset:threshold level="1" range="0.5/1.5"/>
<asset:threshold level="2" range="1.5/2.5"/>
</asset:measureType>
</asset:measureTypes>
<asset:attribute name="PRESSUREZONE" type="string">
<asset:value>E14</asset:value>
</asset:attribute>
<asset:attribute name="INSTALLDATE" type="string">
<asset:value>1986</asset:value>
</asset:attribute>
<asset:attribute name="TYPE" type="string">
<asset:value>Poteau incendie 102</asset:value>
</asset:attribute>
<asset:attribute name="DIAMETER" type="string">
<asset:value>100</asset:value>
<asset:unit>cm</asset:unit>
</asset:attribute>
<asset:attribute name="EXTERNALID" type="string">
<asset:value>PI 4850</asset:value>
</asset:attribute>
<asset:attribute name="DYNPRESSURE" type="string">
<asset:value/>
<asset:unit>bar</asset:unit>
</asset:attribute>
<asset:attribute name="STATICPRESSURE" type="string">
<asset:value/>
<asset:unit>bar</asset:unit>
</asset:attribute>
<asset:attribute name="ENABLED" type="string">
<asset:value>1</asset:value>
</asset:attribute>
</asset:asset>
</asset:request>
</asset:modelref>
</asset:operation>
输出:
<Element '{http://vds.com/assetTypes}operation' at 0x7f05c925c150> <type 'Element'>
{http://vds.com/assetTypes}modelref {'id': 'VDS_TIDWORTH'}
id
输出:
[]
您可以添加当前的python代码吗?tree=ET.ElementTree(file=os.path.join(os.getcwd(),fich))root=tree.getroot()print str(root),在root中为child键入(root):print child.tag,“,child.attrib中的i的child.attrib:print i
,此时我尝试在元素中循环tree=ET.ElementTree(file=os.path.join(os.getcwd(),fich))root=tree.getroot()打印str(root),为root中的子级键入(root):print child.tag,“,child.attrib代表child.attrib:print i
Hello@Mr_Pouet我把我的代码放在帖子里了。我也尝试了一些关于“名称空间”的东西,但目前没有结果。感谢您的帮助文档中没有类型
属性。但是,有值为TYPE
的name
属性。是否也可以添加当前的python代码?tree=ET.ElementTree(file=os.path.join(os.getcwd(),fich))root=tree.getroot()print str(root),TYPE(root)对于root中的child:print child.tag,“”,child.attrib代表child.attrib中的i:print i,此时我尝试在元素中循环tree=ET.ElementTree(file=os.path.join(os.getcwd(),fich))root=tree.getroot()打印str(root),为root中的子级键入(root):print child.tag,“,child.attrib代表child.attrib:print i
Hello@Mr_Pouet我把我的代码放在帖子里了。我也尝试了一些关于“名称空间”的东西,但目前没有结果。感谢您的帮助文档中没有类型
属性。但是,有值为TYPE
的name
属性。
tree = ET.parse(File)
namespaces = {'asset': 'http://vds.com/assetTypes'}
root = tree.getroot()
try:
callevent=root.findall('asset:operation', namespaces)
print callevent
except:
print "Oops"
[]