Jump to content

SAX parser, validate a xml file by a xsd


Kahor
 Share

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 ?

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

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...