3 import java.util.ResourceBundle;
4 import java.io.IOException;
5 import org.jdom.JDOMException;
6 import org.jdom.input.SAXBuilder;
9 * Class to check well-formedness and validity of XML documents.
11 public class XMLValidator {
14 * Check the well-formedness and validity of an XML document.
15 * The file is checked against the schema which is referenced from
16 * within the file. When the document is an XML schema, it is checked
17 * against <a href="http://www.w3.org/2001/XMLSchema.xsd"
18 * target="_blank">http://www.w3.org/2001/XMLSchema.xsd</a>.
20 * @param filename filename of the file to be checked
22 public static boolean isValid(String filename) {
23 ResourceBundle resourceBundle = ResourceBundle.getBundle("test");
25 SAXBuilder builder = new SAXBuilder(true);
26 builder.setFeature("http://xml.org/sax/features/validation",
28 builder.setFeature("http://apache.org/xml/features/validation/"
30 builder.setFeature("http://apache.org/xml/features/validation/"
31 + "schema-full-checking", true);
32 builder.setProperty("http://apache.org/xml/properties/schema/"
33 + "external-schemaLocation",
34 "http://www.w3.org/2001/XMLSchema "
35 + "http://www.w3.org/2001/XMLSchema.xsd "
36 + resourceBundle.getString("LOCAL_SCHEMAS"));
39 builder.build(filename);
41 catch (JDOMException e) {
42 System.out.println("Found an error in " + filename + ".");
43 System.out.println(e.getMessage());
44 System.out.println("");
47 catch (IOException e) {
48 System.out.println("Found an error in " + filename + ".");
49 System.out.println(e.getMessage());
50 System.out.println("");