[jdom-interest] Removing an element

Qasim Q.M.Qasim at Bradford.ac.uk
Tue Mar 5 08:34:12 PST 2002


Hi
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"?>
<users>
  <user username="qasim" url="143.53.31.174" />
  <user username="nabil" url="localhost" />
</users>
  
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 '143.53.31.174', I would like to delete 
the whole user element.

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

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)) 
	 			{
	 				root.removeChild("user");
	 				count++;
	 			} 
	 	if (count==-1)
	 	{
	 	System.out.println("none deleted ");
	 	}	      
	 		}
	 // increment number of description tag found      
	 /// end of loop//      
	 
	}
---------------------------------
 Qasim
Q.M.Qasim at bradford.ac.uk




More information about the jdom-interest mailing list