Validating xml w.r.t XSD in java

scrool

New Member
I am trying to validate an xml file which is generated w.r.t a schema in xsd format.The doubt is if I need to pass the url of the schema or the location of schema on my system ?Similarly, do we need to pass the content of xml file or its location ?Below is the code snippet I am using - \[code\]public void validateDTFAgainstXSD(){ String inputxml = "C:/Users/file.xml"; String schemaLocation = "https://github.abcd/schema.xsd"; try { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); File schemaFile = new File(schemaLocation); Schema schema = factory.newSchema(schemaFile); javax.xml.validation.Validator validator = schema.newValidator(); Source source = new StreamSource(new StringReader(inputxml)); validator.validate(source); System.out.println("File validated"); } catch(Exception ex) { System.out.println("File not validated"); } }\[/code\]
 
Back
Top