Re: Removing Elements

Chris Pratt <[email protected]> Mon, 9 Apr 2012 13:22:48 -0700
Newsgroups gmane.comp.java.jdom.general
Message-ID <CAALdY0yqVcmAc1bJYTpeu2RL9eGTbZNYg2Xoj6hotr4csGj5Dw@mail.gmail.com>
You might try iterating backwards through the list.  I would assume that
removing a child might change the indexing of the rest of the children, if
your start at lists.size() - 1 and work your way toward 0, then the rest of
the indexes won't change.
  (*Chris*)

On Mon, Apr 9, 2012 at 12:39 PM, Bashiro <[email protected]> wrote:

> Thanks a lot for the reply. And thanks to both Rolf and Chris!
> I really appreciate this.
>
> What I am doing is; I am searching for  an "ID" in "contacts" to delete.
> I have the following code->
>
>  List lists;
>  Element  next;
>
>  lists = document.getRootElement().getChildren();
>        for (int i = 0; i < lists.size(); i++) {
>            next = (Element) lists.get(i);
>            if (next.getAttribute("contacts").getValue().equals(ID)) {
>                    next.getAttribute("name").getValue();
>                    next.getAttribute("address").getValue();
>                    next.getAttribute("tel").getValue();
>                    next.getAttribute("mobile").getValue();
>                  next.detach();
>
> This only works when the ID  in question is at [0]. I can view the
> contacts in a table too.
> If the contact is in a second row or a different row than first row it
> does not work.
>
> I even tried this;
>  if
> (next.getAttribute("contacts").getValue().equals(ID)&&next.getAttribute("name").getValue().equals(name))
> {
> do.....
> }
>
> Thanks for any help.
>
> Have a nice day!
>
> bashiro
>                    next.detach();
>                System.out.println("REMOVED  LAST " + domainName + "\n");
>
> Bashiro
> Drammen-Norway
>
> -----Original Message-----
> From: "Rolf Lear" [[email protected]]
> Date: 04/09/2012 19:36
> To: "Bashiro" <[email protected]>
> CC: [email protected]
> Subject: Re: [jdom-interest] Removing Elements
>
>
> Hi Brashiro.
>
> I think (at least I hope) you are mistaken.
>
> You should be able to remove an Element in a number of ways....
>
> Assuming that you have a Element emt which is item 3 in the 'parent'
> ContentList, you should be able to do any one of:
>
> emt.detach();
> parent.removeContent(emt);
> parent.getContent().remove(3);
>
> Rolf
>
>
>
>
> On Mon, 09 Apr 2012 12:47:56 -0400, "Bashiro" <[email protected]> wrote:
> > Hello Folks!
> > First of all thanks to the jDom team for version 2.0!
> > Congrats to us all...
> >
> > My question is; I am trying to remove an element in jDom. It only
> removes
> > the element if
> > the element is located at [0].  Is there a good code or a good book to
> > help me with that ?
> >
> > Thanks
> >
> > Bashiro
> > Drammen-Norway
> >
> > _______________________________________________
> > To control your jdom-interest membership:
> > [email protected]">
> http://www.jdom.org/mailman/options/jdom-interest/[email protected]
>
>
>

_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/[email protected]