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