| 12
 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
 
 | package saxpack;
 
 
 
 
//on importe les API necessaires
 
//pour l'analyse du XML
 
import java.io.File;
 
import java.io.IOException;
 
 
import javax.xml.parsers.SAXParser;
 
import javax.xml.parsers.SAXParserFactory;
 
 
import org.xml.sax.Attributes;
 
import org.xml.sax.SAXException;
 
import org.xml.sax.SAXParseException;
 
import org.xml.sax.helpers.DefaultHandler;
 
 
public class AnalyseSAX extends DefaultHandler {
 
//methode principale
 
public static void main (String args[]) throws IOException {
 
//Si l'utilisateur a oublié de passer
 
//le nom du fichier XML en parametre => erreur
 
if (args.length != 1) {
 
System.err.println("Usage: AnalyseSAX fichierxml");
 
System.exit (1);
 
}
 
//on lance l'analyseur avec le fichier XML en parametre
 
DefaultHandler handler = new AnalyseSAX();
 
SAXParserFactory factory = SAXParserFactory.newInstance(); 
 
try {
 
SAXParser saxParser = factory.newSAXParser();
 
File file = new File("adress.xml"); 
 
saxParser.parse( file, handler );
 
} catch (Throwable t) {
 
//Si on a une erreur pendant l'analyse
 
t.printStackTrace ();
 
System.exit (1);
 
}
 
System.exit (0);
 
}//fin du main
 
 
//Les methodes qui suivent sont appelées
 
//automatiquement par l'analyseur
 
//lorsqu'un événement est détecté
 
//dans le fichier XML.
 
public void error(SAXParseException e) throws SAXParseException{
 
throw e;
 
}
 
 
public void startDocument () throws SAXException {
 
System.out.println("début du document");
 
}
 
 
public void endDocument () throws SAXException {
 
System.out.println("fin du document");
 
}
 
 
public void startElement (String namespaceURI,String simpleName,String qualifiedName,Attributes attrs) throws SAXException {
 
String nomElement = simpleName;
 
if (nomElement.equals("")) nomElement = qualifiedName;
 
System.out.println("startElement : "+ nomElement);
 
}
 
 
public void endElement (String namespaceURI,String simpleName,String qualifiedName) throws SAXException {
 
String nomElement = simpleName;
 
if (nomElement.equals("")){
 
nomElement = qualifiedName;
 
}
 
System.out.println("endElement : "+ nomElement);
 
}
 
 
public void characters (char buf [], int offset, int len)
 
throws SAXException {
 
String s = new String(buf, offset,len);
 
System.out.println (s);
 
}
 
 
 
 
}//fin de la classe | 
Partager