将xpath与lxml.etree._ElementTree一起使用

时间:2015-07-15 06:31:32

标签: python xpath lxml

我有一个对象arptree,我试图找到一个ip-address的mac-address,但是失败了。

>>> arptree 
<lxml.etree._ElementTree object at 0x0000000004641688>

当我尝试以下xpath时,它会返回一个空列表

>>> arptree.xpath("descendant::mac-address[following-sibling::ip-address='10.69.119.150']")
[]

如果我修改xpath以排除“= '10 .69.119.150'”,它实际上会返回一个元素列表。

>>> arptree.xpath("descendant::mac-address[following-sibling::ip-address]")                      

[,,,,,,,,,,

我可以使用for循环来访问内容。我确信ip-address 10.69.119.150的mac-address是存在的。

for elt in arptree.iter():
    print elt.tag, elt.text

奇怪的是,如果我将xml输出复制并粘贴到xml文件中。 然后使用:

from lxml import etree
tree = etree.parse(open('arp.xml'))
tree.xpath("descendant::mac-address[following-sibling::ip-address='10.69.119.150']")

它将返回ip地址的mac-address。

我正在使用带有lxml包的Python 2.7.9。 有人可以帮忙吗?

更新1:示例XML

<arp-table-information>
<arp-table-entry>
    <mac-address>00:a0:a5:76:1a:96</mac-address>
    <ip-address>10.69.119.130</ip-address>
    <hostname>10.69.119.130</hostname>
    <interface-name>vlan.49</interface-name>
    <arp-table-entry-flags>
        <none/>
    </arp-table-entry-flags>
</arp-table-entry>
<arp-table-entry>
    <mac-address>00:0f:bb:c6:26:3d</mac-address>
    <ip-address>10.69.119.150</ip-address>
    <hostname>10.69.119.150</hostname>
    <interface-name>vlan.55</interface-name>
    <arp-table-entry-flags>
        <none/>
    </arp-table-entry-flags>
</arp-table-entry>
</arp-table-information>

更新2:请忽略更新1

当我使用

arptree = ex.device.rpc.get_arp_table_information().getroottree()
arptree.write('arptree.xml', pretty_print=True)

将ElementTree保存为xml,布局更改为

<arp-table-information style="normal">
<arp-table-entry>
<mac-address>
00:a0:a5:76:1a:96
</mac-address>
<ip-address>
10.69.119.130
</ip-address>
<hostname>
10.69.119.130
</hostname>
<interface-name>
vlan.49
</interface-name>
<arp-table-entry-flags>
<none/>
</arp-table-entry-flags>
</arp-table-entry>
<arp-table-entry>
<mac-address>
00:0f:bb:c6:26:3d
</mac-address>
<ip-address>
10.69.119.150
</ip-address>
<hostname>
10.69.119.150
</hostname>
<interface-name>
vlan.55
</interface-name>
<arp-table-entry-flags>
<none/>
</arp-table-entry-flags>
</arp-table-entry>

这可能是以下代码不起作用的原因???

arptree.xpath("descendant::mac-address[following-sibling::ip-address='10.69.119.150']")

根据这个xml文件,任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

在第二个XML中,您在IP地址值之前和之后都有新的行字符。您可以使用normalize-space()函数来修复它:

descendant::mac-address[following-sibling::ip-address[normalize-space()='10.69.119.150']]

演示的工作示例:

from lxml import etree

xml = """<arp-table-information style="normal">
<arp-table-entry>
<mac-address>
00:a0:a5:76:1a:96
</mac-address>
<ip-address>
10.69.119.130
</ip-address>
<hostname>
10.69.119.130
</hostname>
<interface-name>
vlan.49
</interface-name>
<arp-table-entry-flags>
<none/>
</arp-table-entry-flags>
</arp-table-entry>
<arp-table-entry>
<mac-address>
00:0f:bb:c6:26:3d
</mac-address>
<ip-address>
10.69.119.150
</ip-address>
<hostname>
10.69.119.150
</hostname>
<interface-name>
vlan.55
</interface-name>
<arp-table-entry-flags>
<none/>
</arp-table-entry-flags>
</arp-table-entry>
</arp-table-information>"""

root = etree.fromstring(xml)
result = root.xpath("descendant::mac-address[following-sibling::ip-address[normalize-space()='10.69.119.150']]")
for r in result:
    print(etree.tostring(r))

输出

<mac-address>
00:0f:bb:c6:26:3d
</mac-address>