2

I need to validate order of my output xml against a XSD using XMLUnit framework. I am new to XMLUnit and have no idea how to do it. I know that XMLUnit compares two XMLs but how to compare XML against a XSD?

Himanshu Yadav
  • 13,315
  • 46
  • 162
  • 291

1 Answers1

0

Instead of using XMLUnit for this aspect, you could use the javax.xml.validation APIs that are available in Java SE 5 and above:

SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new File("customer.xsd"));

Validator validator = schema.newValidator();
validator.setErrorHandler(new MyErrorHandler());
validator.validate(source);

For More Information

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • I want to use XMLUnit test cases as Unit test cases for my piece of code so that other developers can make sure they are developing XML in a proper way. – Himanshu Yadav Apr 09 '12 at 11:41
  • @HimanshuYadav - You can still use XMLUnit for all your other test cases. Then you can use the Java SE APIs to add an additional test for schema validation. – bdoughan Apr 09 '12 at 11:48
  • Dougham - Thanks for the quick reply. I will try it out with Java SE APIs. – Himanshu Yadav Apr 09 '12 at 11:53
  • Dougham I am getting org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'OLifE'but element is already present in the XSD as – Himanshu Yadav Apr 09 '12 at 13:25