<!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(&quot;user&quot;);</FONT>
<BR><FONT SIZE=2>will always remove the first child name &quot;user&quot;</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>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</FONT>
<BR><FONT SIZE=2>&lt;users&gt;</FONT>
<BR><FONT SIZE=2>&nbsp; &lt;user username=&quot;qasim&quot; url=&quot;143.53.31.174&quot; /&gt;</FONT>
<BR><FONT SIZE=2>&nbsp; &lt;user username=&quot;nabil&quot; url=&quot;localhost&quot; /&gt;</FONT>
<BR><FONT SIZE=2>&lt;/users&gt;</FONT>
<BR><FONT SIZE=2>&nbsp; </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>&nbsp;&nbsp;&nbsp; {</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Element root =document.getRootElement();</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT SIZE=2> List users = root.getChildren(&quot;user&quot;);&nbsp;&nbsp; </FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT SIZE=2> Iterator i=users.iterator();&nbsp;&nbsp; </FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT SIZE=2> while (i.hasNext()) </FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT SIZE=2> {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT SIZE=2> Element user= (Element)i.next();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT SIZE=2> int count=0;</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT SIZE=2> </FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String u=user.getAttributeValue(&quot;username&quot;);</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String l=user.getAttributeValue(&quot;url&quot;);</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT SIZE=2> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int q = username.compareTo(u);</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT SIZE=2> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int r= location.compareTo(l);</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT SIZE=2> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ((q==0) || (r==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nbsp;&nbsp;<FONT SIZE=2> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT SIZE=2> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(&quot;none deleted &quot;);</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT SIZE=2> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT SIZE=2> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT SIZE=2> // increment number of description tag found&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT SIZE=2> /// end of loop//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT SIZE=2> </FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>}</FONT>
<BR><FONT SIZE=2>---------------------------------</FONT>
<BR><FONT SIZE=2>&nbsp;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.&nbsp; 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>