Check xml created with jaxb for xsd file

I have an xml file created using jaxb. I need to check it based on xsd document. Is it possible to simply validate without disassembling. I need to then print the errors in the xml file.

+8
jaxb xml-validation jaxb2
source share
1 answer

Yes, you can use the validator found in java from 1.5. here is the doc link

In addition, you can use the dom-based or stream based APIs to validate your XML document in the xsd file. If you want to use the SAX API for your task, then hear an example:

try { String schemaLang = "http://www.w3.org/2001/XMLSchema"; SchemaFactory factory = SchemaFactory.newInstance(schemaLang); Schema schema = factory.newSchema(new StreamSource("sample.xsd")); Validator validator = schema.newValidator(); validator.validate(new StreamSource("test.xml")); } catch (SAXException e) { System.out.println(" sax exception :" + e.getMessage()); } catch (Exception ex) { System.out.println("excep :" + ex.getMessage()); } 

Otherwise, you can use the DOM, DOM4J, or XOM API. For further links you can see here .

There is also a related answer in stackoverflow.

+10
source share

All Articles