Bonjour à tous,
J'ai besoin de votre aide car j'ai besoin de créer un document XML en entrant dans des valeurs pour les balises et leurs attributs.
Actuellement, lorsque je lance mon programme et que je crée plusieurs éléments (balise + attribut), seul le dernier élément est affiché dans le document.
J'ai essayé par plusieurs moyens de faire une boucle mais le résultat est toujours le même.
Voici le code de l'application :

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
package xml;
 
import javax.swing.JOptionPane;
import javax.xml.parsers.*; 
import org.w3c.dom.*; 
import org.xml.sax.*; 
import javax.xml.transform.*; 
import javax.xml.transform.dom.*; 
import javax.xml.transform.stream.*; 
import java.io.*; 
 
public class CreationDOM{
	public static String txt;
	public static String txt2;
	public static String txt3 = "";
 
	public static void transformerXml(Document document, String fichier) {
        try {
            // Creation of the DOM source
            Source source = new DOMSource(document);
 
            // Creation of the XML file
            File file = new File(fichier);
            Result resultat = new StreamResult(fichier);
 
            // Configuration of the transformer
            TransformerFactory fabrique = TransformerFactory.newInstance();
            Transformer transformer = fabrique.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
 
            // Transformation
            transformer.transform(source, resultat);
        }catch(Exception e){
        	e.printStackTrace();	
        }
    }
 
	public static void creationDOM(){
 
		try{
			// Creation of a new DOM
			DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance();
			DocumentBuilder constructeur = fabrique.newDocumentBuilder();
			Document document = constructeur.newDocument();
 
 
			// Properties of the DOM
			document.setXmlVersion("1.0");
			document.setXmlStandalone(true);
 
			// Creation of the tree of the DOM
			Element racine = document.createElement("Model");
			racine.appendChild(document.createComment("This is a XML document for a model"));
 
 
			while((txt!=null) && (txt2!=null))
			{
			Element modele = document.createElement(txt);
			modele.appendChild(document.createTextNode(txt2));
			racine.appendChild(modele);
			//txt3 += txt + "," + txt2 + ","; 
			//Element nom = document.createElement(txt2);
 
			//nom.setTextContent(txt2);
			//racine.appendChild(nom);
 
			document.appendChild(racine);
			}
 
 
 
			//Save the DOM in the following XML File
			transformerXml(document, "./Project.xml");
 
		}catch(Exception e){
			e.printStackTrace();
		}
 
 
	}	
 
	public static void main(String args[])
	{
		while(true)
		{
 
			txt= JOptionPane.showInputDialog(null, "Enter a name for a tag");
			if (txt == null)
			{
				 System.out.println("No name given for the tag");
				 break;
			}
			else 
			{
				System.out.println("Name of the tag : " + txt);
			}
 
 
			txt2= JOptionPane.showInputDialog(null, "Enter a name for an attribute");
			if (txt2 == null)
			{
				System.out.println("No name given for the attribute");
				break;
			}
			else 
			{
			     System.out.println("Name of the attribute : " + txt2);
			}
 
			creationDOM();
			//System.exit(1);
 
		}	
 
		}
}
Merci d'avance pour votre aide