Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

Use the NETCONF Java Toolkit to Parse an RPC Reply

After submitting an operational or configuration request to the NETCONF server, the server responds with an RPC reply. You can use several approaches to parse the RPC reply in order to extract the desired information.

There are two approaches to parse an XML reply within the context of the NETCONF Java toolkit:

  • Get the org.w3c.dom.Document object and use the native parsing methods available in the standard Java class libraries for a Document object.

  • Use the findValue(List list) and findNodes(List list) methods available in the net.juniper.netconf.XML class on the XML object.

For the first approach, call the getOwnerDocument() method on the reply object to return the Document object.

You can then use methods in the standard Java libraries on the resulting Document object. This method is useful for the flexibility and options available in terms of the standard Java library methods.

For the second approach, the net.juniper.netconf.XML class contains the findValue(List list) and findNodes(List list) methods, which you can use to parse the XML object. You must include the “import java.util.*;” statement in your program code to use the functionality of the List interface or to create an Arrays object as shown in the corresponding examples.

Study the following RPC reply for the get-interface-information operational request:

Parsing an RPC Reply Using findValue()

You can use the findValue() method to determine the value of a given element at any level of the hierarchy. In the example RPC reply for get-interface-information, suppose you want to determine the value of the <admin-status> element of the physical interface ge-0/0/0. Being aware of the format of the RPC reply, you can extract this information using the following code:

Note that the interface name uses a tilde (~) character to identify the particular element. Execution of this code prints “up” to standard output.

Parsing an RPC Reply Using findNodes()

You can use the findNodes() method to obtain the list of all nodes under a given hierarchy as org.w3c.dom.Node objects. The following code snippet obtains a list of all <physical-interface> nodes under the <interface-information> element in the hierarchy:

However, you might want to extract a specific node. The following code returns the hierarchy for the ge-0/0/1 interface only:

Example: Parsing an RPC Reply Using findNodes() (Detailed)

The following example takes this approach a step further and parses through the child nodes to extract and print the content for just the <name> elements. This sample code focuses on the portion of the program that parses the RPC reply and does not represent a complete program.