[jdom-interest] Removing an element
Qasim
Q.M.Qasim at Bradford.ac.uk
Wed Mar 6 05:10:45 PST 2002
Thanks for your response.
Actually my initial code was correct ( it could delete the 'user' tag).
However, I was forgetting to save the XML file after the manipulation.
the working code is:
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 us=user.getName();
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.removeContent(user);
System.out.println("removed "+ us);
count++;
}
if (count==-1)
{
System.out.println("none deleted ");
}
}
try {
XMLOutputter outputter = new XMLOutputter(" ", true);
//output to a file
FileWriter writer = new FileWriter("users.xml");
outputter.output(document, writer);
writer.close();
} catch(java.io.IOException e) {
System.err.println("Problem with writing to file " );
e.printStackTrace();
}
}
Thanks
Qasim
r 2002 18:48:36 -0000 James Cook
<James.Cook at jobpartners.com> wrote:
> Your line:
> root.removeChild("user");
> 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:
> users.remove(i);
>
> 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.
>
> Regards
> James
>
> -----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
>
>
> 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
>
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhos
> t.com
>
>
> 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.
---------------------------------
Qasim
Q.M.Qasim at bradford.ac.uk
More information about the jdom-interest
mailing list