Créer une Jtable a partir d'un fichier XML
Bonjour,
J'essaie depuis un moment de construire un JTable avec un fichier XML... j'ai lu énormément de tutoriel et de façon de faire des table et lire des fichier XML et je retient que Jdom2 pour parser c'est une bonne option.
J'ai donc un fichier XML assez simple: nommé municipalite.xml
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
|
<?xml version="1.0" encoding="UTF-8"?>
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="municipalite.xsd" generated="2015-12-10T15:55:34">
<municipalite>
<Municipalite>Abénakis (Sainte-Claire)</Municipalite>
<Bureau>1232</Bureau>
<ZoneCP>3 Est</ZoneCP>
<Information_x0020_concernant_x0020_la_x0020_municipalité>N/A</Information_x0020_concernant_x0020_la_x0020_municipalité>
</municipalite>
<municipalite>
<Municipalite>Abercorn</Municipalite>
<Bureau>1605</Bureau>
<ZoneCP>5 Est / Ouest</ZoneCP>
<Information_x0020_concernant_x0020_la_x0020_municipalité>N/A</Information_x0020_concernant_x0020_la_x0020_municipalité>
</municipalite>
<municipalite>
<Municipalite>Acton Vale</Municipalite>
<Bureau>1605</Bureau>
<ZoneCP>6 Nord</ZoneCP>
<Information_x0020_concernant_x0020_la_x0020_municipalité>Une portion pourrait être couverte par le bureau 1602</Information_x0020_concernant_x0020_la_x0020_municipalité>
</municipalite>
<municipalite>
<Municipalite>Adamsville (Bromont)</Municipalite>
<Bureau>1605</Bureau>
<ZoneCP>5 Ouest</ZoneCP>
<Information_x0020_concernant_x0020_la_x0020_municipalité>N/A</Information_x0020_concernant_x0020_la_x0020_municipalité>
</municipalite>
</dataroot> |
Et j'ai mon Code... qui ouvre le fichier municipalité et qui se nomme StaticTable.java
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
|
package TesterConcepts;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import javax.swing.*;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
public class StaticTable extends JFrame {
// constructor that will display a JTable based on elements received as arguments
StaticTable(Object[][] obj, String[] header) {
super("Static JTable example");
// JPanel to horl the JTable
JPanel panel = new JPanel(new BorderLayout());
// constructor of JTable with a fix number of objects
JTable table = new JTable(obj, header);
panel.add(new JScrollPane(table));
add(panel); // adding panel to frame
// and display it
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
pack();
}
// to run the whole thing
public static void main(String[] args) throws JDOMException, IOException {
SAXBuilder builder = new SAXBuilder();
File xmlFile = new File("/Users/Dsptch/Desktop/DGPFxmltxt/municipalite.xml");
Document document = (Document) builder.build(xmlFile);
Element rootNode = document.getRootElement();
java.util.List list = rootNode.getChildren("municipalite");
for (int i = 0; i < list.size(); i++) {
Element node = (Element) list.get(i);
String[][] rowAndColumn = {
{(node.getChildText("Municipalite")), (node.getChildText("Bureau")),(node.getChildText("ZoneCP"))}
};
// defines the header
String[] header = {"Ville", "Bureau Local", "Zone CP"};
// build the GUI
StaticTable staticTable = new StaticTable(rowAndColumn, header);
}
}
} |
Le problème c'est que je ne sais plus comment disposer tout ça... Quand je vais l'exécution, il fait ce que je lui demande il prend chaque municipalités et les places au bon endroits ect... mais il fait une table pour chaque municipalité au lieux de faire une table avec toutes les municipalités... 8O de quoi faire virer fou car il y a a 2079 dans le xml original :roll:
Une idée ? de mon erreur? Je ne vois plus clair...
Merci de votre aide!