IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Format d'échange (XML, JSON...) Java Discussion :

Sérialiser un conteneur d'objet java en xml [XStream]


Sujet :

Format d'échange (XML, JSON...) Java

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mars 2012
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2012
    Messages : 3
    Points : 7
    Points
    7
    Par défaut Sérialiser un conteneur d'objet java en xml
    Bonjour,
    Je souhaite sérialiser et de-sérialiser en xml une hastable qui est un conteneur d'objet java.
    Pour l'instant j'ai réussi a mettre en œuvre grâce a la bibliothèque XStream la
    sérialisation de l'instance d'objet simple et leurs dé-sérialisation mais dès que je souhaite sérialiser un conteneurs d'objet quelconque j'obtiens un fichier xml incomplet et inexploitable.
    Je me suis alors poser la question si il ne s’agissait pas d'un problème avec les DTD créer automatiquement par la bibliothèque XStream et savoir si il exister d'autre solutions malheuresement je n'aie rien trouvé de concluant donc j'en appelle a votre aide.
    Donc voici l'objet sérialiser
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
     package Personne;
     
    import java.io.*;
     
     
     
    public class Personne 
    {
     
            private String nom;
            private String prenom;
     
            public String getNom() 
    		{
                    return nom;
            }
            public void setNom(String nom) 
    		{
                    this.nom = nom;
            }
            public String getPrenom() 
    		{
                    return prenom;
            }
            public void setPrenom(String prenom) 
    		{
                    this.prenom = prenom;
            }
            public String toString()
            {
                    return this.nom + " " + this.prenom;
            }
    }
    et voici le programme qui sérialise
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    import java.util.*;
    import java.io.*;
    import Personne.Personne;
    import com.thoughtworks.xstream.XStream;
    import com.thoughtworks.xstream.io.xml.DomDriver;
     
     
    public class TestHashtable {
     
      public static void main(String[] args) 
      {
    	//String toto;
    	XStream xstream = new XStream(new DomDriver());
     
    	Personne p1 = new Personne();
    	Personne p2 = new Personne();
    	p1.setNom("1");
    	p1.setPrenom("10");
     
    	p2.setNom("2");
    	p2.setPrenom("20");
     
        Hashtable  htable = new Hashtable();
        htable.put(new Integer(3), new Integer(30));
        htable.put(new Integer(1), new Integer(10));
        htable.put(new Integer(2), new Integer(20));
    	//toto = htable.get(new Integer(2));
        System.out.println(htable.get(new Integer(2)));
     
    	Hashtable  htableP = new Hashtable();
        htable.put(p1.getNom(),p1.getPrenom());
        htable.put(p2.getNom(),p2.getPrenom());
     
    	//toto = htable.get(new Integer(2));
        System.out.println(htable.get("1"));
     
     
    	try {
               FileOutputStream fout = new FileOutputStream("personne2.xml");
               xstream.toXML(htableP, fout);
    		   //xstream.toXML(p2, fout);
            } 
     
    	 catch (FileNotFoundException e) 
    		{
                  e.printStackTrace();
            }
     
     
     
      }
    }
    et voici une partis de la documentation que j'ai utilisé
    http://ericreboisson.developpez.com/...a/xml/xstream/

    En vous remerciant d'avance pour vos idées!

  2. #2
    Modérateur

    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    12 551
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 12 551
    Points : 21 607
    Points
    21 607
    Par défaut
    Je ne suis pas tellement amateur de Xstream, mais je remarque que ton objet htableP, tu ne mets jamais rien dedans.
    Autrement dit, s'il a l'air vide, c'est probablement lié au fait qu'il est vide.
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  3. #3
    Futur Membre du Club
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mars 2012
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2012
    Messages : 3
    Points : 7
    Points
    7
    Par défaut
    Oui je m' excuse pour l'erreur de code
    donc voici la rectification
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    import java.util.*;
    import java.io.*;
    import Personne.Personne;
    import com.thoughtworks.xstream.XStream;
    import com.thoughtworks.xstream.io.xml.DomDriver;
     
     
    public class TestHashtable {
     
      public static void main(String[] args) 
      {
     
    	XStream xstream = new XStream(new DomDriver());
     
    	Personne p1 = new Personne();
    	Personne p2 = new Personne();
    	Personne p3 = new Personne();
     
    	p1.setNom("1");
    	p1.setPrenom("10");
     
    	p2.setNom("2");
    	p2.setPrenom("20");
     
     
     
        Hashtable  htable = new Hashtable();
        htable.put(new Integer(3), new Integer(30));
        htable.put(new Integer(1), new Integer(10));
        htable.put(new Integer(2), new Integer(20));
     
        System.out.println(htable.get(new Integer(2)));
     
     
        htable.put(p1.getNom(),p1.getPrenom());
        htable.put(p2.getNom(),p2.getPrenom());
    	htable.put(p3.getNom(),p3.getPrenom());
     
     
        System.out.println(htable.get("1"));
     
     
    	try {
               FileOutputStream fout = new FileOutputStream("personne2.xml");
               xstream.toXML(htable, fout);
     
            } 
     
    	 catch (FileNotFoundException e) 
    		{
                  e.printStackTrace();
            }
     
     
     
      }
    }
    et voici le résultat de la sérialisation
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
     
    <hashtable>
      <entry>
        <string>2</string>
        <string>20</string>
      </entry>
      <entry>
        <string>1</string>
        <string>10</string>
      </entry>
      <entry>
        <int>3</int>
        <int>30</int>
      </entry>
      <entry>
        <int>2</int>
        <int>20</int>
      </entry>
      <entry>
        <int>1</int>
        <int>10</int>
      </entry>
    </hashtable>
    Donc le problème ici est q' il n'y a pas de balise Personne mais entry ce qui n'aie pas le nom des classes contenus dans le conteneurs d'objet java.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Sérialiser un ArrayList java en XML
    Par absot dans le forum XML/XSL et SOAP
    Réponses: 0
    Dernier message: 22/12/2009, 19h26
  2. sauvegarder un objet java dans un attribut xml
    Par younder dans le forum Format d'échange (XML, JSON...)
    Réponses: 1
    Dernier message: 06/01/2009, 11h15
  3. [Axis] Désérialisation XML vers Objet Java
    Par jemini_fr dans le forum Services Web
    Réponses: 2
    Dernier message: 10/12/2007, 14h38
  4. sauvegarder un objet java avec XML
    Par dark2 dans le forum Format d'échange (XML, JSON...)
    Réponses: 13
    Dernier message: 07/02/2007, 13h10
  5. [SAX] Passer d'objet java en fichier XML?
    Par spoutyoyo dans le forum Format d'échange (XML, JSON...)
    Réponses: 15
    Dernier message: 05/01/2005, 08h31

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo