I have been trying to parse some XML for a couple of hours now with no luck. Checked similar threads and reviewed the ElementTree docs and still quite lost.
Basically, I am receiving some XML output from a router that is stored in a string, that I in turn must parse for some specific information.
Here is a sample of the xml I am working on:
xml = """<rpc-reply xmlns:junos="http://xml.juniper.net/junos/14.1D0/junos">
<route-information xmlns="http://xml.juniper.net/junos/14.1D0/junos-routing">
<!-- keepalive -->
<route-table>
<table-name>inet.0</table-name>
<destination-count>52</destination-count>
<total-route-count>52</total-route-count>
<active-route-count>52</active-route-count>
<holddown-route-count>0</holddown-route-count>
<hidden-route-count>0</hidden-route-count>
<rt junos:style="brief">
<rt-destination>5.5.5.5/32</rt-destination>
<rt-entry>
<active-tag>*</active-tag>
<current-active/>
<last-active/>
<protocol-name>Direct</protocol-name>
<preference>0</preference>
<age junos:seconds="428929">4d 23:08:49</age>
<nh>
<selected-next-hop/>
<via>lo0.0</via>
</nh>
</rt-entry>
</rt>
</route-table>
</route-information>
<cli>
<banner></banner>
</cli>
</rpc-reply>"""
For example, the node I would like to get-to/print contents is the rt-destination.
I have tried:
root = ET.fromstring(xml)
values = root.find('rt')
for element in values:
print element.text
This,
value= root.find('rt-destination')
print value
And this to set root (pointer?) at the specific node,
x = root.getiterator(tag = "destination-count")
Any help regarding how to traverse to this specific node or how to get to the desired outcome would be immensely appreciated.