delete element from xml file

View: New views
3 Messages — Rating Filter:   Alert me  

delete element from xml file

by meriem bessais :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.

I want to delete an element from xml-file. this is my code but the file dosen't change when I test it.

 

 

public static String supp(){

            try{

            racine=document.getRootElement();

            Element rac=racine.getChild("voitures");

               List   listelt = rac.getChildren();

                  int ok=1;

               Iterator i = listelt.iterator();

             while(i.hasNext()&& ok==1)

           {

               Element courrent = (Element)i.next();

               if (courrent.getAttribute("immatricule").getValue().equals("8345 tU 125")){

                   System.out.println(" "+courrent.getAttribute("immatricule").getValue());

                     //Element free = (Element)courrent.detach();

                    System.out.println(rac.getName());

                   

                    rac.removeContent(courrent);

                     ok=0;

                     //courrent.removeChild(couleur);

                     System.out.println("supprimé");

               }

               

           }}catch(Exception e){return e.getMessage();}

 

 

 

 

 


_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@...

Re: delete element from xml file

by Laurent Bihanic :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

You should not iterate on children and modify the content directly on the
element at the same time.
In your for() loop, use i.remove() to remove the current element.

Laurent


meriem bessais a écrit :

> *I want to delete an element from xml-file. this is my code but the file
> dosen't change when I test it.*
>
> *public* *static* String supp(){
>             *try*{
>             /racine/=/document/.getRootElement();
>             Element rac=/racine/.getChild("voitures");
>                List   listelt = rac.getChildren();
>                   *int* ok=1;
>                Iterator i = listelt.iterator();
>              *while*(i.hasNext()&& ok==1)
>            {
>                Element courrent = (Element)i.next();
>                *if*
> (courrent.getAttribute("immatricule").getValue().equals("8345 tU 125")){
>                    System./out/.println("
> "+courrent.getAttribute("immatricule").getValue());
>                      //Element free = (Element)courrent.detach();
>                     System./out/.println(rac.getName());
>
>                     rac.removeContent(courrent);
>                      ok=0;
>                      //courrent.removeChild(couleur);
>                      System./out/.println("supprimé");
>                }
>            }}*catch*(Exception e){*return* e.getMessage();}
_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@...

RE: delete element from xml file

by Deepa Janamatti (RBIN/EMT2) :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

Using an Iterator might lead to ConcurrentModificationException. So, use ListIterator instead of Iterator to remove the element.

Regards,
Deepa

-----Original Message-----
From: jdom-interest-bounces@... [mailto:jdom-interest-bounces@...] On Behalf Of Laurent Bihanic
Sent: Tuesday, 16. December 2008 4:45 AM
To: meriem bessais
Cc: jdom-interest@...
Subject: Re: [jdom-interest] delete element from xml file

Hi,

You should not iterate on children and modify the content directly on the
element at the same time.
In your for() loop, use i.remove() to remove the current element.

Laurent


meriem bessais a écrit :

> *I want to delete an element from xml-file. this is my code but the file
> dosen't change when I test it.*
>
> *public* *static* String supp(){
>             *try*{
>             /racine/=/document/.getRootElement();
>             Element rac=/racine/.getChild("voitures");
>                List   listelt = rac.getChildren();
>                   *int* ok=1;
>                Iterator i = listelt.iterator();
>              *while*(i.hasNext()&& ok==1)
>            {
>                Element courrent = (Element)i.next();
>                *if*
> (courrent.getAttribute("immatricule").getValue().equals("8345 tU 125")){
>                    System./out/.println("
> "+courrent.getAttribute("immatricule").getValue());
>                      //Element free = (Element)courrent.detach();
>                     System./out/.println(rac.getName());
>
>                     rac.removeContent(courrent);
>                      ok=0;
>                      //courrent.removeChild(couleur);
>                      System./out/.println("supprimé");
>                }
>            }}*catch*(Exception e){*return* e.getMessage();}
_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@...

_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@...