我知道这比看起来的要简单,但我想不通……
如何遍历两个Java NodeList集合以查看是否存在匹配。
例如:
NodeList NODE1 = doc1.getElementsByTagName("tag1");
NodeList NODE2 = doc2.getElementsByTagName("tag2");我想遍历NODE1和NODE2,看看这两个标记中是否有匹配的数据。我有Java基础知识,而且我是XML方面的新手,因此非常感谢您的帮助。
发布于 2017-02-08 21:07:30
试一下下面这几行:
public Collection<Node> getMatchingNodes(NodeList n1, NodeList n2, Class<? extends Node> typeToMatch){
Collection<Node> n1Nodes = getNodes(n1, typeToMatch);
Collection<Node> n2Nodes = getNodes(n2, typeToMatch);
n1Nodes.retainAll(n2Nodes);
return n1Nodes;
}
private Collection<Node> getNodes(NodeList nodeList, Class<? extends Node> typeToMatch){
Collection<Node> n1Nodes = new ArrayList<>();
for(int i = 0; i < nodeList.getLength(); i++){
if(nodeList.item(1).getClass() == typeToMatch){
n1Nodes.add(nodeList.item(i));
}
}
return n1Nodes;
}请注意,这只比较相同类型的节点,例如文本。
https://stackoverflow.com/questions/42109759
复制相似问题