2

I need to parse an xml file, lest say called example.xml, that looks like the following:

<?xml version="1.0" encoding="ISO-8859-1"?>
<nf:rpc-reply xmlns:nf="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns="http://www.cisco.com/nxos:1.0:if_manager">
 <nf:data>
  <show>
   <interface>
    <__XML__OPT_Cmd_show_interface___readonly__>
     <__readonly__>
      <TABLE_interface>
       <ROW_interface>
        <interface>Ethernet1/1</interface>
        <state>down</state>
        <state_rsn_desc>Link not connected</state_rsn_desc>
        <admin_state>up</admin_state>

I need to get "interface", and "state" elements as such: ['Ethernet1/1', 'down'] Here is my solution that doesnt work:

from lxml import etree

parser = etree.XMLParser()
tree = etree.parse('example.xml', parser)

print tree.xpath('//*/*/*/*/*/*/*/*/interface/text()')
print tree.xpath('//*/*/*/*/*/*/*/*/state/text()')

1 Answer 1

4

You need to handle namespaces here:

from lxml import etree

parser = etree.XMLParser()
tree = etree.parse('example.xml', parser)
ns = {'ns': 'http://www.cisco.com/nxos:1.0:if_manager'}

interface = tree.find('//ns:ROW_interface', namespaces=ns)
print [interface.find('.//ns:interface', namespaces=ns).text,
       interface.find('.//ns:state', namespaces=ns).text]

Prints:

['Ethernet1/1', 'down']

Using collections.namedtuple():

interface_node = tree.find('//ns:ROW_interface', ns)

Interface = namedtuple('Interface', ['interface', 'state'])
interface = Interface(interface=interface_node.find('.//ns:interface', ns).text,
                      state=interface_node.find('.//ns:state', ns).text)

print interface

Prints:

Interface(interface='Ethernet1/1', state='down')
Sign up to request clarification or add additional context in comments.

4 Comments

that works, is it possible to add the extracted data directly to a named tuple instead of a list? like so interafces = ('interfaces'['interface', 'state'])
@dragon great, you can also try to make use of lxml.objectify..
one last bit, can we also parse a string in the same way rather than a file?
@dragon sure, use fromstring instead of parse.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.