Lettisha Saroj wrote:My requirement is to compare 2 xml files. Remove the duplicates from the original file and create a new xml file with the changes.
That doesn't sound anything like what your original post said:
I need to delete the node -<NameValuePairs> which has <name>Local Variables</name>.
It doesn't help when you post code which tries unsuccessfully to solve one problem and then tell us that your requirement is something else entirely.
However as far as your original requirement goes: your original code never tries to delete any <NameValuePairs> elements. It looks like it might try to delete <name> elements, but without first checking whether they have a child text node whose value is "Local Variables". If it were my problem I would probably use XPath to identify the nodes you want to delete, without all the loops you have, and then delete those nodes.
There's also a gotcha which many programmers fall into and it isn't limited to Java. If you have a list of N elements and you want to delete those elements from the list, after you delete element 1 from the list the rest all get renumbered, so if you next try to delete element 2, you're pointing at the element which was formerly numbered 3. So first of all this only deletes half of the elements in the list, and second of all the other half of the deletion attempts are pointing to elements which aren't in the list any more. However I don't know if that's an issue in this particular case because you aren't exactly deleting elements from the list. Usually this gotcha is fixed by deleting the elements in reverse order, from N down to 1 (or N-1 down to 0 in Java style), so you could perhaps try that.
And like Bill, I'm suspicious that you don't see any changes because your code doesn't serialize the modified DOM out to a new file.