Pesquisar neste blogue

sexta-feira, 4 de janeiro de 2019

Validating XML against XSD in Java

There are several ways to do this however to most simplest way i came across is using XmlUnit

From the manual

Validator v = Validator.forLanguage(Languages.W3C_XML_SCHEMA_NS_URI);
v.setSchemaSource(new StreamSource("Book.xsd"));
ValidationResult r = v.validateInstance(new StreamSource(new File("Book.xml")));
 
StreamSource accepts more constructors as shown here StreamSource Doc 

Here is the API of ValidationResult so you can transverse the problems encountered.

I usually work with Oracle Databases so i found this thread to create a XSD file from a table.

So if you need to validate data coming your table from a given file (csv,xlsx etc..) my suggestions are :

  • Look into SimpleFlatMapper to load the data.  
  • Look into Jackson to convert into XML
  • Consider XmlUnit to do the validation.
All the best.

Sem comentários:

Enviar um comentário