Reading XML data from a file

762 views
Skip to first unread message

Gavin McKee

unread,
Apr 8, 2016, 9:56:18 AM4/8/16
to Junos Python EZ
Hi Guys,

Here is my scenario , some devices do not have NETCONF enabled at this time, so I wanted to manually go into the device execute a show command and save the xml response to a local file e.g.

show route | display xml | save <filename>.xml

I want to scp that file from the device to a local disk and then do the following

route_table = RouteTable(path=<filename>).get()

Now the trouble is that I get zero items back, when attempting to read the file, so I thought I would parse it with lxml and strip out any spaces
parser = etree.XMLParser(remove_blank_text=True)
tree = etree.parse(<filename>,parser)

Again when reading the file I get zero items back!  Is this something that can be done ?

Thanks

Gav


Ganesh Nalawade

unread,
Apr 8, 2016, 2:52:05 PM4/8/16
to Gavin McKee, Junos Python EZ
Hi Gav,

Can you please share the xml file and yaml table you are using.

Regard,
Ganesh

--
You received this message because you are subscribed to the Google Groups "Junos Python EZ" group.
To unsubscribe from this group and stop receiving emails from it, send an email to junos-python-...@googlegroups.com.
Visit this group at https://groups.google.com/group/junos-python-ez.
To view this discussion on the web visit https://groups.google.com/d/msgid/junos-python-ez/98be4e2f-ecdf-4e46-9b9d-0ef48d31f6c4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Gavin McKee

unread,
Apr 8, 2016, 3:11:37 PM4/8/16
to Junos Python EZ
Just one last point - the attached YAML is working fine when I use PyEZ to load the route table using a device connection.

Stacy W. Smith

unread,
Apr 8, 2016, 4:34:25 PM4/8/16
to Gavin McKee, Junos Python EZ
It's expecting the XML to be rooted at <route-information>, and the namespaces to be stripped.

--Stacy

--
You received this message because you are subscribed to the Google Groups "Junos Python EZ" group.
To unsubscribe from this group and stop receiving emails from it, send an email to junos-python-...@googlegroups.com.
Visit this group at https://groups.google.com/group/junos-python-ez.

Ganesh Nalawade

unread,
Apr 8, 2016, 4:51:07 PM4/8/16
to Stacy W. Smith, Gavin McKee, Junos Python EZ
Hi  Gav,

As Stacy mentioned xml should point to <route-information> and namespace removed.
One way to achieve this is as below. Let me know if it works for you.

from lxml import etree
from jnpr.junos.op.routes import RouteTable
from jnpr.junos.jxml import remove_namespaces

root = etree.parse('routes.xml').getroot()
rt_info = root.getchildren()[0]

tbl = RouteTable()
tbl.xml = remove_namespaces(rt_info)

print tbl.items()

Regards,
Ganesh


Gavin McKee

unread,
Apr 8, 2016, 8:35:46 PM4/8/16
to Junos Python EZ, st...@acm.org, gavmc...@googlemail.com
Thanks Stacy and Ganesh, as always your help greatly appreciated.

Gav

Gavin McKee

unread,
Apr 8, 2016, 8:58:07 PM4/8/16
to Junos Python EZ
Sorry guys,

One last question on this.  If you follow the approach that Ganesh provided , removing the namespace from the file, should you be able to do something like this:

root = etree.parse('routes.xml').getroot()
rt_info = root.getchildren()[0]

tbl = RouteTable()
tbl.xml = remove_namespaces(rt_info)

print tbl.items()

tbl.savexml('<some path>/newxml.xml')

new_route_table = RouteTable(path=newxml.xml').get()

print new_route_table

I've tried this and it does not seem to work when I read the file back.  Is this something you would expect to work?

Thanks again

Gav

On Friday, 8 April 2016 09:56:18 UTC-4, Gavin McKee wrote:

Gavin McKee

unread,
Apr 11, 2016, 11:17:38 AM4/11/16
to Junos Python EZ
Hi Ganesh,

I added the traceback in the jxml.py file

>>> rpc_reply = process_reply(open('/home/gmckee/Documents/routes.xml').read())
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/jnpr/junos/jxml.py", line 175, in cscript_conf
    return NCElement(etree.tostring(reply), transform_reply)._NCElement__doc
  File "src/lxml/lxml.etree.pyx", line 3350, in lxml.etree.tostring (src/lxml/lxml.etree.c:79228)
TypeError: Type 'str' cannot be serialized.



On Friday, 8 April 2016 09:56:18 UTC-4, Gavin McKee wrote:

Ganesh Nalawade

unread,
Apr 11, 2016, 11:44:55 AM4/11/16
to Gavin McKee, Junos Python EZ
Hi Gavin,

It seems the code present of Github is different from the one in your PyEz version.
Can you replace below line in jxml.py file
>>>  return NCElement(etree.tostring(reply), transform_reply)._NCElement__doc
With
>>>return NCElement(reply, transform_reply)._NCElement__doc
to check below code snippet works fine?

from jnpr.junos.op.routes import RouteTable
from jnpr.junos.jxml import cscript_conf as process_reply

rpc_reply = process_reply(open('routes.xml').read())

tbl = RouteTable()
tbl.xml = rpc_reply.getchildren()[0]

tbl.savexml('newxml.xml')

new_route_table = RouteTable(path='newxml.xml').get()
Regards,
Ganesh

--
You received this message because you are subscribed to the Google Groups "Junos Python EZ" group.
To unsubscribe from this group and stop receiving emails from it, send an email to junos-python-...@googlegroups.com.
Visit this group at https://groups.google.com/group/junos-python-ez.

Gavin McKee

unread,
Apr 11, 2016, 1:23:32 PM4/11/16
to Junos Python EZ
Here is the solution (thanks to Ganesh for working through this with me)

There are two approaches , i think the second one is best as the newer version of the PyEz lib will be supporting it

1. Pass the process_reply call an etree object

from lxml import etree

from jnpr.junos.op.routetable import RouteTable

from jnpr.junos.jxml import cscript_conf as process_reply

 

root = etree.parse('/<path>/routes.xml').getroot()

rpc_reply = process_reply(root)

tbl = RouteTable()

tbl.xml = rpc_reply.getchildren()[0]

 

>>> tbl

RouteTable:None: 5 items


or


2. You can change the following function in the jnpr.junos.jxml.py file

 

From

 

def cscript_conf(reply):

    try:

        device_params = {'name': 'junos'}

        device_handler = manager.make_device_handler(device_params)

        transform_reply = device_handler.transform_reply()

        return NCElement(etree.tostring(reply), transform_reply)._NCElement__doc

    except:

        traceback.print_exc(file=sys.stdout)

        return None

 

To

 

def cscript_conf(reply):

    try:

        device_params = {'name': 'junos'}

        device_handler = manager.make_device_handler(device_params)

        transform_reply = device_handler.transform_reply()

       return NCElement(reply, transform_reply)._NCElement__doc

    except:

        traceback.print_exc(file=sys.stdout)

        return None


I've tested both approaches and they both work .

Gav


On Friday, 8 April 2016 09:56:18 UTC-4, Gavin McKee wrote:
Reply all
Reply to author
Forward
0 new messages