I came across this problem where I cant remove element from the document.
Here is my code.
-
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
-
-
DocumentBuilder builder = dbf.newDocumentBuilder();
-
-
Document document = builder.newDocument();
-
-
document = builder.parse(new InputSource(new StringReader(string)));
-
-
Basically I am reading XML file of another website and putting it in a string and parsing it. Now to implement a search functionality, I need to get rid of an element.
I tried it the following way as suggest by a website while googling,
-
NodeList nodelist = document.getElementsByTagName("name");
-
Element e1 = (Element)nodeList.item(1);
-
e1.getParentNode().removeChild(e1);
-
It removes stuff from the NodeList when I do a out.println() before and after this code to verify, but document remains intact when I print it out (using xsl to format it and Transformer). But thats what I expected anyway.
I simply dont know how to remove an element from the actual DOM document. Any ideas anyone?
Cheers for looking at it.