wood burning stoves 2.0*
The moose likes XML and Related Technologies and the fly likes Removing an XML Element with the same name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Removing an XML Element with the same name" Watch "Removing an XML Element with the same name" New topic
Author

Removing an XML Element with the same name

John Paul Hoey
Greenhorn

Joined: Apr 15, 2012
Posts: 25
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

Joined: Apr 15, 2012
Posts: 25
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Removing an XML Element with the same name
 
Similar Threads
Overwriting a specific XML section
How to pass an XML document (object) between classes and then pass back to Main Class
How to compare two huge xml files
Display contents in GUI format
How to get nested elements using DOM