XMLValidatorpublic class XMLValidator extends Object
Constructors Summary |
---|
public XMLValidator()
// Currently, does nothing
|
Methods Summary |
---|
public static void | main(java.lang.String[] args)
if (args.length != 1) {
System.out.println("Usage: java javajaxb.util.XMLValidator " +
"[XML filename]");
return;
}
try {
File xmlFile = new File(args[0]);
FileReader reader = new FileReader(xmlFile);
XMLValidator validator = new XMLValidator();
// Validate, and write errors to system output stream
validator.validate(reader, System.out);
} catch (FileNotFoundException e) {
System.out.println("Could not locate XML document '" +
args[0] + "'");
} catch (IOException e) {
System.out.println("Error processing XML: " + e.getMessage());
e.printStackTrace();
}
| public void | validate(java.io.Reader reader, java.io.OutputStream errorStream)
PrintStream printStream = new PrintStream(errorStream);
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
SAXParser parser = factory.newSAXParser();
parser.parse(new InputSource(reader), new DefaultHandler());
// If we got here, no errors occurred
printStream.print("XML document is valid.\n");
} catch (Exception e) {
e.printStackTrace(printStream);
}
|
|