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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
| //Parse un document XML en Java avec l'API SAX
//on importe les APIs necessaires
//pour l'analyse du XML
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class AnalyseSAX extends DefaultHandler{
static JFrame fenetreTest = new JFrame("Test");
JPanel jpGroupe1;
JPanel jpGroupe2;
JPanel jpGroupe3;
JPanel tabPanel[] = new JPanel[3];
JComboBox cbx;
JTextField tf;
Vector vectComponent = new Vector();
Vector vectGroupe = new Vector();
int numero=1;
int num=0;
int id=1;
int comp=1;
JPanel groupe;
public static JFrame constructionFenetre(){
fenetreTest.setSize(800,600);
/*JPanel b = new JPanel();
b.setBackground(Color.red);
b.setPreferredSize(new Dimension(700,50));
JComboBox t1 = new JComboBox();
JTextField t2 = new JTextField();
JTextField t3 = new JTextField();
JButton b1 = new JButton();
JSpinner s1 = new JSpinner();
b1.setPreferredSize(new Dimension(20,22));
s1.setPreferredSize(new Dimension(40,22));
t1.setPreferredSize(new Dimension(80,22));
t2.setPreferredSize(new Dimension(340,22));
t3.setPreferredSize(new Dimension(51,22));
b.add(t1);
b.add(t2);
b.add(b1);
b.add(t3);
b.add(s1);*/
return fenetreTest;
}
// Les méthodes qui suivent sont appelées
// automatiquement par l'analyseur
// lorsqu'un événement est détecté
//dans le fichier XML.
public void startDocument() throws SAXException {
System.out.println( "debut du document");
}
public void endDocument() throws SAXException {
fenetreTest.add(groupe);
System.out.println( "fin du document");
}
/**@param String namespaceURI
* @param String sName simple name
* @param String qName qualified name
* @param Attributes attrs
*/
public void startElement(String namespaceURI, String sName, String qName, Attributes attrs) throws SAXException {
String eName = sName; // element name
if ("".equals(eName)) {
eName = qName; // not namespaceAware
}
num++;
System.out.println( "balise ouvrante: " + eName + num);
if ("object".equals(eName)){
if (attrs != null) {
// Listage des attributs
if (attrs.getLength() != 0) {
System.out.println( "listage des parametres pour la balise " + eName + ":");
}
for (int i = 0; i < attrs.getLength(); i++) {
String aName = attrs.getLocalName(i);
// Récupération du nom de l'attribut
if ("".equals(aName)) {
aName = attrs.getQName(i);
}
System.out.println( " " + aName + "=\"" + attrs.getValue(i) + "\"");
if("type".equals(aName)){
if("group".equals(attrs.getValue("type"))||"pagectrl".equals(attrs.getValue("type"))){
System.out.println("création du groupe numéro"+id);
groupe = new JPanel();
groupe.setBackground(Color.red);
}
if("cbx".equals(attrs.getValue("type"))){
cbx = new JComboBox();
groupe.add(cbx);
System.out.println("création composant"+comp);
comp++;
}
if("edit".equals(attrs.getValue("type"))||"date".equals(attrs.getValue("type"))){
tf = new JTextField();
groupe.add(tf);
System.out.println("création composant"+comp);
comp++;
}
if("button".equals(attrs.getValue("type"))){
JButton bu = new JButton();
groupe.add(bu);
System.out.println("création composant"+comp);
comp++;
}
}
}
}
}
}
public void endElement( String namespaceURI, String simpleName, String qualifiedName) throws SAXException {
String nomElement = simpleName;
if (nomElement.equals("")) {
nomElement = qualifiedName;
String b;
JPanel b1 = new JPanel();
}
if (num==6){
System.out.println("ajout composant au groupe"+id);
id++;
}
if ("object".equals(nomElement)){
System.out.println( "endElement : " + nomElement + num);
num--;
}
}
} // Fin de la classe
classe Test...
import java.io.File;
import java.io.IOException;
import javax.swing.JFrame;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.helpers.DefaultHandler;
public class Test {
// Méthode principale
public static void main(String args[]) throws IOException {
// Si l'utilisateur a oublié de passer
// le nom du fichier XML en paramètre => erreur
if (args.length != 1) {
System.err.println( "Erreur, il manque un argument");
System.exit(1);
}
// on lance l'analyseur avec le fichier XML en parametre
DefaultHandler handler = new AnalyseSAX();
SAXParserFactory factory;
factory = SAXParserFactory.newInstance();
try {
SAXParser saxParser = factory.newSAXParser();
saxParser.parse( new File(args[0]), handler);
} catch (Throwable t) {
// Si on a une erreur pendant l'analyse
t.printStackTrace();
System.exit(1);
}
JFrame fenetreTest = AnalyseSAX.constructionFenetre();
fenetreTest.show();
//System.exit(0);
} // fin du main
} |
Partager