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
|
package packageTreatment;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.Parent;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.XMLOutputter;
public class Ajouter {
public static void ajouter(String parNom, String parPrenom, String parEmail, String parTelPortable, String parTelFixe, String parCommentaire)
{
Document document = Ajouter.ajout("WebContent/annuaire.xml", parNom, parPrenom, parEmail, parTelPortable, parTelFixe, parCommentaire);
enregistreFichier(document,"WebContent/NEWannuaire.xml");
}
private static Document ajout(String fileName, String parNom, String parPrenom, String parEmail, String parTelPortable, String parTelFixe, String parCommentaire) throws JDOMException, IOException
{
Document document = null ;
File file = new File(fileName);
SAXBuilder builder = new SAXBuilder();
document = builder.build(file);
//----------------- Création des éléments ------------------ \\
Element personne = new Element("personne");
personne.setAttribute("id",nom) ;
Element varNom = new Element("nom");
varNom.setText(nom);
Element varPrenom = new Element("prenom");
varNom.setText(prenom);
Element varEmail = new Element("email");
varNom.setText(email);
Element varTelPortable = new Element("telPortable");
varNom.setText(telPortable);
Element varTelFixe = new Element("telFixe");
varNom.setText(telFixe);
Element varCom = new Element("commentaire");
varNom.setText(commentaire);
// ----------------- Création de la hiérarchie ---------------- \\
personne.addContent(varNom);
personne.addContent(varPrenom);
personne.addContent(varEmail);
personne.addContent(varTelPortable);
personne.addContent(varTelFixe);
personne.addContent(varCom);
document.getRootElement().addContent(personne);
return document ;
}
private static void enregistreFichier(Document document,String fichier) throws Exception
{
XMLOutputter sortie = new XMLOutputter();
sortie.output(document, new FileOutputStream(fichier));
}
} |
Partager