📄 validator.java
字号:
// Validator.java// Validate an XML document against its DTD.package com.deitel.jws1.xml;// Java core packagesimport java.io.*;// Java extension packagesimport org.w3c.dom.*;import org.xml.sax.*;import javax.xml.parsers.*;public class Validator { public static void main( String args[] ) { // check command-line arguments for file name if ( args.length != 1 ) { // print usage information System.err.println( "Usage: java Validator filename" ); System.exit( 1 ); } // create parser and parse given Document try { // get a DocumentBuilderFactory for creating // a DocumentBuilder (i.e., an XML parser) DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // enable document validation factory.setValidating( true ); // get a DocumentBuilder from the DocumentBuilderFactory DocumentBuilder builder = factory.newDocumentBuilder(); // set custom error handler builder.setErrorHandler( new MyErrorHandler() ); // parse an XML document and create a DOM tree in // memory Document messageDocument = builder.parse( new File( args[ 0 ] ) ); System.out.println( "Document is valid." ); } // end try // handle exception creating parser catch ( ParserConfigurationException parserException ) { System.err.println( "Error creating parser: " ); parserException.printStackTrace(); } // handle exception parsing Document catch ( SAXException saxException ) { System.err.println( "Error parsing Document: " ); saxException.printStackTrace(); } // handle exception reading data from Document catch ( IOException ioException ) { System.err.println( "Error reading XML Document: " ); ioException.printStackTrace(); } }}class MyErrorHandler implements ErrorHandler{ // throw SAXException for fatal errors public void fatalError( SAXParseException fatalException ) throws SAXException { throw fatalException; } // throw SAXParseException for fatal errors public void error( SAXParseException errorException ) throws SAXParseException { throw errorException; } // print warnings to System.err public void warning( SAXParseException warn ) throws SAXParseException { System.err.println( warn.getMessage() ); }}/* ************************************************************************** * (C) Copyright 2003 by Deitel & Associates, Inc. and Prentice Hall. * * All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * ***************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -