Question

I trying to work with a XML file from where a get a list of IPs and subnets, after that I want to check if those IP are within of a subnet.

QueryXML getNodes = new QueryXML(Queries);  
NodeList IPs = getNodes.query();

QueryXML getSubnets = new QueryXML(Queries1);
NodeList subnets = getSubnets.query();

At this point, I already have my NodeList with each element from the XML file (Ips & subnets), but my problem now is that, I would like to convert those NodeList to Array and so, use the following element which is calling a constructur with two arrays as a parameter.

SubnetUtilsExample findobjects = new SubnetUtilsExample(IPs, subnets); 

I googled it but i didnt find a right way. Could somebody help me with that?

Thanks.

Was it helpful?

Solution 2

I've tried creating my own method but ufortunatelly I couldnt fix it on this way.

Finally what I did is create an ArrayList and inlcude the NodeValue of each item in the Array. Then, now I have an Arraylist with all items from my QueryXML ready to use.

Final code:

ArrayList<String> IPtable=new ArrayList<String>();
QueryXML getNodes = new QueryXML(Queries);
NodeList IPs = getNodes.query();
 for (int n=0; n<IPs.getLength();n++){
  String ip =IPs.item(n).getNodeValue();
   IPtable.add(ip);
}
ArrayList<String> IPSubnet=new ArrayList<String>();
QueryXML getSubnets = new QueryXML(Queries1);
NodeList subnets = getSubnets.query();
 for (int s=0; s<subnets.getLength();s++){
String subnet =subnets.item(s).getNodeValue();
     IPSubnet.add(subnet);

}

I'm not sure that It's the right way, but at less it works properlly and fast. I hope it'll useful for somebody else.

OTHER TIPS

Since Java 8, you can work with IntStream and map, where nodeList is instance of NodeList :

List<Node> nodes = IntStream.range(0, nodeList.getLength())
        .mapToObj(nodeList::item)
        .collect(Collectors.toList());

You can also convert Nodelist to Array List by Simply adding this piece of code

var getNodesArray = [].slice.call(IPs);

console.log(getNodesArray);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top