0

我正在做一些 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());
}
}
}
4

1 回答 1

0

用于编译

javac ValidateXML.java

用于执行程序

 java ValidateXML <fileName>

我从您的程序中意识到了这一点,您接受 arg(0) 作为字符串,这是您文件的位置。

于 2012-04-20T09:35:34.427 回答