Parse the NETCONF Server Response in Perl Client Applications
In a NETCONF Perl client application, after establishing a connection
to a NETCONF server, the client application can submit one or more
requests by invoking Perl methods. The NETCONF server returns the
appropriate information in an <rpc-reply>
element. There are two ways of parsing the NETCONF server’s
response:
By using functions of XML::LibXML::DOM
By using functions of XML::LibXML::XPATHContext
Prior to Junos OS Release 16.1, every Junos OS release
included a new, release-dependent version of the NETCONF Perl client. Beginning in Junos OS Release 16.1, the NETCONF Perl
client is release-independent, is hosted on GitHub and CPAN, and can
manage devices running any version of the Junos OS release. The release-independent version of the NETCONF Perl client does
not include the Net::Netconf::Transform
module that was present in the release-dependent versions of the
client.
For example, consider the following reply from a NETCONF server:
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="http://xml.juniper.net/junos/16.1R1/junos" message-id='3'> <chassis-inventory xmlns="http://xml.juniper.net/junos/16.1R1/junos-chassis"> <chassis style="inventory"> <name>Chassis</name> <serial-number>G1234</serial-number> <description>MX80-48T</description> ... </chassis> </chassis-inventory> </rpc-reply>
Suppose the user wants to parse the response and retrieve the
value of the <serial-number>
element.
The following code uses XML::LibXMl::DOM
to retrieve the value. The example stores the response in a variable
and calls methods of DOM
to parse the response.
my $query = "get_chassis_inventory"; my $res = $jnx->$query(); my $rpc = $jnx->get_dom(); my $serial = $rpc->getElementsByTagName("serial-number")->item(0)->getFirstChild->getData; print ("\nserial number: $serial");
The following code uses XML::LibXML::XPATHContext
to retrieve the value. The example stores the response in a variable
and calls XPathContext
methods to retrieve
the value. The local-name()
function returns
the element name without the namespace. The XPATH expression appears
on multiple lines for readability.
my $query = "get_chassis_inventory"; my $res = $jnx->$query(); my $rpc= $jnx->get_dom(); my $xpc = XML::LibXML::XPathContext->new($rpc); my $serial=$xpc->findvalue(' /*[local-name()="rpc-reply"] /*[local-name()="chassis-inventory"] /*[local-name()="chassis"] /*[local-name()="serial-number"]'); print ("\nserial number: $serial");
Change History Table
Feature support is determined by the platform and release you are using. Use Feature Explorer to determine if a feature is supported on your platform.