我正在做一些XML验证工具。所以我需要代码如何从键盘获取dtd和xml文件作为参数。我的代码如下。请建议我。我在java中更新。如何从键盘获取dtd和xml文件?
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());
}
}
}
你是什么意思“从键盘”?你可以用你的键盘输入xml/dtd,但我不认为这是你的问题... – cello 2012-04-20 09:26:42
可能是指命令行...'new File(args [0])' – radimpe 2012-04-20 09:34:11
感谢您的回复。我需要两个命令行输入作为DTD和XML。 – Rishi 2012-04-20 09:41:43