使用python如何获取元素中的所有特定属性

2024-10-01 00:32:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从XML文件中获取所有extWorkEquipmentIdTYPE属性。通过属性TYPE,我可以通过函数element.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>

我的python代码:

        tree = ET.ElementTree(file=os.path.join(os.getcwd(),fich))
        root = tree.getroot()
        print str(root), type(root)

        for child in root:
            print child.tag, " ", child.attrib
            for i in child.attrib:
                print i

输出:

<Element '{http://vds.com/assetTypes}operation' at 0x7f05c925c150> <type 'Element'>
{http://vds.com/assetTypes}modelref   {'id': 'VDS_TIDWORTH'}
id

我试图设置名称空间,但是dict是空的。。。你知道吗

    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"

输出:

[]

Tags: namecomhttpstringthresholdvaluetypeunit