I have an XML file that is formatted like this:
<state>
<image>
<imageUrl>./testImages/testimage.png</imageUrl>
<perspective id="0">
<zoomLevel>1.0</zoomLevel>
<offsetX>0.0</offsetX>
<offsetY>0.0</offsetY>
</perspective>
<perspective id="1">
<zoomLevel>1.0</zoomLevel>
<offsetX>0.0</offsetX>
<offsetY>0.0</offsetY>
</perspective>
</image>
</state>
In that file, I have multiple image nodes, but that is not the point. What I'd like is to be able to remove the < image > node (and all its child nodes)from the document.
I have the following code so far:
private void updateImageElement(Element image, Model model) throws SAXException, IOException, ParserConfigurationException{
Element rootElement = doc.getDocumentElement();
rootElement.removeChild(image);
image.getParentNode().removeChild(image);
}
The "rootElement.removeChild(image);" line throws the following exception:
org.w3c.dom.DOMException: NOT_FOUND_ERR: An attempt is made to reference a node in a context where it does not exist.
Which is weird, because if I print "rootElement", it displays "state", which IS image's parent node.
I then tried the following line ("image.getParentNode().removeChild(image)). This one doesn't throw an Exception, but nothing is removed either.
If I print that line, it also says the parent node is "state, so I can't even figure out what the difference between the two lines is.