Stack Exchange
Deleting a Node using an ArrayList in java
I have a method that retrieves all of the Nodes of an XML file, and I need to delete a certain Node (without knowing its name, parent, or anything).  All I have is this ArrayList of Nodes that contains all of the Nodes inside "Ex2.xml", but the node still appears in the XML file when I try to delete it using:
BN2.get(5).getParentNode().removeChild(BN2.get(5));
Note: BN2 is the ArrayList that contains the nodes.
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 file: the  is not showing although it is written here
<a>
  <b>
    <s>
        <e>AAA</e>
        <e>BBB</e>
        <e>CCC</e>
    </s>
    <d>
        <v>whatever</v>
        <f>
        </f>
    </d>
</b>



You're reading a file in a doc variable, delete some element from a completely unrelated list BN2, then write the doc to the file. Why would that remove anything from the document. Where does BN2 come from?

Can you add the code that fill BN2? and maybe an extract of the xml?

BN2 is being filled by a class that uses the same XML file and here is the XML File @AbbéRésina
JB well the thing is if i need to delete a node from an XML file that i dont know anything about
