我有这个 XML 代码:
<root>
<node>
</first_child>
</second_child>
</third_child>
</node>
</root>
我需要一一获取所有子节点,并使用 DOM 保存三个 Node 变量。
如果我使用
doc.getElementsByTagName("node");
我将这个“节点”与所有子节点一起使用,而我只需要“first_child、second_child 和third_child”
如何获得这个?
最佳答案
Element el = (Element)(doc.getElementsByTagName("node").item(0));
NodeList children = el.getChildNodes();
for (int i=0; i<children.getLength(); i++) {
System.out.println(children.item(0).getNodeValue());
}
关于java - 使用 DOM 选择节点的所有子节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12712547/