-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathparse_interface_info.java
More file actions
51 lines (45 loc) · 1.78 KB
/
Copy pathparse_interface_info.java
File metadata and controls
51 lines (45 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* Copyright (c) 2013 Juniper Networks, Inc.
* All Rights Reserved
*
* Use is subject to license terms.
*
*/
//code to parse layered rpc reply
import net.juniper.netconf.Device;
import net.juniper.netconf.XML;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
public class parse_interface_info {
public static void main(String[] args) throws IOException,
SAXException {
Device device = CreateDevice.createDevice();
device.connect();
XML rpc_reply = device.executeRPC("get-interface-information");
System.out.println(rpc_reply.toString());
// Obtain a list of list of ‘org.w3c.dom.Node’ objects
List<String> list = Arrays.asList("interface-information","physical-interface");
List physical_interfaces_list = rpc_reply.findNodes(list);
// Print the value for each of the name elements:
for (Object o : physical_interfaces_list) {
Node node = (Node) o;
NodeList child_nodes_of_phy_interface = node.getChildNodes();
// child_nodes_of_phy_interface contains nodes like <name> and <admin-status>
// Get each <name> node from the NodeList
for (int i = 0; i < child_nodes_of_phy_interface.getLength(); i++) {
Node child_node = child_nodes_of_phy_interface.item(i);
if (child_node.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
if (child_node.getNodeName().equals("name")) { // Print the text value of the <name> node
System.out.println(child_node.getTextContent());
}
break;
}
}
}
}