Specifying the XML Parser for a Junos PyEZ Session
Extensible Markup Language (XML) is a standard for representing and communicating information. It is a metalanguage for defining customized tags that are applied to a data set or document to describe the function of individual elements and codify the hierarchical relationships between them. Junos OS natively supports XML for the operation and configuration of devices running Junos OS.
Client applications use XML parsers to read and work with XML documents, for example, the command output or configuration data returned as XML in the RPC reply of a device running Junos OS. XML parsers can use different approaches to parse an XML document. Document Object Model (DOM) parsers create a tree representation of the whole XML document, which is loaded in its entirety into memory. By contrast, a Simple API for XML (SAX) parser performs event-based parsing and parses each part of the XML document sequentially. As a result, SAX parsers only load a small portion of the XML data in memory at any time.
SAX parsers do not require a lot of memory to perform operations, but DOM parser memory requirements increase with the document size. In general DOM parsing is faster than SAX parsing, because the application can access the entire XML document in memory. However, as the XML document size increases, DOM parsers require more memory, and SAX parsing becomes more efficient.
Junos PyEZ uses the
library, which defaults to using DOM parsing for XML processing. In
this case, the parser converts the entire XML document into an
lxml object and loads it into memory. Devices running
Junos OS can return large XML documents for show command output and
configuration data. If you only need to retrieve a small subset of
values from the XML in these cases, DOM parsing can be inefficient.
Starting in Junos PyEZ Release 2.3.0, Junos PyEZ uses SAX parsing when possible in the following cases:
When you use operational Tables and Views to retrieve structured output
When you include the
use_filter=Trueargument in the
Device()instance for a given session and perform operations that request a subset of tag values from XML output
For example, the following script executes the
get_interface_information RPC and filters the XML output
to return only the
<name> element for
each physical interface. In this case, SAX parsing is used, because
only a subset of the XML is requested.
from jnpr.junos import Device from lxml import etree with Device(host='router.example.com', use_filter=True) as dev: sax_input = '<interface-information><physical-interface><name/></physical-interface></interface-information>' result = dev.rpc.get_interface_information(filter_xml=sax_input) print (etree.tostring(reply, encoding='unicode'))