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
|
public class parseurSAX extends DefaultHandler {
// attribut va nous permettre de savoir si nous visitons une balise
public boolean isAtt = false;
public boolean isFP = false;
public boolean isOT = false;
public static String titreFeuille;
public parseurSAX(){
super();
}
/*
* Ouverture d'une nouvelle balise.
*/
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException{
if (qName.equals("att")){
isAtt = true;
System.out.println(attributes.getValue("n"));
}
if (qName.equals("fp")){
isFP = true;
}
}
/*
* methode qui correspond à la fermeture d'une balise.
*/
public void endElement(String uri, String localName, String qName) throws SAXException{
if (qName.equals("att"))
isAtt = false;
}
/*
* methode qui correspond à ce que l'on peut trouver entre deux balises
*/
public void characters(char[] ch, int start, int length) throws SAXException{
if (isAtt){
// On récupère la bonne partie de la string
String content = new String(ch, start, length);
if (!content.isEmpty())
System.out.println(content);
}
if (isFP){
// On récupère la bonne partie de la string
String content = new String(ch, start, length);
if (!content.isEmpty())
System.out.println(content);
}
if (isOT){
String titreFeuille = new String(ch, start, length);
System.out.println("Test Titre " + titreFeuille);
}
}
public static void main(String[] args){{
try {
creerFeuilleExcel();
// On instancie le parseur SAX Java
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parseur = factory.newSAXParser();
// Le fichier XML à parser
File file = new File("./XML/1.xml");
// Le Handler que nous avons précédemment coder
DefaultHandler handler = new parseurSAX();
// On lance le parsing du fichier
parseur.parse(file, handler);
} catch (Exception ex) {
Logger.getLogger(parseurSAX.class.getName()).log(Level.SEVERE, null, ex);
}
}
} |
Partager