繁体   English   中英

有没有办法使用python通过elementtree中的id属性过滤xml

[英]Is there a way to filter xml by an id attribute in elementtree using python

我是 python 和 elementtree 的新手,我正在尝试从 xml 中过滤一些信息。 我设法找到了每个 TX id(坐标)所需的信息,但是我想将其过滤为仅 Tx id“TxA”。 我已经包含了 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)

您可以只使用 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'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM