我有一个对象arptree,我试图定位一个ip地址的mac地址,但是失败了。
>>> 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地址10.69.119.150的mac地址就在那里。
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地址。
我在lxml包中使用Python2.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文件,有人能帮忙吗?
发布于 2015-07-15 07:38:55
在第二个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>https://stackoverflow.com/questions/31423028
复制相似问题