| 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
 
 | InputStream schema = this.getClass().getResourceAsStream("schema.xsd ou DTD");
 
public static void validerXML(String xml,InputStream xsd) throws SAXException, IOException, ParserConfigurationException, SAXParseException {
 
		logger.debug("Validation du PLI xml");
 
		String JAXP_SCHEMA_LANGUAGE =
			"http://java.sun.com/xml/jaxp/properties/schemaLanguage";
		String W3C_XML_SCHEMA =
			"http://www.w3.org/2001/XMLSchema";
		String JAXP_SCHEMA_SOURCE =
			"http://java.sun.com/xml/jaxp/properties/schemaSource";
 
//		 Suppression du BOM UTF8 s'il existe
 
		if ((xml.charAt(0) == 0x00EF) && (xml.charAt(1) == 0x00BB) && (xml.charAt(2) == 0x00BF) ) {
 
		xml = xml.substring(3, xml.length());
 
		}
 
//		Suppression du BOM UTF8 s'il existe
 
		if ((xml.charAt(0) == 0x00EF) && (xml.charAt(1) == 0x00BB) && (xml.charAt(2) == 0x00BF) ) {
 
			xml = xml.substring(3, xml.length());
 
		}
 
		InputSource inputSource = new InputSource();
 
		StringReader strread = new StringReader(xml);
 
		inputSource.setCharacterStream(strread); 
 
 
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 
		dbf.setIgnoringComments(true);
 
	//	dbf.setNamespaceAware(true);
 
		dbf.setValidating(true);
 
		dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
 
		dbf.setAttribute(JAXP_SCHEMA_SOURCE, xsd);
 
 
//		Parsage du fichier XML avec DOM
 
		DocumentBuilder db = dbf.newDocumentBuilder();
 
		db.setErrorHandler(new ErrorHandler() {
 
			public void fatalError(SAXParseException e) throws SAXParseException {
 
				logger.error(e);
 
				throw e;
 
			}
 
 
			public void error(SAXParseException e) throws SAXParseException {
 
				logger.error(e);
 
				throw e;
 
			}
 
 
			public void warning(SAXParseException e){
 
				logger.error(e);
			}
 
		});
 
		Document doc = db.parse(inputSource);
 
 
	} | 
Partager