Search code examples
javaarraysnodelist

How could I convert from NodeList to Array and work with this Array in others class?


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.


Solution

  • 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.