• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Removing an XML Element with the same name

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I was wondering if someone could help me with my query please.

I have the following XML elements within my XML document:

<AddressLines>
<AddressLine>143</AddressLine>
<AddressLine>Fork Street</AddressLine>
<AddressLine>East Calder</AddressLine>
<AddressLine>Lothian</AddressLine>
<AddressLine>Scotland</AddressLine>
</AddressLines>

What i want to do is delete ONLY the third element from the XML above, without affecting the other address lines within the XML, however unfortunately, i cant seem to figure out how to do this.

As an example, when i want to remove a node which contains one element from my XML document then i use the following code; this works successfully:



Also, when i want to add text to the third element of the above XML, i use a NodeList contained within the following method; again this works successfully:



However as prevously stated, i just cant seem to figure out how to remove only one element from the above XML example.

I tried looping through the NodeList and then deleting the item i want to remove however i cant seem to get this to work; im also not sure if this is the best approach either.

Any help in this matter would be very much appreciated.

Thanks in advance

John








 
John Paul Hoey
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Ive managed to figure this out therefore you can ignore this question - however ill post in the off chance anyone else needs to know

I found that i can use the .getElementsByTagName method with my XML document object which allows me to allocate a specific element item in the list, therefore i didnt need to use a NodeList.

Here is the code i used:



Thanks

John
 
Space pants. Tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic