[XOM] MultipleParentException lors de la création d'un fichier XML
Bonjour,j'essaye de créer un fichier XML avec XOM depuis tout à l'heure mais je bloque sur cette exception :
Citation:
Exception in thread "main" nu.xom.MultipleParentException: [nu.xom.Element: proposition] child already has a parent.
at nu.xom.Element.insertionAllowed(Unknown Source)
at nu.xom.ParentNode._insertChild(Unknown Source)
at nu.xom.ParentNode.insertChild(Unknown Source)
at nu.xom.ParentNode.appendChild(Unknown Source)
J'ai essayé de modifier mon code,mais je ne vois pas où est le problème car je ne vois qu'un seul parent pour tous les nœuds.
Voici mon code,il consiste à insérer le contenu d'un ArrayList dans un fichier XML :
Code:
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
| Element root = new Element("Quiz");
Attribute id = new Attribute("ID","0");
id.setType(Attribute.Type.ID);
root.addAttribute(id);
Document doc = new Document(root);
String p=" ",corr=" ";
Question ques=null;
Iterator<Question> it=liste.iterator();
while(it.hasNext()){
ques=it.next();
Element enonce=new Element("enonce");
enonce.appendChild(ques.enonce);
Element propo=new Element("proposition");
Iterator<String> pro=ques.proposition.iterator();
while(pro.hasNext()){
p=new String(pro.next());
enonce.appendChild(propo);
propo.appendChild(p);
}
Element position=new Element("position_reponse");
corr=String.valueOf(ques.correct);
enonce.appendChild(position);
position.appendChild(corr);
root.appendChild(enonce);
}
try{
FileOutputStream out = new FileOutputStream("inst-new.xml");
Serializer ser = new Serializer(out,"ISO-8859-1");
ser.setIndent(1);
ser.write(doc);
}catch(FileNotFoundException e1){
System.out.println("Fichier non trouve \n"+e1.getMessage());
}catch(UnsupportedEncodingException e2){
System.out.println("Encodage impossible \n"+e2.getMessage());
}
catch (IOException e) {
System.out.println("Probleme ecriture fichier \n"+e.getMessage());
e.printStackTrace();
}
catch(Exception e3){
System.out.println(e3.getMessage());
}
} |
Je remercie d'avance tous ceux qui prendront la peine de me répondre.