[英]Nodelist to arraylist<String> conversion
我已經提取了節點列表,例如this.NodeList xml = doc.getElementsByTagName(tagName)
現在我想將 xml 轉換為 ArrayList 類型,有什么建議嗎?
關於 Java
從 Java 8 開始,您可以使用 IntStream 和 map,其中 nodeList 是NodeList
實例:
List<String> nodeNames = IntStream.range(0, nodeList.getLength())
.mapToObj(nodeList::item)
.map(n -> n.getNodeName())
.collect(Collectors.toList());
這會將節點的名稱收集到列表中。
對於更通用的用途,您可以收集Node
元素,然后對其進行處理:
List<Node> nodes = IntStream.range(0, nodeList.getLength())
.mapToObj(nodeList::item)
.collect(Collectors.toList());
請注意,從 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.