我提取了类似的节点列表,
this.NodeList xml = doc.getElementsByTagName(tagName)
现在我想将xml转换为ArrayList类型,有什么建议吗?
答案 0 :(得分:0)
var nodeArrayList = new ArrayList(xmlNodeList.OfType<XmlNode>().ToList());
答案 1 :(得分:0)
关于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());