节点列表到arraylist <string>转换</string>

时间:2012-04-24 16:10:04

标签: java xml

我提取了类似的节点列表, this.NodeList xml = doc.getElementsByTagName(tagName)

现在我想将xml转换为ArrayList类型,有什么建议吗?

2 个答案:

答案 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());
相关问题