Gérer un document XML en Java
Bonjour
je suis en train de créer et modifier un fichier xml avec netbeans. J'ai déjà créé un document avec le code suivant:
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
| package tp1;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class Exercice1 {
static Element racine=new Element("personnes");
static org.jdom.Document document =new Document(racine);
static void enregistrer(String fichier) {
try {
XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
sortie.output(document, new FileOutputStream(fichier));
} catch (IOException e) {
}
}
public static void main(String[]args) {
Element etudiant=new Element("element");
racine.addContent(etudiant);
Attribute classe =new Attribute("classe","LFIM3");
etudiant.setAttribute(classe);
Element nom=new Element("nom");
nom.setText("Votre nom");
etudiant.addContent(nom);
XMLOutputter sortie =new XMLOutputter(Format.getPrettyFormat());
try {
sortie.output(document, System.out);
} catch (IOException ex) {
Logger.getLogger(Exercice1.class.getName()).log(Level.SEVERE, null, ex);
}
enregistrer("D:/3LF/TPXML.xml");
}} |
Je ne sais pas comment réaliser une fonction de suppression d'un élément de document.
Merci
Gérer un document XML en Java
Saut toi, je vois effectivement que tu as utilisé la technologie JDOM pour gérer tes fichiers XML. Ainsi comme te l'a conseillé Thelvin tu dois parser ton fichier XML puis parcourir celui-ci a travers ces itérations afin de récupérer le noeud que tu désires supprimer puis appliquer l'instruction de fruwen7. Là je pense que ça devrait marcher. a + :ccool: