Bonjour, j'ai écrit un code pour supprimer un élément dans un fichier XML. Ce code fonctionne, oui....mais qu'une seule fois.
Si je le lance, ça me supprime l'élément souhaité, mais je ne peux pas faire une deuxième suppression ( je ne peux pas faire d'ajout non plus).Je ne peux vraiment plus rien faire après cette première suppression.
Voici mon code :
D'où vient le problème ?
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 package com.dis.mercure.struts.actions; import java.io.*; import org.jdom.*; import org.jdom.input.*; import org.jdom.output.*; import java.util.List; import java.util.Iterator; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.DynaActionForm; import org.jdom.Document; public class testSuppr_Action extends Action { static String fichierd; static Document document; static Element racine; static String chemindContent; static String elmtdContent; static String element; public testSuppr_Action(){} public ActionForward execute(final ActionMapping mapping, final ActionForm form, final HttpServletRequest request, final HttpServletResponse response) throws Exception { final DynaActionForm theForm = (DynaActionForm) form; chemindContent = theForm.getString("chemind"); elmtdContent =theForm.getString("elmtd"); try { lireFichier(); supprElement(); enregistreFichier(); } catch(Exception e){ System.out.println(e); } return (mapping.findForward("failure")); } public static void main(String[] args){} //On parse le fichier et on initialise la racine de //notre arborescence static void lireFichier() throws Exception { SAXBuilder sxb = new SAXBuilder(); document = sxb.build(new File(chemindContent)); for(int i=0;i<document.getContentSize();i++){ } racine = document.getRootElement(); } //On fait des modifications sur un Element static void supprElement() { //Dans un premier temps on liste tous les types List listType = racine.getChildren("type"); Iterator i = listType.iterator(); //On parcours la liste grâce à un iterator while(i.hasNext()) { Element courant = (Element)i.next(); //Si le type possède une valeur, on applique la modification if(courant.getChild(elmtdContent)!=null ) { //On supprime l'Element en question courant.removeChild(elmtdContent); //On renomme l'Element père sachant qu'une balise XML n'accepte //ni les espaces ni les caractères spéciaux //"type modifié" devient "type_modifie" courant.setName("type_modifie"); } } } //On enregistre notre nouvelle arborescence dans le fichier //d'origine dans un format classique. static void enregistreFichier() throws Exception { XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat()); sortie.output(document, new FileOutputStream(chemindContent)); } }
Partager