cancel
Showing results for 
Search instead for 
Did you mean: 

Remove node elements

Former Member
0 Kudos

Hi all,

I'm trying to remove some items from a context node.

Pretty new with webdynpro, so all suggestions are welcome.

//Reset table

IWDNode node = wdContext.currentContextElement().node().getChildNode("Projects", 0);

wdContext.currentContextElement().setTest("Size: "+node.size());

node.moveFirst();

for(int i=0; i < node.size(); i++){

wdContext.currentContextElement().setTest("Success: "+node.removeElement(wdContext.currentContextElement()));

node.moveNext();

}

This code will not work, I don't know why, but removeElement returns false.

Regards,

Adri

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Thanks for your replies, things are much clearer now.

One problem I couldn't solve with this code:

for(int i=0; i < node.size(); i++){

node.removeElement(wdContext.nodeProjects().getProjectsElementAt(i));

}

Not all the elements are removed after the for loop. If I check it with node.size() still 1 item is in the node. It also appears in my table.. (which is a little annoying..)

Did someone know why?

@Abhijeet, I will try your code later, because testing isn't possible right now. Thanks anyway!

Former Member
0 Kudos

Adri,

It's a common idiom that in collections with index-based access you must remove lements in reverse order (from tail to head):


for(int i=node.size() - 1; i >= 0; i--){
node.removeElement(wdContext.nodeProjects().getProjectsElementAt(i));
}

This solves "fundamental" problem with your code: on every iteration you are reducing size of collection but increasing index at the same time, so some elements "survive". Here is how "forward remove" should work (also I don't recommend it due to being non-obvious for occasional code reader):


int count = node.size();
while( count-- > 0 ) {
node.removeElement(wdContext.nodeProjects().getProjectsElementAt(0));
}

Notice that we remove zero element on every step and repeat this operation N time where N is size of node.

Valery Silaev

SaM Solutions

http://www.sam-solutions.net

Former Member
0 Kudos

Hi Valery,

This Approch works fine.

I have a node with around 1000 no of elements. There is a performance issue in looping through the node and remove the elements one by one.

Is there any other short cut way (any clear() methods or similar API), to do that.?

Appreciate your reply.

Regards,

Sekar

Answers (3)

Answers (3)

Former Member
0 Kudos

Thanks for your reply.

The problem is solved now!

abhijeet_mukkawar
Active Contributor
0 Kudos

Hi Adri,

try this:

wdContext.currentContextElement().setTest("Success: "+node.removeElement(node.current<give Node name>Element());

instead:

wdContext.currentContextElement().setTest("Success: "+node.removeElement(wdContext.currentContextElement()));

because in your case you are reffering to the root context's element but we want to remove the node's element

regards

Former Member
0 Kudos

Try this...


wdContext.nodeProjects().removeElement(wdContext.nodeProjects().getProjectsElementAt(i));

Don't forget to decrement i each time you remove an element (because the size() method will update each time).

(you don't need to worry about moveFirst and moveNext this way)