Création d'un fichier xml

hajjajiayda Messages postés 1 Date d'inscription mercredi 4 mars 2009 Statut Membre Dernière intervention 11 mars 2009 - 11 mars 2009 à 09:32
pegase31 Messages postés 6138 Date d'inscription dimanche 21 décembre 2003 Statut Modérateur Derniè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)>


< ! ELEMENT  categorie (actualite, nature, voyage, sport, voiture, animaux, autre)>


< ! ELEMENT portrait (valeur1, mot_portrait+)>
< ! ELEMENT  valeur1                       (true|false)>
< ! ELEMENT  mot_portrait          # PCDATA>


< ! ELEMENT  nature      (valeur2, mot_nature+)>
< ! ELEMENT  valeur2                        (true|false)>
< ! ELEMENT  mot_nature              # PCDATA>


< ! ELEMENT  voyage (valeur3, mot_voyage+)>
< ! ELEMENT  valeur3                       (true|false)>
< ! ELEMENT  mot_voyage           # PCDATA>


< ! ELEMENT  sport (valeur4, mot_sport+)>
< ! ELEMENT  valeur4                         (true|false)>
< ! ELEMENT  mot_sport            # PCDATA> ] >




< ! ELEMENT  voiture     (valeur5, mot_voiture+)>
< ! ELEMENT  valeur5                        (true|false)>
< ! ELEMENT  mot_voiture              # PCDATA>


< ! ELEMENT  animaux (valeur6, mot_animaux+)>
< ! ELEMENT  valeur6                         (true|false)>
< ! ELEMENT  mot_animaux            # PCDATA> ] >


< ! ELEMENT  autre (valeur7, mot_autre)>
< ! ELEMENT  valeur7                         (true|false)>
< ! ELEMENT  mot_autre            # PCDATA> ] >

Et voila le code que j'ai saisie sur le logiciel NetBeans et ça ne marche plus:

package nossystème;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author Ayda
 */


 


 




import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.w3c.dom.NodeList.*;
import javax.xml.transform.*;
import javax.xml.transform.TransformerFactory.*;
import javax.xml.transform.Transformer.*;
import javax.xml.transform.stream.StreamResult.*;
import javax.xml.transform.dom.DOMSource.*;




import java.util.*;




import java.io.*;
import org.jdom.*;
import org.jdom.output.*;
import org.jdom.Attribute.*;
import org.jdom.Document;
import org.jdom.Element;


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){}
}
}

S'il vous plais, trouvez une solution pour moi.

1 réponse

pegase31 Messages postés 6138 Date d'inscription dimanche 21 décembre 2003 Statut Modérateur Dernière intervention 4 septembre 2013 12
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.

Bon courage à toi

Peg'
0
Rejoignez-nous