AnsweredAssumed Answered

Validate xml against xsd

Question asked by saket.saraf on Jun 2, 2013
Hi,

I am using below code to validate the xml against xsd. It works perfect however I want to pick my xsd schema file from alfresco repository. Currently I am injecting xsdPath in context file and xsd is placed in my code.

      InputStream inputStream = contentService.getReader(
               fileInfo.getNodeRef(), ContentModel.PROP_CONTENT)
               .getContentInputStream();

         SchemaFactory factory = SchemaFactory
               .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
         String filePath = new ClassPathResource(xsdPath).getFile()
               .getAbsolutePath();
         Schema schema = factory.newSchema(new StreamSource(new File(
               filePath)));
         Validator validator = schema.newValidator();
         validator.validate(new StreamSource(inputStream));
         return true;

Is there any way to read the xsd file from repository and pass that file to ClassPathResource? So my code will work with that modification. Or anyone have the code which will validate the xml from xsd which placed somewhere in repository.

Any quick solution or advise would be helpful.

Thanks.


Outcomes