Salut,

Je suis étudiant en deuxième année info et j'ai un petit souci au niveau d'affichage de donnée à partir d'un fichier xml dans un JTable. Pour être plus précis, mon JTable affiche une table pour chaque élément demandé au lieu de les lister.

voici mon fichier xml:
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
<?xml version="1.0" encoding="UTF-8"?>
<container>
  <job num="12345656">
    <customer>
      <name>John Smith</name>
      <shopName>Omega</shopName>
      <contactNumber>006139876566</contactNumber>
    </customer>
    <watch mark="Rolex">
      <workRequiries>Battery</workRequiries>
      <dateRecieve>2009-10-29</dateRecieve>
      <dateSend>2009-10-30</dateSend>
      <cost>2000</cost>
    </watch>
    <commentary>Battery HS</commentary>
  </job>
  <job num="1234567676">
    <customer>
      <name>John Connor</name>
      <shopName>Swatch Group</shopName>
      <contactNumber>006139787887</contactNumber>
    </customer>
    <watch mark="Swatch">
      <workRequiries>Cells</workRequiries>
      <dateRecieve>2009-09-12</dateRecieve>
      <dateSend>2009-10-31</dateSend>
      <cost>200</cost>
    </watch>
    <commentary>Buy complementary...</commentary>
  </job>
</container>

et 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
126
127
128
129
130
131
132
133
134
135
136
 
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
 
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
 
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.Iterator;
import java.util.List;
 
public class XmlJTable extends JPanel {
 
private boolean DEBUG = false;
 
	static org.jdom.Document document;
	static Element racine;
 
	public SimpleTableDemo() {
		super(new GridLayout(1,0));
 
 
		SAXBuilder sxb = new SAXBuilder();
		try {
 
			document = sxb.build(new File("test.xml"));
		} catch (Exception e5) {
		}
 
 
 
		racine = document.getRootElement();
		System.out.println("racine "+racine);
		List listJob = racine.getChildren("job");
		System.out.println(listJob.size());
 
		Iterator i = listJob.iterator();
		while (i.hasNext()) {
 
			Element courant = (Element) i.next();
 
			String[] columnNames = {"Number",
					"Costumer name",
					"Shop name",
					"Costomer contact",
					"Watch mark",
					"Work require",
					"Recieve date",
					"Send date",
					"Cost",
			"Commentary"};
			String[][] data = {
					{
						courant.getAttributeValue("num"), courant.getChild("customer").getChild("name").getText(),
						courant.getChild("customer").getChild("shopName").getText()
						, courant.getChild("customer").getChild("contactNumber").getText()
						, courant.getChild("watch").getAttributeValue("mark"),courant.getChild("watch").getChild("workRequiries").getText()
						,courant.getChild("watch").getChild("dateSend").getText(),courant.getChild("watch").getChild("dateSend").getText()
						,courant.getChild("watch").getChild("cost").getText(),courant.getChild("commentary").getText()}
 
 
 
			};
 
			final JTable table = new JTable(data, columnNames);
			table.setPreferredScrollableViewportSize(new Dimension(500, 70));
			table.setFillsViewportHeight(true);
 
			if (DEBUG) {
				table.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent e) {
						printDebugData(table);
					}
				});
			}
 
		   scrollPane = new JScrollPane(table);
 
 
			add(scrollPane);
		}
 
	}
 
	private void printDebugData(JTable table) {
		int numRows = table.getRowCount();
		int numCols = table.getColumnCount();
		javax.swing.table.TableModel model = table.getModel();
 
		System.out.println("Value of data: ");
		for (int i=0; i < numRows; i++) {
			System.out.print("    row " + i + ":");
			for (int j=0; j < numCols; j++) {
				System.out.print("  " + model.getValueAt(i, j));
			}
			System.out.println();
		}
		System.out.println("--------------------------");
	}
 
	/**
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event-dispatching thread.
         */
	private static void createAndShowGUI() {
		//Create and set up the window.
		JFrame frame = new JFrame("XmlJTable ");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
		//Create and set up the content pane.
		XmlJTable  newContentPane = new XmlJTable ();
		newContentPane.setOpaque(true); //content panes must be opaque
		frame.setContentPane(newContentPane);
 
		//Display the window.
		frame.pack();
		frame.setVisible(true);
	}
 
	public static void main(String[] args) {
		//Schedule a job for the event-dispatching thread:
		//creating and showing this application's GUI.
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				createAndShowGUI();
			}
		});
	}
}
et voici le résultat:

Je vous remerci en avance!