简体   繁体   English

节点列表到数组列表<String>转换

[英]Nodelist to arraylist<String> conversion

I have extracted nodelist like, this.NodeList xml = doc.getElementsByTagName(tagName)我已经提取了节点列表,例如this.NodeList xml = doc.getElementsByTagName(tagName)

Now I want to convert the xml to ArrayList type, any suggestions?现在我想将 xml 转换为 ArrayList 类型,有什么建议吗?

Concerning Java关于 Java

Since Java 8, you can work with IntStream and map, where nodeList is instance of NodeList :从 Java 8 开始,您可以使用 IntStream 和 map,其中 nodeList 是NodeList实例:

List<String> nodeNames = IntStream.range(0, nodeList.getLength())
        .mapToObj(nodeList::item)
        .map(n -> n.getNodeName())
        .collect(Collectors.toList());

This will collect the nodes' name into a list.这会将节点的名称收集到列表中。

For more general purpose, you can collect the Node elements and then work on it:对于更通用的用途,您可以收集Node元素,然后对其进行处理:

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

Note that since Java 10, you could also just var instead of List<Node> :请注意,从 Java 10 开始,您也可以只使用var代替List<Node>

var nodes = IntStream.range(0, nodeList.getLength())
        .mapToObj(nodeList::item)
        .collect(Collectors.toList());
var nodeArrayList = new ArrayList(xmlNodeList.OfType<XmlNode>().ToList());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM