我有一个检索XML文件的所有节点的方法,我需要删除某个节点(不知道它的名字,父节点或任何东西)。我只有ArrayList
个节点包含“Ex2.xml”中的所有节点,但当我尝试使用以下命令删除它时,该节点仍然出现在XML文件中:
BN2.get(5).getParentNode().removeChild(BN2.get(5));
注意: BN2是包含节点的ArrayList
。
File xmlFile = new File("Ex2.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);
doc.getDocumentElement().normalize();
BN2.get(5).getParentNode().removeChild(BN2.get(5));
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource domSource = new DOMSource(doc);
StreamResult streamResult = new StreamResult(new File("Ex2.xml"));
transformer.transform(domSource, streamResult);
XmL文件:虽然写在这里但未显示
<a>
<b>
<s>
<e>AAA</e>
<e>BBB</e>
<e>CCC</e>
</s>
<d>
<v>whatever</v>
<f>
</f>
</d>
</b>