[jdom-interest] Removing an element

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

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

More information about the jdom-interest mailing list