hi,
i m parsing xml file to java file....its is showing errors...unable to find the xml file...
xml file:
<?xml version = "1.0" ?>
<Employee-Detail>
<Employee>
<Emp_Id> E-001 </Emp_Id>
<Emp_Name> Vinod </Emp_Name>
<Emp_E-mail> Vinod1@yahoo.com </Emp_E-mail>
</Employee>
<Employee>
<Emp_Id> E-002 </Emp_Id>
<Emp_Name> Amit </Emp_Name>
<Emp_E-mail> Amit2@yahoo.com </Emp_E-mail>
</Employee>
<Employee>
<Emp_Id> E-003 </Emp_Id>
<Emp_Name> Deepak </Emp_Name>
<Emp_E-mail> Deepak3@yahoo.com </Emp_E-mail>
</Employee>
</Employee-Detail>
java file:
package testxml;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.io.*;
public class EmployeeDetails{
public static void main(String[] args) throws IOException{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter XML file name:");
String xmlFile = bf.readLine();
EmployeeDetails detail = new EmployeeDetails(xmlFile);
}
public EmployeeDetails(String str){
try{
File file = new File(str);
if (file.exists()){
SAXParserFactory parserFact = SAXParserFactory.newInstance();
SAXParser parser = parserFact.newSAXParser();
System.out.println("XML Data: ");
DefaultHandler dHandler = new DefaultHandler(){
boolean id;
boolean name;
boolean mail;
public void startElement(String uri, String localName,
String element_name, Attributes attributes)throws SAXException{
if (element_name.equals("Emp_Id")){
id = true;
}
if (element_name.equals("Emp_Name")){
name = true;
}
if (element_name.equals("Emp_E-mail")){
mail = true;
}
}
public void characters(char[] ch, int start, int len) throws SAXException{
String str = new String (ch, start, len);
if (id){
System.out.println("Emp_Id: "+str);
id = false;
}
if (name){
System.out.println("Name: "+str);
name = false;
}
if (mail){
System.out.println("E-mail: "+str);
mail = false;
}
}
};
parser.parse(str, dHandler);
}
else{
System.out.println("File not found!");
}
}
catch (Exception e){
System.out.println("XML File hasn't any elements");
e.printStackTrace();
}
}
}
the error is FILE NOT FOUND...
can anyone help me out....
thanx