使用python如何在ElemenTree中获取所有特定属性

时间:2016-02-02 16:44:34

标签: python attributes elementtree

我正在尝试从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"

输出:

[]

0 个答案:

没有答案