Jump to content
Sign in to follow this  

SAX parser, validate a xml file by a xsd

Recommended Posts

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 ?

Share this post

Link to post
Share on other sites

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();		}	   }

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Create New...