import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParser; import org.xml.sax.XMLReader; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.ErrorHandler; import org.xml.sax.Attributes; import org.xml.sax.helpers.DefaultHandler; import java.util.*; import java.io.*; public class SimpleSaxParserIndent extends DefaultHandler { private String currentElement = new String(); private static String urlToParse; private int indent; private boolean inline; // Constructor public SimpleSaxParserIndent () { // Invokes constructor of the superclass super(); this.indent=0; this.inline=false; } static public void main(String[] args) { try{ // // 1. Creation d'un JAXP SAXParserFactory et configuration // SAXParserFactory factory = SAXParserFactory.newInstance(); // // si on veut valider le document par rapport a // sa declaration de type // //factory.setValidating(true); // // 2. Creation d'un JAXP SAXParser // SAXParser saxParser = factory.newSAXParser(); // // 3. Obtention du XMLReader // XMLReader xmlReader = saxParser.getXMLReader(); // // 4. Affectation du ContentHandler pour le // XMLReader. Remarque: on utilise la classe courante // xmlReader.setContentHandler(new SimpleSaxParserIndent()); // // 5. Affectation du ErrorHandler avant parsing // xmlReader.setErrorHandler(new MyErrorHandler(System.err)); // // 6. On parse le document (en donnant son URL) en utilisant // le XMLReader // urlToParse = convertToFileURL(args[0]); xmlReader.parse(urlToParse); } catch (Throwable t) { t.printStackTrace(); } System.exit(0); } // debut du document public void startDocument() throws SAXException { System.out.println("Start Document: "+urlToParse); } // debut de l'element public void startElement(String namespaceURI, String localName, // local name String rawName, // qualified name Attributes atts) throws SAXException { //Retour a la ligne System.out.print("\n"); //Creation du decalage for(int i =0; i"); } // Pour les noeuds textes public void characters (char[] ch, int start, int length) { String text; for(int i = start; i<=start + length-1; i++){ if(ch[i]=='\n') ch[i]=' '; } text = new String (ch, start, length); System.out.print(text); } // d'apres vous public void endElement(java.lang.String uri, java.lang.String localName, java.lang.String rawName) throws SAXException { String eltName = localName; //Creation du decalage indent--; System.out.print("\n"); for(int i =0; i"); } // fin du document public void endDocument() throws SAXException { System.out.print("\n"); System.out.print("End Document\n"); } ////////////////////////////////////////////////// /* Convert from a filename to a file URL. */ ////////////////////////////////////////////////// private static String convertToFileURL(String filename) { String path = new File(filename).getAbsolutePath(); if (File.separatorChar != '/') { path = path.replace(File.separatorChar, '/'); } if (!path.startsWith("/")) { path = "/" + path; } return "file:" + path; } ////////////////////////////////////////////////// /* classe interne pour reporter les erreurs */ ////////////////////////////////////////////////// private static class MyErrorHandler implements ErrorHandler { /** Error handler output goes here */ private PrintStream out; MyErrorHandler(PrintStream out) { this.out = out; } private String getParseExceptionInfo(SAXParseException spe) { String systemId = spe.getSystemId(); if (systemId == null) { systemId = "null"; } String info = "URI=" + systemId + " Line=" + spe.getLineNumber() + ": " + spe.getMessage(); return info; } public void warning(SAXParseException spe) throws SAXException { out.println("Warning: " + getParseExceptionInfo(spe)); } public void error(SAXParseException spe) throws SAXException { String message = "Error: " + getParseExceptionInfo(spe); throw new SAXException(message); } public void fatalError(SAXParseException spe) throws SAXException { String message = "Fatal Error: " + getParseExceptionInfo(spe); throw new SAXException(message); } } }