I am trying to remove a node from a large xml file. With this code the tags of the other elements are altered as well. I was hoping someone could explain why or how to fix it.
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Document document = dbf.newDocumentBuilder().parse(new File(filePath)); //filePath - source file
/*while (document.getElementsByTagName("IMFile").getLength() != 0){
//Loop until all childs are removed
Element element = (Element) document.getElementsByTagName("IMFile").item(0);
element.getParentNode().removeChild(element);
}*/
//Test for first appearance
Element element = (Element) document.getElementsByTagName("IMFile").item(0);
element.getParentNode().removeChild(element);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
t.transform(new DOMSource(document), new StreamResult(new File(filePath+"_New"))); //destination
It changes positions of the xml such as:
<Attribute id="7" value="1920" name="width"/>
to <Attribute id="7" name="width" value="1920"/>
Also it cuts off some open or end tags:
<PowerPointFilename></PowerPointFilename
> to <PowerPointFilename/>