• 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

Unable to delete a specific node in XML

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an XML file and I need to delete a specific node. The node to be deleted will be defined dynamically based on the logic. I have been searching in internet for a solution but couldn't delete my node still. am getting error - (NOT_FOUND_ERR: An attempt is made to reference a node in a context where it does not exist)Below is a sample XML File. I need to delete the node -<NameValuePairs> which has <name>Local Variables</name>. Below is my sample XML & Java Code




 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you sure it is not working?

Have you written the modified document out to a new file, or what?

Bill
 
Lettisha Saroj
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Bill,

I have tried both the statements given below but I am still getting the same error. My requirement is to compare 2 xml files. Remove the duplicates from the original file and create a new xml file with the changes.

oDestchildNode.getParentNode().removeChild(oDestchildNode); //Not Working
oDoc3.getDocumentElement().removeChild(oDestchildNode);

Later I have tried with Element. I do not get any error when tried with Element but the node doesnt get deleted in this case.

I have been searching for a solution since few days but no luck.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that the Node method removeChild() can throw a DOMException - where do you check for this?

It has been a while since I worked with this API so maybe I remember incorrectly, but why are you even trying to removeChild() from the DocumentElement?

Bill
 
Lettisha Saroj
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I could finally fix this up by using XPath and I had to traverse the node in reverse order when deleting. Below is the final code that worked

>
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad to hear you got it working!
reply
    Bookmark Topic Watch Topic
  • New Topic