Parse the NETCONF Server Response in Perl Client Applications
After establishing a connection to a NETCONF server, a NETCONF Perl 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
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/20.1R1/junos" message-id='3'> <chassis-inventory xmlns="http://xml.juniper.net/junos/20.1R1/junos-chassis"> <chassis style="inventory"> <name>Chassis</name> <serial-number>G1234</serial-number> <description>MX960</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");