hajjajiayda
Messages postés1Date d'inscriptionmercredi 4 mars 2009StatutMembreDernière intervention11 mars 2009
-
11 mars 2009 à 09:32
pegase31
Messages postés6138Date d'inscriptiondimanche 21 décembre 2003StatutModérateurDernière intervention 4 septembre 2013
-
11 mars 2009 à 09:41
Salut, j'ai une formulaire qui contient des TextField, et je veux extraire les données de ces "TextField" et les enregistrer dans un fichier Xml et voila la DTD du fichier xml:
<?xml version="1.0" encoding="UTF-8" Sandalone='yes'?>
<!DOCTYPE Jdom [
< ! ELEMENT image (titre, créateur, description, taille, format , date, catégorie+)>
< ! ELEMENT titre #PCDATA>
< ! ELEMENT description #PCDATA>
< ! ELEMENT createur #PCDATA>
< ! ELEMENT taille #PCDATA>
< ! ELEMENT format #PCDATA>
< ! ELEMENT date_creation #PCDATA>
< ! ELEMENT droit_dacces (oui|non)>
public class Jdom
{Annotation an=new Annotation();
//Nous allons commencer notre arborescence en créant la racine XML
//qui sera ici "personnes".
static Element racine = new Element("media");
//On crée un nouveau Document JDOM basé sur la racine que l'on vient de créer
static org.jdom.Document document = new Document(racine);
public Jdom()
{
//On crée un nouvel Element etudiant et on l'ajoute
//en temps qu'Element de racine
Element image = new Element("image");
racine.addContent(image);
//On crée un nouvel Element nom, on lui assigne du texte
//et on l'ajoute en temps qu'Element de etudiant
Element titre = new Element("titre");
titre .setText(an.titre.getText());
image.addContent(titre);
Element description = new Element("description");
description.setText(an.description.getText());
image.addContent(description);
Element createur = new Element("createur");
createur.setText(an.createur.getText());
image.addContent(createur);
Element taille = new Element("taille");
taille .setText(an.taille.getText());
image.addContent(taille);
Element format = new Element("format");
format.setText(an.format.getText());
image.addContent(format);
Element date_creation = new Element("date_creation");
date_creation .setText(an.date_de_creation.getText());
image.addContent(date_creation);
Element categorie= new Element("categorie");
Element actualite = new Element("Actualite");
Attribute valeur1 = new Attribute("Valeur1","true");
actualite.setAttribute("valeur1", an.cactualite.toString());
Attribute mot_actualite = new Attribute("mot_actualite","actualite");
actualite.setAttribute("mot_actualite", an.actualite.getText());
categorie.addContent(actualite);
Element nature= new Element("Nature");
Attribute valeur2 = new Attribute("Valeur2","true");
actualite.setAttribute("valeur2", an.cnature.toString());
Attribute mot_nature = new Attribute("mot_nature","nature");
actualite.setAttribute("mot_nature", an.nature.getText());
categorie.addContent(nature);
Element voyage=new Element("Voyage");
Attribute valeur3 = new Attribute("Valeur3","false");
voyage.setAttribute("valeur3", an.cvoyage.toString());
Attribute mot_voyage = new Attribute("mot_voyage"," ");
voyage.setAttribute("mot_voyage", an.voyage.getText());
categorie.addContent(voyage);
Element sport= new Element("Sport");
Attribute valeur4 = new Attribute("Valeur4","true");
sport.setAttribute("valeur4", an.csport.toString());
Attribute mot_sport = new Attribute("mot_sport"," tennis");
sport.setAttribute("mot_sport", an.sport.getText());
categorie.addContent(sport);
Element voiture= new Element("Voiture");
Attribute valeur5 = new Attribute("Valeur5","true");
voiture.setAttribute("valeur5", an.cvoiture.toString());
Attribute mot_voiture = new Attribute("mot_voiture"," megane rouge");
voiture.setAttribute("mot_voiture", an.voiture.getText());
categorie.addContent(voiture);
Element animaux= new Element("Animaux");
Attribute valeur6 = new Attribute("Valeur6","false");
animaux.setAttribute("valeur6", an.canimaux.toString());
Attribute mot_animaux = new Attribute("mot_animaux"," ");
animaux.setAttribute("mot_animaux", an.animaux.getText());
categorie.addContent(animaux);
Element autre=new Element("Autre");
Attribute valeur7 = new Attribute("valeur7","false");
autre.setAttribute("valeur7", an.cautre.toString());
Attribute mot_autre = new Attribute("mot_autre"," ");
autre.setAttribute("mot_autre", an.autre.getText());
categorie.addContent(autre);
image.addContent(categorie);
Element droit_dacces = new Element("droit_dacces");
droit_dacces.setText(an.droit_dacces.getSelectedItem().toString());
image.addContent(droit_dacces);
//Les deux méthodes qui suivent seront définies plus loin dans l'article
affiche();
enregistre("xml.xml");
}
//Ajouter ces deux méthodes à notre classe JDOM1
static void affiche()
{
try
{
//On utilise ici un affichage classique avec getPrettyFormat()
XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
sortie.output(document, System.out);
}
catch (java.io.IOException e){}
}
static void enregistre(String fichier)
{
try
{
//On utilise ici un affichage classique avec getPrettyFormat()
XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
//Remarquez qu'il suffit simplement de créer une instance de FileOutputStream
//avec en argument le nom du fichier pour effectuer la sérialisation.
sortie.output(document, new FileOutputStream("xml.xml"));
}
catch (java.io.IOException e){}
}
}
pegase31
Messages postés6138Date d'inscriptiondimanche 21 décembre 2003StatutModérateurDernière intervention 4 septembre 201312 11 mars 2009 à 09:41
Bonjour,
"S'il vous plais, trouvez une solution pour moi." .. heu, non, on va t'aider à trouver une solution, mais on ne vas pas la trouver à ta place ...
Si le premier paquet que tu as fournis est un xml, je peux te dire qu'il est foireux car toutes tes lignes sont des commentaires. Je te conseille fortement d'aller faire un tour sur l'excellent tuto de BlackWizzard dispo sur ce site, concernant la structure du XML.