XML file validation is somewhat painful job. If we have XML Schema(XSD) that can be done very easily using Apache XmlBean. Also traversing or parsing XML will be joyful with XmlBean.
First we have to generate Java class for XSD using XmlBean's scomp command, this can be done in command line. there are some options to generate source only or only Jar file.
After having Xmlbean objects for XSD, parsing and validating can be done like below:
First we have to generate Java class for XSD using XmlBean's scomp command, this can be done in command line. there are some options to generate source only or only Jar file.
After having Xmlbean objects for XSD, parsing and validating can be done like below:
void validate(String fileName) {XmlDocument xmlObject = XmlDocument.Factory.parse(new File(fileName)); //parse the XML file
ArrayListvalidationErrors = new ArrayList ();
XmlOptions validationOptions = new XmlOptions();
validationOptions.setErrorListener(validationErrors);
boolean isValid = xmlObject.validate(validationOptions); // to display error we should pass options.
if (!isValid) {for (XmlValidationError es : validationErrors) {System.err.println(es);}}}
Comments
Post a Comment