有没有一种方法可以使用python通过elementtree中的id属性过滤xml

时间:2019-12-29 11:27:46

标签: python python-3.x xml if-statement elementtree

我是python和elementtree的新手,正在尝试从xml过滤一些信息。我已经设法找到每个TX ID(坐标)所需的信息,但是我想将其过滤为Tx ID“ TxA”。 Ive包括了xml文件的一部分和下面的代码,并带有一些注释来帮助显示问题。任何帮助或指导,我们将不胜感激。

先前已设置所有列表(因此追加) 各节评论过,对所有Tx ID都有效,但是我现在回去尝试过滤 Subelm.attrib给出Tx ID。我已经在第5-8行的代码中展示了两次尝试。

XML的一部分:

<TX id="TxA">
  <Tx_WGS84_Longitude>-105.0846057</Tx_WGS84_Longitude>
  <Tx_WGS84_Latitude>42.9565772</Tx_WGS84_Latitude>
  <Tx_Easting>678133.8818</Tx_Easting>
  <Tx_Northing>895120.939</Tx_Northing>
  <Channel id="3">
    <Ant_Config> =</Ant_Config>
    <Ant_name>Tx_ant_name1</Ant_name>
    <Ant_Electrode_1>TxAEL1<Ant_Easting>678135.1069</Ant_Easting><Ant_Northing>895248.2057</Ant_Northing></Ant_Electrode_1>
    <Ant_Electrode_2>TxAEL2<Ant_Easting>678137.0213</Ant_Easting><Ant_Northing>891059.2502</Ant_Northing></Ant_Electrode_2>
  </Channel>
  <Channel id="1">
    <Ant_Config> =</Ant_Config>
    <Ant_name>Tx_ant_name2</Ant_name>
    <Ant_Electrode_1>TxAEL1<Ant_Easting>678135.1069</Ant_Easting><Ant_Northing>895248.2057</Ant_Northing></Ant_Electrode_1>
    <Ant_Electrode_2>TxAEL2<Ant_Easting>678137.0213</Ant_Easting><Ant_Northing>891059.2502</Ant_Northing></Ant_Electrode_2>
  </Channel>
</TX>
<TX id="TxB">
  <Tx_WGS84_Longitude>-105.08459550832</Tx_WGS84_Longitude>
  <Tx_WGS84_Latitude>42.9506068474998</Tx_WGS84_Latitude>
  <Tx_Easting>678135.4896</Tx_Easting>
  <Tx_Northing>893006.9206</Tx_Northing>
  <Channel id="3">
    <Ant_Config> =</Ant_Config>
    <Ant_name>Tx_ant_name1</Ant_name>
    <Ant_Electrode_1>TxBEL1<Ant_Easting>678135.6131</Ant_Easting><Ant_Northing>893055.2569</Ant_Northing></Ant_Electrode_1>
    <Ant_Electrode_2>TxBEL2<Ant_Easting>678138.3127</Ant_Easting><Ant_Northing>888854.3852</Ant_Northing></Ant_Electrode_2>
  </Channel>
  <Channel id="1">
    <Ant_Config> =</Ant_Config>
    <Ant_name>Tx_ant_name2</Ant_name>
    <Ant_Electrode_1>TxBEL1<Ant_Easting>678135.6131</Ant_Easting><Ant_Northing>893055.2569</Ant_Northing></Ant_Electrode_1>
    <Ant_Electrode_2>TxBEL2<Ant_Easting>678138.3127</Ant_Easting><Ant_Northing>888854.3852</Ant_Northing></Ant_Electrode_2>
  </Channel>

Python代码的一部分:

for child in root:
if child.tag=='Layout':
    for subelm in child:
        if subelm.tag=='TX':
            for name in subelm.iter('TxA'):
                print (subelm.attrib)
            if ('id' in subelm.attrib.text): 
                print (subelm.attrib.text)
                for channel in subelm:
                    for electrode in channel:
                        for electrode1 in electrode.iter('Ant_Electrode_1'):
                            for electrode1 in electrode.iter('Ant_Easting'):
                                x1t.append(electrode1.text)
                            for electrode1 in electrode.iter('Ant_Northing'):
                                y1t.append(electrode1.text)
                        for electrode2 in electrode.iter('Ant_Electrode_2'):
                            for electrode2 in electrode.iter('Ant_Easting'):
                                x2t.append(electrode2.text)
                            for electrode2 in electrode.iter('Ant_Northing'):
                                y2t.append(electrode2.text)

1 个答案:

答案 0 :(得分:0)

您可以只使用xpath表达式:

>>> from lxml import etree
>>> with open('data.xml') as fd:
...   doc = etree.parse(fd)
...
>>> matches = doc.xpath('//TX[@id="TxA"]')
>>> matches
[<Element TX at 0x7fbfdbf50370>]
>>> matches[0].find('Tx_WGS84_Longitude').text
'-105.0846057'