我正在做一些 xml 验证工具。所以我需要代码如何从键盘获取 dtd 和 xml 文件作为参数。我的代码如下。请给我提意见。我是java新手。
public class ValidateXML
{
public static void main(String args[]) {
try{
File x = new File(args[0]);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setErrorHandler(new org.xml.sax.ErrorHandler() {
//To handle Fatal Errors
public void fatalError(SAXParseException exception)throws SAXException {
System.out.println("Line: " +exception.getLineNumber() + "\tColumn: "+e.getColumnNumber() + "\nFatal Error: "+exception.getMessage());
}
//To handle Errors
public void error(SAXParseException e)throws SAXParseException {
System.out.println("Line: " +e.getLineNumber() + "\tColumn: "+e.getColumnNumber() + "\nError: "+e.getMessage());
}
//To Handle warnings
public void warning(SAXParseException err)throws SAXParseException{
System.out.println("Line: " +err.getLineNumber() + "\tColumn: "+e.getColumnNumber() + "\nWarning: "+err.getMessage());
}
});
Document xmlDocument = builder.parse(new FileInputStream(x));
System.out.println("execute!!");
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}