0

I am doing some xml validation tool. So i need code how to get dtd and xml file from keyboard as argument. My code is below. Please advise me. I am newer in 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());
}
}
}
Rishi
  • 95
  • 2
  • 9

1 Answers1

0

For compiling use

javac ValidateXML.java

For executing the program

 java ValidateXML <fileName>

I have realized this from your program, you are accepting the arg(0) as String which is the location to your file.

Phani
  • 5,319
  • 6
  • 35
  • 43
  • Hi phani thanks for your reply. I need DTD and xml file from keyboard input like argument. – Rishi Apr 20 '12 at 09:39
  • If that's the case then use interactive as there is a limitation on the command line length to 32767 characters in Windows. Look at this example for interactive command line input: http://www.devdaily.com/java/edu/pj/pj010005 – Phani Apr 20 '12 at 09:55
  • Phani, i want to exact code, how can i use command line dtd file and xml file. (Ex: java ValidateXML dtdfile xmlfile) – Rishi Apr 20 '12 at 11:31
  • I think StackOverflow doesn't promote the complete code, you try finding it from internet, if you run in to any problems then please copy here. – Phani Apr 20 '12 at 12:57
  • phani above my code work is fine, but it works only input xmlfile in commandline. I want to both dtdfile and xmlfile passing input as commandline argument. Because I want to select which dtd will run at this xmlfile. – Rishi Apr 20 '12 at 14:10