Validating xml java parser schema

I have found it useful to have my own easy-to-use XML validation tool because of limitations or issues of the previously mentioned approaches.

Java makes it easy to write such a tool and this post demonstrates how easy it is to develop a simple XML validation tool in Java.

As the comment in the code states, the JDK 7 language change supporting catching of multiple exceptions in a single /** * Generates array of Stream Source instances representing XSDs * associated with the file paths/names provided and use JDK 8 * Stream API.

* * This method can be commented out if using a version of * Java prior to JDK 8.

*/ 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.

In most cases, I have tried to comment the code that requires JDK 7 or JDK 8 to identify these dependencies and provide alternative approaches in earlier versions of Java.

The most significant lines of code from that application when discussing validation of XML against one or more XSDs is shown next. Schema Schema(Source[]) (where the array of transform. * * @param xml File Path And Name Path/name of XML file to be validated; * should not be null or empty.

For this demonstration, I’m using a very simple manifestation of a Servlet 2.5 file just shown is valid per the Servlet 2.5 XSDs and the output of running this simple Java-based XSD validation tool prove that by not reporting any validation errors. As mentioned earlier, this simple tool requires JDK 8 as currently written but could be easily adapted to work on JDK 5, JDK 6, or JDK 7.

An XSD-valid XML file does not lead to very interesting results with this tool.

The next code listing shows an intentionally invalid As the last output shows, things are more interesting in terms of output when the provided XML is not XSD valid.

There is one important caveat I wish to emphasize here.

275

Leave a Reply