<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 5.5.2653.12">
<TITLE>RE: [jdom-interest] Removing an element</TITLE>
</HEAD>
<BODY>
<P><FONT SIZE=2>Your line:</FONT>
<BR><FONT SIZE=2>root.removeChild("user");</FONT>
<BR><FONT SIZE=2>will always remove the first child name "user"</FONT>
</P>
<P><FONT SIZE=2>You need to modify the live List, maintain a count (i) of where you are in the iterator, then replace the line above with:</FONT></P>
<P><FONT SIZE=2>users.remove(i);</FONT>
</P>
<P><FONT SIZE=2>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.</FONT></P>
<P><FONT SIZE=2>Regards</FONT>
<BR><FONT SIZE=2>James</FONT>
</P>
<P><FONT SIZE=2>-----Original Message-----</FONT>
<BR><FONT SIZE=2>From: Qasim [<A HREF="mailto:Q.M.Qasim@Bradford.ac.uk">mailto:Q.M.Qasim@Bradford.ac.uk</A>]</FONT>
<BR><FONT SIZE=2>Sent: 05 March 2002 16:34</FONT>
<BR><FONT SIZE=2>To: jdom-interest@jdom.org</FONT>
<BR><FONT SIZE=2>Subject: [jdom-interest] Removing an element</FONT>
</P>
<BR>
<P><FONT SIZE=2>Hi</FONT>
<BR><FONT SIZE=2>I am trying to remove an element from my xml file. the structure of my </FONT>
<BR><FONT SIZE=2>xml file is as follws:</FONT>
</P>
<P><FONT SIZE=2><?xml version="1.0" encoding="UTF-8"?></FONT>
<BR><FONT SIZE=2><users></FONT>
<BR><FONT SIZE=2> <user username="qasim" url="143.53.31.174" /></FONT>
<BR><FONT SIZE=2> <user username="nabil" url="localhost" /></FONT>
<BR><FONT SIZE=2></users></FONT>
<BR><FONT SIZE=2> </FONT>
<BR><FONT SIZE=2>I search the xml file with a username and url and if my search matches </FONT>
<BR><FONT SIZE=2>a username and url, I want to delete that element. For instance if the </FONT>
<BR><FONT SIZE=2>username is 'qasim' and url is '143.53.31.174', I would like to delete </FONT>
<BR><FONT SIZE=2>the whole user element.</FONT>
</P>
<P><FONT SIZE=2>I have written the follwing code, but it does not work. can some one </FONT>
<BR><FONT SIZE=2>help:</FONT>
</P>
<P><FONT SIZE=2>public void removeElement(String username,String location)</FONT>
<BR><FONT SIZE=2> {</FONT>
<BR><FONT SIZE=2> Element root =document.getRootElement();</FONT>
<BR><FONT SIZE=2> </FONT>
<BR> <FONT SIZE=2> List users = root.getChildren("user"); </FONT>
<BR> <FONT SIZE=2> Iterator i=users.iterator(); </FONT>
<BR> <FONT SIZE=2> while (i.hasNext()) </FONT>
<BR> <FONT SIZE=2> { </FONT>
<BR> <FONT SIZE=2> Element user= (Element)i.next(); </FONT>
<BR> <FONT SIZE=2> int count=0;</FONT>
<BR> <FONT SIZE=2> </FONT>
<BR> <FONT SIZE=2> String u=user.getAttributeValue("username");</FONT>
<BR> <FONT SIZE=2> String l=user.getAttributeValue("url");</FONT>
<BR> <FONT SIZE=2> </FONT>
<BR> <FONT SIZE=2> int q = username.compareTo(u);</FONT>
<BR> <FONT SIZE=2> int r= location.compareTo(l);</FONT>
<BR> <FONT SIZE=2> if ((q==0) || (r==0)) </FONT>
<BR> <FONT SIZE=2> {</FONT>
<BR> <FONT SIZE=2> root.removeChild("user");</FONT>
<BR> <FONT SIZE=2> count++;</FONT>
<BR> <FONT SIZE=2> } </FONT>
<BR> <FONT SIZE=2> if (count==-1)</FONT>
<BR> <FONT SIZE=2> {</FONT>
<BR> <FONT SIZE=2> System.out.println("none deleted ");</FONT>
<BR> <FONT SIZE=2> } </FONT>
<BR> <FONT SIZE=2> }</FONT>
<BR> <FONT SIZE=2> // increment number of description tag found </FONT>
<BR> <FONT SIZE=2> /// end of loop// </FONT>
<BR> <FONT SIZE=2> </FONT>
<BR> <FONT SIZE=2>}</FONT>
<BR><FONT SIZE=2>---------------------------------</FONT>
<BR><FONT SIZE=2> Qasim</FONT>
<BR><FONT SIZE=2>Q.M.Qasim@bradford.ac.uk</FONT>
</P>
<P><FONT SIZE=2>_______________________________________________</FONT>
<BR><FONT SIZE=2>To control your jdom-interest membership:</FONT>
<BR><FONT SIZE=2><A HREF="http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com" TARGET="_blank">http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com</A></FONT>
</P>
<BR>
<P>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 systems.</P>
</BODY>
</HTML>