xml print element values with python

88 Views Asked by At

I am testing pyez with rpc calls to get a mac-table, so far i am able to pull the data but when i print this data doesn´t show me the values it shows me only memory addresses.

below my code:

dev = Device( user='labroot', host='X.X.X.X', password='*****')

dev.open()

Device(X.X.X.X)

macs = dev.rpc.get_vpls_mac_table()

dev.close()

test = macs.findall('l2ald-mac-entry/l2-mac-entry/l2-mac-address')

print(test)

This print shows me the following:

for x in test:
...     print(x)
...
<Element l2-mac-address at 0x7fc606f66d88>
<Element l2-mac-address at 0x7fc606f66f38>
<Element l2-mac-address at 0x7fc606f66cb0>
<Element l2-mac-address at 0x7fc606f66f80>
<Element l2-mac-address at 0x7fc606f66b00>
<Element l2-mac-address at 0x7fc606f6f050>
<Element l2-mac-address at 0x7fc606f6f0e0>

the content of ¨macs¨ is like this:

<l2ald-rtb-macdb>
<l2ald-mac-entry style="brief-rtb">
<l2-mac-routing-instance>VPLS</l2-mac-routing-instance>
<l2-mac-bridging-domain>__VPLS__</l2-mac-bridging-domain>
<l2-bridge-vlan>none</l2-bridge-vlan>
<l2-mac-entry>
  <l2-mac-address>be:01:01:01:01:01</l2-mac-address>
  <l2-mac-flags>D</l2-mac-flags>
  <l2-mac-logical-interface>lsi.1049332</l2-mac-logical-interface>

what i want to print are the mac-addresses under l2-mac-entry hierarchies, in this example would be value ¨be:01:01:01:01:01¨.

Hope you can help.

1

There are 1 best solutions below

0
On

It is convenient to use XPath for XML parsing. You may try something like this:

import xml.etree.ElementTree as ET

for x in test:
    el = ET.fromstring(x)
    mac = el.find(".//*l2-mac-address").text
    print(mac)

returns "be:01:01:01:01:01" for your data example