The simple Java tool for validating XML against one or more XSDs has now been shown (complete code listing is at bottom of post). The sample application shown and explained here attempts to demonstrate that and is a useful tool for simple command line validation of XML documents against specified XSDs.
With it in place, we can run it against an example XML file and associated XSDs. One could easily port this to Groovy to be even more script-friendly.
It contains a reference to an XSD file, which you will also need to copy from below or download and enter into the webpage (in a second step, when you are prompted for it) to see the validation results.
Hi, I want to validate xml against a xsd in java 1.6.
In other words, XSDs with no "include" dependencies will generally be provided on the command line before those XSDs that include them.
The next code listing is for the complete package dustin.examples.xmlvalidation; import sax.
*/ private static Stream Source generate Stream Sources From Xsd Paths Jdk8( final String xsd Files Paths) that could be used instead of this method for code bases on a version of Java earlier than JDK 8.
This single-class Java application is most useful when it's executed from the command line.
*/ public static void main(final String arguments) function prints a usage statement if fewer than two command line arguments are passed to it because it expects at least the name/path of the XML file to be validated and the name/path of an XSD to validate the XML against. System.out; /** * Validate provided XML against the provided XSDs.
function takes the first command line argument and treats that as the XML file's path/name and then treats all remaining command lin arguments as the paths/names of one or more XSDs. */ public class Xml Validator Despite what the length of this post might initially suggest, using Java to validate XML against an XSD is fairly straightforward.
The problem is when i give the xsd path in the java code as below, Source schema File = new Stream Source(new File(".\config\abc.xsd")); it executes fine but when i give xsd in the xml file as below, java code is not able to find the xsd. SAXParse Exception: cvc-elt.1: Cannot find the declaration of element 'abc'. If i do the same thing in 1.5 there are no problems in both approaches of validation of xml.