Connecting Tech Pros Worldwide Forums | Help | Site Map

Removing node/element from DOM

Member
 
Join Date: Sep 2007
Posts: 52
#1: Oct 10 '09
I came across this problem where I cant remove element from the document.

Here is my code.

Expand|Select|Wrap|Line Numbers
  1. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  2.  
  3. DocumentBuilder builder = dbf.newDocumentBuilder();
  4.  
  5. Document document = builder.newDocument();
  6.  
  7. document = builder.parse(new InputSource(new StringReader(string)));
  8.  
  9.  
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,

Expand|Select|Wrap|Line Numbers
  1. NodeList nodelist = document.getElementsByTagName("name");
  2. Element e1 = (Element)nodeList.item(1);
  3. e1.getParentNode().removeChild(e1);
  4.  
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.

Reply