Generer l'arborescence d'un JTree dans un fichier XML

cs_coltman Messages postés 97 Date d'inscription jeudi 3 janvier 2008 Statut Membre Dernière intervention 10 février 2009 - 21 janv. 2009 à 22:23
cs_coltman Messages postés 97 Date d'inscription jeudi 3 janvier 2008 Statut Membre Dernière intervention 10 février 2009 - 22 janv. 2009 à 14:30
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

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());
            }
       
    }
}

1 réponse

cs_coltman Messages postés 97 Date d'inscription jeudi 3 janvier 2008 Statut Membre Dernière intervention 10 février 2009
22 janv. 2009 à 14:30
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 ...

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());
}

}
}
0
Rejoignez-nous