• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How can we remove a Node from a DOM tree?

 
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know that one of the way is:-
lNode.getParentNode().removeChild(lNode);

In this wmethod i need to check both lNode and lNode.getParentNode() not null before executing this method??
As if this method is called then refernce of ParentNode becomes null as it is detached from the parent node. Again calling the same method on that node can throw the nullpointerexception
Any other method which don't need the Parent Node reference
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have done something similar but not using the node to be deleted itself.
I have instead taken the root Node and then gone down the hierarchy to the parent node and on which I have called the removeChild() method. For e.g.

Assuming that the parent Node that you retrieve is the one whose child you have to remove.
But you can also first get the reference to the parent node first and then remove the child node. Something like the following

I suppose in this case the NullPointerException should not arise.
 
Andy Smith
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is where my problem is ..
and thats what i m doing ..
Suppose i want to remove the lNode ...
i used
Node lparentNode = lNode.getParentNode(); // parent is not null
lparentNode.remove(lNode); // thiswill work fine and will remove the lNode from the parent node
Now suppose i again call the same method on same node
Node lparentNode = lNode.getParentNode(); // parent is null this time as it is already removed from parent..
lparentNode.remove(lNode); // Will cause NUllPOinterException.....
Hope u got my problem now..???
Anyother method instead of getting reference to parent node??
I know that i can always check for parent to be not null.... but is there some other way???
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't get it. Why do you want to remove a node twice?
 
Ketan KC Chachad
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andy,
I too, like Lasse, am not understanding your problem.

Now suppose i again call the same method on same node


If you remove it once then the parent does not exist and the parent node then will always be null. In that case there is no use in keeping the node itself unless you plan to use the node somewhere else later on. So kindly let us know your exact requirements and secondly I do not think there is any other way for checking up on a node's parent other than getting it using getParentNode() checking if it is null or not null. Lasse, what is your opinion?
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm also inclined to think that removeChild() is the only way to go. On the other hand, I haven't worked with DOM structures which I need to tear apart so it's absolutely possible that I have missed something...
 
Andy Smith
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ideally i should not have called the removeElement on same node...
But i have to remove the element from the tree based upon many conditions ...
there are lot of if.. else......etc...
And i dont know whether the element is already removed....to removeElement is a method in the base code..and i can't change it so easily...to include a check for parent...
And yes i can alwayz provide a wrapper from removeElment in my class..which will check for the parent .. if not null then only call the base removeElement...

Is there any way out.??? thats my question?
[ November 27, 2003: Message edited by: Andy Smith ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic