[jdom-interest] Removing an element

James Cook James.Cook at jobpartners.com
Tue Mar 5 10:48:36 PST 2002

Your line:
will always remove the first child name "user"

You need to modify the live List, maintain a count (i) of where you are in
the iterator, then replace the line above with:

Be warned though this will only work for the first instance that you find,
once you have removed it, you should break, else the iterator will not
represent the list. If you have to keep going, build a vector of those that
should be removed and then remove them in a loop afterwards.


-----Original Message-----
From: Qasim [mailto:Q.M.Qasim at Bradford.ac.uk]
Sent: 05 March 2002 16:34
To: jdom-interest at jdom.org
Subject: [jdom-interest] Removing an element

I am trying to remove an element from my xml file. the structure of my 
xml file is as follws:

<?xml version="1.0" encoding="UTF-8"?>
  <user username="qasim" url="" />
  <user username="nabil" url="localhost" />
I search the xml file with a username and url and if my search matches 
a username and url, I want to delete that element. For instance if the 
username is 'qasim' and url is '', I would like to delete 
the whole user element.

I have written the follwing code, but it does not work. can some one 

public void removeElement(String username,String location)
    	Element root =document.getRootElement();
	 List users = root.getChildren("user");   
	 Iterator i=users.iterator();   
	 while (i.hasNext()) 
	 Element user= (Element)i.next();      
	 int count=0;
	  		String u=user.getAttributeValue("username");
	  		String l=user.getAttributeValue("url");
	 		int q = username.compareTo(u);
	 		int r= location.compareTo(l);
	 		if ((q==0) || (r==0)) 
	 	if (count==-1)
	 	System.out.println("none deleted ");
	 // increment number of description tag found      
	 /// end of loop//      
Q.M.Qasim at bradford.ac.uk

To control your jdom-interest membership:

This e-mail and any attachments are confidential and may also be privileged.
If you are not the named recipient, please notify the sender immediately and
do not disclose the contents to another person, use it for any purpose, or
store or copy the information in any medium. Any views or opinions presented
are solely those of the author and do not necessarily represent those of
Jobpartners.  Jobpartners is unable to guarantee the security (especially
that of attached files) of any email content outside of our own computer
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20020305/6a02e5af/attachment.htm

More information about the jdom-interest mailing list