Removing a node from a XML File in Java

MrPickle

New Member
I have an XML file that is formatted like this:\[code\]<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>\[/code\]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:\[code\]private void updateImageElement(Element image, Model model) throws SAXException, IOException, ParserConfigurationException{ Element rootElement = doc.getDocumentElement(); rootElement.removeChild(image); image.getParentNode().removeChild(image);}\[/code\]The "rootElement.removeChild(image);" line throws the following exception:\[code\]org.w3c.dom.DOMException: NOT_FOUND_ERR: An attempt is made to reference a node in a context where it does not exist.\[/code\]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.
 
Back
Top