package org.llgc; import java.io.File; import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; 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 XMLSAX extends DefaultHandler { private boolean close = false; private int deep = 0; private Deep0 pos0 = Deep0.UNKNOWN; private Deep1 pos1 = Deep1.UNKNOWN; private Deep2 pos2 = Deep2.UNKNOWN; public enum Deep0 { UNKNOWN, CONTACTS }; public enum Deep1 { UNKNOWN, CONTACT }; public enum Deep2 { UNKNOWN, NOM, PRENOM, IP }; @Override public void startDocument () throws SAXException { System.out.println ("Liste des documents"); close = false; deep = 0; } @Override public void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException { close = false; switch (deep) { case 0: { if (qName.equals ("contacts")) { pos0 = Deep0.CONTACTS; } else { fatalError (new SAXParseException ("Fichier invalide. Élément racine incorrect : " + qName + ".", null)); } break; } case 1: { if (qName.equals ("contact")) { pos1 = Deep1.CONTACT; } else { fatalError (new SAXParseException ("Fichier invalide. Élément niveau 1 incorrect : " + qName + ".", null)); } break; } case 2: { if (qName.equals ("nom")) { pos2 = Deep2.NOM; } else if (qName.equals ("prenom")) { pos2 = Deep2.PRENOM; } else if (qName.equals ("ip")) { pos2 = Deep2.IP; } else { fatalError (new SAXParseException ("Fichier invalide. Élément niveau 2 incorrect : " + qName + ".", null)); } break; } default: { fatalError (new SAXParseException ("Fichier invalide. Niveau d'arborescence (3) trop grand.", null)); break; } } if ( (pos0 == Deep0.CONTACTS) && (pos1 == Deep1.CONTACT) && (pos2 == Deep2.UNKNOWN)) { System.out.println (qName + " - " + attributes.getValue ("id") + " : "); } deep++; } @Override public void characters (char[] ch, int start, int length) throws SAXException { if (close) { return; } if ( (ch[start] != '\n') && (pos0 == Deep0.CONTACTS) && (pos1 == Deep1.CONTACT)) { switch (pos2) { case NOM: System.out.print ("Nom : "); break; case PRENOM: System.out.print ("Prénom : "); break; case IP: System.out.print ("IP : "); break; default: fatalError (new SAXParseException ("Fichier invalide. Type inconnu.", null)); // Erreur // impossible // normalement. break; } System.out.println (new String (ch, start, length)); } } @Override public void endElement (String uri, String localName, String qName) throws SAXException { deep--; switch (deep) { case 0: pos0 = Deep0.UNKNOWN; break; case 1: pos1 = Deep1.UNKNOWN; break; case 2: pos2 = Deep2.UNKNOWN; break; default: // Erreur impossible normalement. fatalError (new SAXParseException ("Fichier invalide. Type inconnu.", null)); break; } close = true; } @Override public void endDocument () throws SAXException { close = true; } @Override public void fatalError (SAXParseException e) throws SAXException { throw new SAXException (e); } public static void main (String[] args) { SAXParserFactory factory = SAXParserFactory.newInstance (); try { SAXParser parser = factory.newSAXParser (); parser.parse (new File ("contact.xml"), new XMLSAX ()); } catch (ParserConfigurationException | SAXException | IOException e) { e.printStackTrace (); return; } } }