FileDocCategorySizeDatePackage
XMLValidator.javaAPI DocExample1972Sun Jul 07 09:39:26 BST 2002javajaxb.util

XMLValidator

public class XMLValidator extends Object

Fields Summary
Constructors Summary
public XMLValidator()

        // Currently, does nothing
    
Methods Summary
public static voidmain(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 voidvalidate(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);
        }