简体   繁体   中英

Nodelist to arraylist<String> conversion

I have extracted nodelist like, this.NodeList xml = doc.getElementsByTagName(tagName)

Now I want to convert the xml to ArrayList type, any suggestions?

Concerning Java

Since Java 8, you can work with IntStream and map, where nodeList is instance of 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:

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> :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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