Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Removing an XML Element with the same name

 
John Paul Hoey
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • 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
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic