Java 我怎样才能从 NodeList 转换为 Array 并在其他类中使用这个 Array?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19452380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:19:18  来源:igfitidea点击:

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

javaarraysnodelist

提问by Sallyerik

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.

我尝试使用 XML 文件,从中获取 IP 和子网列表,然后我想检查这些 IP 是否在子网内。

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.

在这一点上,我已经有了来自 XML 文件(Ips 和子网)的每个元素的 NodeList,但我现在的问题是,我想将这些 NodeList 转换为数组,因此,使用以下调用构造函数的元素以两个数组作为参数。

SubnetUtilsExample findobjects = new SubnetUtilsExample(IPs, subnets); 

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

我用谷歌搜索,但我没有找到正确的方法。有人可以帮我吗?

Thanks.

谢谢。

采纳答案by Sallyerik

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.

最后我所做的是创建一个 ArrayList 并包含 Array 中每个项目的 NodeValue。然后,现在我有一个 Arraylist,其中包含我的 QueryXML 中的所有项目可供使用。

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.

我不确定这是正确的方法,但至少它可以正常快速地工作。我希望它对其他人有用。

回答by Micky Q

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

您还可以通过简单地添加这段代码将节点列表转换为数组列表

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

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

console.log(getNodesArray);

控制台日志(getNodesArray);

回答by KeyMaker00

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

从 Java 8 开始,您可以使用 IntStream 和 map,其中 nodeList 是 的实例NodeList

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