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
| import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
public class Sax_Parser extends DefaultHandler{
protected String nomFichierXML;
int cptTag = 0;
/** Cree une nouvelle instance de Sax_Parser */
public Sax_Parser() {
setNomFichierXML(null);
}
public Sax_Parser(String nfx) {
setNomFichierXML(nfx);
}
/** Methodes get et set */
public String getNomFichierXML() { return nomFichierXML; }
public void setNomFichierXML(String nfx) { nomFichierXML = nfx; }
/** Methodes d'affichage */
static protected void trace (String s) {
System.out.println(s);
}
static protected void trace (String Comment, String s) {
System.out.println(Comment + " : " + s);
}
static protected void trace (String s, int i) {
System.out.println(s + " : " + i);
}
/** Methodes du ContentHandler */
public void characters(char[] ch, int start, int length) throws SAXException
{
String chaine = new String(ch,start,length).trim();
if(chaine.length()>0) trace("@ Carateres",chaine);
}
public void startDocument() throws SAXException
{
trace("** Debut du document **");
}
public void endDocument() throws SAXException
{
trace("** Fin du document **");
}
public void startElement(java.lang.String uri, java.lang.String localName,java.lang.String qName,Attributes attr) throws SAXException
{
trace(" * Debut d'un element ");
cptTag++;
trace("++ compteur de tag :",cptTag);
if(uri!=null && uri.length()>0) trace(" uri", uri);
trace(" NomLocal", localName);
if(uri!=null && uri.length()>0) trace(" NomComplet", qName);
int nAttr = attr.getLength();
trace(" Nombre d'attribut",nAttr);
if(nAttr == 0) return;
for(int i=0;i<nAttr;i++)
trace(" attribut n° "+i+ "=" + attr.getLocalName(i) + "avec valeur : "+ attr.getValue(i));
}
public void endElement(java.lang.String uri, java.lang.String localName,java.lang.String qName) throws SAXException
{
trace(" * Fin de l'element "+localName);
cptTag++;
trace("++ compteur de tag :",cptTag);
}
} |