| 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
 
 | try {
 
DocumentBuilder parser =               DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = parser.parse(new File("monfichier.xml"));
 
// on cree SchemaFactory capable d'interpertrer un XML schemas
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
 
// on recupère le fichier representant le schema xsd
Source schemaFile = new StreamSource(new File("monSchema.xsd"));
 
// on cree une instance du schema
Schema schema = factory.newSchema(schemaFile);	           	           	          	            // on cree un objet validateur pour valider une instance de notre document
 
Validator validator = schema.newValidator();	                  	           
ErrorHandlerImpl errorImpl = new ErrorHandlerImpl();
validator.setErrorHandler(errorImpl);	           
validator.validate(new DOMSource(document));
 
System.out.println("ok");	           
 
 
 
	       } catch (ParserConfigurationException e) {
	    	   // recupération de la syntaxe du fichier xml
	           System.err.println("ParserConfigurationException caught...");
	           //e.printStackTrace();
	       } catch (SAXException e) {
	    	   // récupération de l'erreur
	    	   String[] tabString = e.getMessage().split("'");
	    	   System.out.println("voici l'errueur SAXException ");
	    	   for (int i=0;i<tabString.length;i++){
	    		   System.out.println(tabString[i]);
	    	   }	    	       	   
	           System.err.println("SAXException caught...");
	           e.getException();
	       } catch (IOException e) {
	           System.err.println("IOException caught...");
	           //e.printStackTrace();
	       }
	}
 
private class ErrorHandlerImpl extends DefaultHandler {
 
	protected String message(SAXParseException e){
            String message = "Message : "+e.getMessage()+"\n";
            message += "Ligne "+e.getLineNumber()+", colonne "+e.getColumnNumber()+"\n";
            message += "Public id : "+e.getPublicId()+"\n";
            message += "System id : "+e.getSystemId();
            return message;
         }
         protected void printSAXException(SAXParseException e){
            System.out.println(message(e));
            if(e.getException() != null){
            e.getException().printStackTrace();
            }
         }
         public void warning(SAXParseException exception){
            System.out.println("*** Warning ***");
            printSAXException(exception);
         }
         public void error(SAXParseException exception){
            System.out.println("*** Erreur ***");
            printSAXException(exception);
         }
         public void fatalError(SAXParseException exception) throws SAXException{
            String message = "*** Erreur fatale ***\n";
            message += message(exception);
            SAXException se = new SAXException(message, exception);
            throw se;
 
         }
} | 
Partager