Kahor Posted June 11, 2008 Share Posted June 11, 2008 I use this code to try and validate my xml file by an xml schema, the thing is, when the xml is not valid (ie opening a tag without closing it) I get errors, so i know the xml file is read, but if the xml file is "xml valid" but not valid according to the xml schema...no errors. public DelegateHandler() throws SAXException, ParserConfigurationException { SAXParserFactory factory = SAXParserFactory.newInstance();// // Use the validating parser factory.setNamespaceAware(true); factory.setValidating(true); saxParser = factory.newSAXParser();// try { saxParser.setProperty( "http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); } catch (SAXNotRecognizedException x) { // Happens if the parser does not support JAXP 1.2 x.printStackTrace(); } System.out.println("Test"); saxParser.setProperty( "http://java.sun.com/xml/jaxp/properties/schemaSource", new File("C:\\grammaire.xsd"));} my xsd file starts like that <?xml version="1.0" encoding="UTF-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.url.com/server" xmlns="http://www.url.com/server" xmlns:conf="http://www.url.com/server"elementFormDefault="qualified" attributeFormDefault="unqualified"> and my xml file : <?xml version="1.0" encoding="UTF-8"?><scroonxmlns="http://www.url.com/server"> Any idea ? Link to comment Share on other sites More sharing options...
Kahor Posted June 11, 2008 Author Share Posted June 11, 2008 I ended up using public static Schema compileSchema(String schema) throws SAXException { // Get the SchemaFactory instance which understands W3C XML Schema language SchemaFactory sf = SchemaFactory .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); return sf.newSchema(new File(schema)); }// compileSchema... public static void main... { try { Schema schema = compileSchema("file.xsd"); Validator validator = schema.newValidator(); validator.validate(new StreamSource(new File("file.xml"))); } catch (Exception ex) { ex.printStackTrace(); System.out.println("GET CAUSE:"); ex.getCause().fillInStackTrace(); } } Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.