| 12
 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
 
 | public class CreationDOM{
	public static void transformerXml(Document document, String fichier) {
 
		try {
 
 
            // Création de la source DOM
            Source source = new DOMSource(document);
 
            // Création du fichier de sortie
            File file = new File(fichier);
            Result resultat = new StreamResult(fichier);
 
            // Configuration du 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();	
        }
 
 
		try{
 
 
			// Propriétés du DOM
			document.setXmlVersion("1.0");
			document.setXmlStandalone(true);
 
			// Création de l'arborescence du DOM
			Element racine = document.createElement("annuaire");
			racine.appendChild(document.createComment("Commentaire sous la racine"));
 
			Element personne = document.createElement("personne");
			personne.setAttribute("id","0");
			racine.appendChild(personne);
 
			Element nom = document.createElement("nom");
			nom.setTextContent("un nom");
			personne.appendChild(nom);
 
			Element prenom = document.createElement("prenom");
			prenom.setTextContent("un prénom");
			personne.appendChild(prenom);
 
			Element adresse = document.createElement("adresse");
			adresse.setTextContent("une adresse");
			personne.appendChild(adresse);
 
			document.appendChild(racine);
 
			//Sauvegarde du DOM dans un fichier XML
 
			transformerXml(document, "./NouveauDocument.xml");
		}catch(Exception e){
			e.printStackTrace();
 
	}	
	}
} | 
Partager