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 :

Générer l'arborescence d'un JTree dans un fichier XML [JDOM]


Sujet :

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

  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    53
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 53
    Par défaut Générer l'arborescence d'un JTree dans un fichier XML
    Bonjour à tous, tout est dans le titre mais je n'y arrive pas ca fait 4h que je suis dessus je ne vois peut etre pas où je me gourré ... merci pour vos réponses ...

    voici mon code

    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
    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
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Enumeration;
     
    import javax.swing.JOptionPane;
    import javax.swing.tree.DefaultMutableTreeNode;
     
    import org.jdom.*;
    import org.jdom.output.Format;
    import org.jdom.output.XMLOutputter;
     
     
    public class TreetoXML {
     
    	public Element xmlrootelement=null;
    	public Document treetoxmldoc=null;
    	private Element previousroot=null;
     
     
    	public TreetoXML (DefaultMutableTreeNode tametreerootnode) {
     
    		xmlrootelement=new Element("TREE_TO_XML_BY_YOUSS");
    		treetoxmldoc=new Document(xmlrootelement);
     
    		Element newelement = new Element ("MAIN_TREE");
    		previousroot=newelement;
     
     
     
     
    	}
     
    	public void execute (DefaultMutableTreeNode node) { //parentnode is a category that was encountered by the program while scanning the children
     
    		Object o = node.getUserObject();
     
    		if (o instanceof Player) {
     
    			if (node.isRoot()==false) {
     
    				if (node.isLeaf()==false) {
     
    					Player th = (Player) node.getUserObject();
     
    					Element elt = new Element("Category");
    					elt.setAttribute("Name",th.Name);
    					elt.setAttribute("ID",th.ID);
     
    					this.previousroot=elt;
     
    					Enumeration listofdirectchildren = node.children();
     
    					while (listofdirectchildren.hasMoreElements()) {
     
    						DefaultMutableTreeNode currentchild = null;
    						currentchild=(DefaultMutableTreeNode) listofdirectchildren.nextElement();
     
    						if (currentchild.isLeaf()) {
     
    							Player childth = (Player) currentchild.getUserObject();
     
    							Element childelt = new Element("Player");
    							childelt.setAttribute("Name",childth.Name);
    							childelt.setAttribute("ID",childth.ID);
     
    							previousroot.addContent(childelt);
     
    						}
     
    						else {
     
    							this.execute(currentchild);
     
    						}
     
    					}
     
     
    				}
     
    				else {
     
    					Player th = (Player) node.getUserObject();
     
    					Element elt = new Element("Player");
    					elt.setAttribute("Name",th.Name);
    					elt.setAttribute("ID",th.ID);
     
    					previousroot.addContent(elt);
    				}
     
     
    			}
     
    			else {
     
    				xmlrootelement.addContent(this.previousroot);
    				System.out.println("Pour la verif repond a ces question\n" +
    						"-tes root?"+node.isRoot()+
    						"\n-tes qui?"+previousroot.toString());
    			}
    		}
     
    		else {
     
    			JOptionPane.showMessageDialog(null, "Sorry but the node"+node.toString() +" is not " +
    					"consideredas a 'Player' object!\n" +
    					"Note that"+node.toString() +"won't be in the final XML file of the tree.");
    		}
    	}
     
    	public void save () {
     
    		XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
            try {
    			sortie.output(treetoxmldoc, new FileOutputStream("C:/Data/YOUSS/montest.xml"));
    		} catch (FileNotFoundException e1) {
    			System.out.println(e1.getMessage());
    		} catch (IOException e1) {
    			System.out.println(e1.getMessage());
    			}
     
    	}
    }

  2. #2
    Membre averti
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    53
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 53
    Par défaut
    bon ben c'est bon je me suis embrouillé tout simplement ... voici le code solution pour ceux qui se trouveraient coincés dans le meme cas ...
    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
    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
    84
    85
    86
    87
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Enumeration;
     
    import javax.swing.tree.DefaultMutableTreeNode;
     
    import org.jdom.*;
    import org.jdom.output.Format;
    import org.jdom.output.XMLOutputter;
     
     
    public class TreetoXML {
     
    	public Element xmlrootelement=null;
    	public Document treetoxmldoc=null;
     
    	public TreetoXML (DefaultMutableTreeNode MONARBREtreerootnode) {
     
    		xmlrootelement=new Element("MONARBRE");
    		treetoxmldoc=new Document(xmlrootelement);
    		this.execute(MONARBREtreerootnode, xmlrootelement);
     
    	}
     
    	public void execute (DefaultMutableTreeNode node, Element parelement) { //parentnode is a category that was encountered by the program while scanning the children
     
    		Object o = node.getUserObject();
     
    		if (o instanceof Joueur) {
     
    				Enumeration enumchildren = node.children();
     
    				while (enumchildren.hasMoreElements()) {
     
    					DefaultMutableTreeNode currentnode=(DefaultMutableTreeNode) enumchildren.nextElement();
    					Object p = currentnode.getUserObject();
     
    					if (p instanceof Joueur ) {
     
     
     
    							Element newelement;
     
    							if (currentnode.isLeaf()) {
     
    								newelement = new Element("Joueur");
    							}
     
    							else {
     
    								newelement = new Element("Category");
     
    							}
     
    							Joueur newth = null;
    							newth = (Joueur) currentnode.getUserObject();
     
    							newelement.setAttribute("Name", newth.Name);
    							newelement.setAttribute("ID", newth.ID);
     
    							parelement.addContent(newelement);
     
    							if (currentnode.isLeaf()==false) {
     
    							execute(currentnode,newelement);
     
    						}
    					}
    				}
     
    		}
    	}
     
    	public void save () {
     
    		XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
            try {
    			sortie.output(treetoxmldoc, new FileOutputStream("C:/Data/YOUSS/Desktop/jeveumontesteuuu.xml"));
    		} catch (FileNotFoundException e1) {
    			System.out.println(e1.getMessage());
    		} catch (IOException e1) {
    			System.out.println(e1.getMessage());
    			}
     
    	}
    }

  3. #3
    Invité de passage
    Inscrit en
    Février 2011
    Messages
    1
    Détails du profil
    Informations forums :
    Inscription : Février 2011
    Messages : 1
    Par défaut
    Bonjour
    Cela fait 2 ans que vous avez posté votre code,alors je pense pas que vous utilisez le forum maintenant ,mais j'ai toujours espoir, alors SVP j'ai copié collé votre code mais j'ai un problème au niveau de "joueur" a chaque ligne ou il y a cette "joueur" j'ai une erreur,donc SVP est ce que vous pouvez m'aider en indiquant ou puis-je trouvé l'erreur.
    Merci, cordialement.

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

Discussions similaires

  1. Arborescence disque dans un fichier XML
    Par gwen_javagiste dans le forum Format d'échange (XML, JSON...)
    Réponses: 3
    Dernier message: 26/08/2009, 23h13
  2. Insertion dans un fichier xml à partir d'un xsl
    Par alexandre54 dans le forum XSL/XSLT/XPATH
    Réponses: 3
    Dernier message: 21/03/2003, 09h45
  3. Insertion dans un fichier XML
    Par [DreaMs] dans le forum XMLRAD
    Réponses: 4
    Dernier message: 27/02/2003, 17h16
  4. Lire un attribut dans un fichier XML en C++
    Par ti.k-nar dans le forum XML
    Réponses: 2
    Dernier message: 14/10/2002, 15h22
  5. Balises HTML dans un fichier XML
    Par Bastet79 dans le forum XML/XSL et SOAP
    Réponses: 12
    Dernier message: 04/09/2002, 15h29

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