我想要获取属于文档一部分的所有节点,并且只能使用getElementByTagName("*")
获取元素节点。但是这不会返回其他节点即,属性和其他节点。如何从DOM文档(元素,属性等)获取所有节点
是否有api可用或者我必须对这些元素进一步迭代以获得属性节点?
这就是我现在要做的;要知道,如果有任何其他方式或事先直接的API相同
private static List<Node> getAllNodes(Document doc) {
List<Node> returnList = new LinkedList<>();
NodeList nodes = doc.getElementsByTagName("*");
for (int index = 0; index < nodes.getLength(); index++) {
returnList.add(nodes.item(index));
NamedNodeMap attribList = nodes.item(index).getAttributes();
if (attribList == null) {
continue;
}
for (int j = 0; j < attribList.getLength(); j++) {
returnList.add(attribList.item(j));
}
}
return returnList;
}
感谢
请粘贴您的DOM节点文件。 –
请注意,属性的语义随[DOM4](属性不再是节点)而改变,它们不再实现Nodes接口。这可能与java无关,但是DOM树的节点与附加到节点的属性之间的语义区别仍然值得注意。 – the8472