slt, voilà je souhait après avoir récupère dans un objet certain donnée enregistre dans un fichier xml l'objet mais voilà lorsque j'exécute le programme et que je lancer l'insertion il me lève un exception de type IndexOutOfBounds Exception que je ne comprend pas et si je relance le programme l'insertion est effectué. si je relance a nouveau le programme au lieu de continue l'insertion l'exception précédente est léve.ensuite lorsque insertion est réaliser dans mon fichier xml au niveau de l'attribut id de l'élément object les valeurs vont en paire de 2 au lieu d'une incrémentation 0,1,2,.......
bref voici mon code:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.List;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
 
 
public class Newtestmoteur {
    public static void main(String[] args){
       try{
 
           new Newtestmoteur().new moteur().traitement();
       }
       catch(JDOMException e){
           System.out.println(e.getMessage());
           e.printStackTrace();
       }
        catch(NullPointerException ex){
           System.out.println(ex.getMessage());
           ex.printStackTrace();
       }
       catch(IOException ex){
           System.out.println(ex.getMessage());
           ex.printStackTrace();
       }
       catch(Exception ex){
           System.out.println(ex.getMessage());
           ex.printStackTrace();
       }
    }
         public class Terme {
        private String terme;
 
         public void Setterme(String e){
             if(e.isEmpty())
                  throw new NullPointerException("veuiller entre le terme");
             if(e.equals(""))
                 throw new NullPointerException("veuiller entre le terme");
             else
                 terme=e;
         }
         public String getterme(){
             return  terme;
         }
    }
    public class Def{
         private String definition;
 
            public void Setdefinition(String e){
             if(e.isEmpty())
                  throw new NullPointerException("veuiller entre la défintion");
             if(e.equals(""))
                 throw new NullPointerException("veuiller entre la défintion");
             else
                 definition=e;
         }
             public String getdefinition(){
             return  definition;
         }
    }
   public class moteur {
        Def definition;
        Terme terme;
        String r;
        int choix;
        boolean n_existe=true;
        BufferedReader in=null;
        Element racine =new Element("enciclopedie");
        Document document=new Document(racine);
        org.jdom.Element racine2;
        org.jdom.Document document2;
        List liste;
        public moteur (){
            in=new BufferedReader(new InputStreamReader(System.in));
            definition=new Def();
            terme =new Terme();
        }
        public  void creation() throws IOException{
        String textfile="support.xml";
        XMLOutputter sortie =new XMLOutputter(Format.getPrettyFormat());
        sortie.output(document,new FileOutputStream(textfile));
    }
       public  void affiche() throws IOException{
       XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
       sortie.output(document,System.out);
    }
      public void enregistre() throws FileNotFoundException, IOException {
       String textfile="support.xml";
       XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
       sortie.output(document, new FileOutputStream(textfile));
    }
     public int parcourire() throws JDOMException, IOException{
       String textfile="support.xml";
       SAXBuilder sas=new SAXBuilder();
       document2=sas.build(new File(textfile));
       racine2=document2.getRootElement();
       liste=racine2.getChildren("object");
       System.out.println(liste.size());
       return liste.size();
   }
     public String taille(Element q){
           List  tail=q.getChildren("object");
             return ""+tail.size();
        }
     public void ajout(String s,Terme e,Def f) throws FileNotFoundException, IOException{
       Element object=new Element("object");
       racine.addContent(0,object);
       Attribute type= new Attribute("type",s);
       object.setAttribute(type);
       r="0";
       Attribute id=new Attribute("id",r);
       object.setAttribute(id);
       Element term =new Element("terme");
       term.setText(e.getterme());
       object.addContent(term);
       Element defi =new Element("définition");
       defi.setText(f.getdefinition());
       object.addContent(defi);
       enregistre();
     }
     public void ajout2(int a,String s,Terme e,Def f) throws FileNotFoundException, IOException, Exception{
       Iterator i =liste.iterator();
       while(i.hasNext()){
           Element courant=(Element)i.next();
           System.out.println(courant.getChild("terme").getTextTrim());
            if(courant.getChild("terme").getTextTrim().equals(e.getterme())){
                     n_existe=false;
                }
           }
          if(!n_existe)
               throw new Exception("terme est déjà présent dans les archives");
          if(n_existe){
       Element object=new Element("object");
       racine.addContent(a,object);
       Attribute type= new Attribute("type",s);
       object.setAttribute(type);
       r=taille(racine);
       Attribute id=new Attribute("id",r);
       object.setAttribute(id);
       Element term =new Element("terme");
       term.setText(e.getterme());
       object.addContent(term);
       Element defi =new Element("définition");
       defi.setText(f.getdefinition());
       object.addContent(defi);
      }
       enregistre();
     }
     public void inserer(String a,Terme b,Def c) throws JDOMException, IOException,FileNotFoundException, Exception{
        int j=parcourire();
        Iterator i =liste.iterator();
        while(i.hasNext()){
           Element courant=(Element)i.next();
           System.out.println(courant.getChild("terme").getTextTrim());
       }
        if(j==0){
              ajout(a, b, c);
              return;
               }
        if(j>0){
            ajout2(j, a, b, c);
            return;
        }
        }
 
      public void insertion() throws IOException, JDOMException,FileNotFoundException,Exception{
           String typ;
           System.out.println("type de l'object :");
           typ=in.readLine();
           System.out.println("terme object:");
           terme.Setterme(in.readLine());
           System.out.println("définition object :");
           definition.Setdefinition(in.readLine());
           inserer(typ,terme,definition);
         }
      public  void supprimer(){
 
    }
       public  void traitement() throws IOException, NullPointerException, Exception{
       in=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("1:pour creation");
        System.out.println("2:pour ajouter");
        System.out.println("3:pour supprimer");
        System.out.println("4:pour afficher");
        System.out.println("choix:");
        choix=Integer.parseInt(in.readLine());
        switch(choix){
            case 1:
                  creation();
                  System.out.println("5:pour retour");
                  choix=Integer.parseInt(in.readLine());
            case 2:
                  insertion();
                  System.out.println("5:pour retour");
                  choix=Integer.parseInt(in.readLine());
            case 3:
                 supprimer();
                 System.out.println("5:pour retour");
                 choix=Integer.parseInt(in.readLine());
            case 4:
                 affiche();
                 System.out.println("5:pour retour");
                 choix=Integer.parseInt(in.readLine());
            case 5:
                traitement();
            default:
                 traitement();
        }
       }
    }
}
voici mon fichier xml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
 
   <?xml version="1.0" encoding="UTF-8"?>
<enciclopedie>
  <object type="doc" id="0">
    <terme>toto</terme>
    <définition>tata</définition>
  </object>
   <object type="doc" id="2">
    <terme>tete</terme>
    <définition>titi</définition>
  </object>
</enciclopedie>
que faire toute aide sera là bien venu
merci