我正在尝试使用digester解析xml。
我的XML
<root>
<Employee>
<Id>1</Id>
<FirstName>Charles</FirstName>
<LastName>Madigen</LastName>
<Location>Louisiana</Location>
<Skill>Accountant</Skill>
</Employee>
</root>
我的员工类
public class Employee {
private int empId;
private String fName;
private String lName;
private String location;
private String skill;
public Employee(int empId, String fName, String lName, String location,
String skill) {
this.empId = empId;
this.fName = fName;
this.lName = lName;
this.location = location;
this.skill = skill;
}
@Override
public String toString() {
return "Employee [empId=" + empId + ", fName=" + fName + ", lName="
+ lName + ", location=" + location + ", skill=" + skill + "]";
}
public void setEmpId(int empId) {
this.empId = empId;
}
public void setfName(String fName) {
this.fName = fName;
}
public void setlName(String lName) {
this.lName = lName;
}
public void setLocation(String location) {
this.location = location;
}
public void setSkill(String skill) {
this.skill = skill;
}
}
和我的读者课
public class CSVtoXMLTransformer {
public static void main(String[] args) throws IOException {
CSVtoXMLTransformer cx=new CSVtoXMLTransformer();
//cx.transfromer();
cx.validator();
}
void validator() throws IOException{
String itemTag = "root/Employee";
Digester digester = new Digester();
digester.setValidating(false);
digester.addObjectCreate(itemTag, "assignment3.Employee");
digester.addCallMethod(itemTag + "/Id", "setEmpId", 0);
digester.addCallMethod(itemTag + "/FirstName", "setfName", 0);
digester.addCallMethod(itemTag + "/LastName", "setlName", 0);
digester.addCallMethod(itemTag + "/Location", "setLocation", 0);
digester.addCallMethod(itemTag + "/Skill", "setSkill", 0);
File inputFile = new File( "generatedEmployee.xml" );
Employee emp;
try {
emp = (Employee)digester.parse( inputFile );
System.out.println(emp);
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
但是在跑步的过程中我遇到了这个错误,任何人都可以帮我解决这个问题
org.xml.sax.SAXParseException; systemId: file:/D:/workspace/poc2/generatedEmployee.xml; lineNumber: 2; columnNumber: 11; Error at line 2 char 11: assignment3.Employee
at org.apache.commons.digester.Digester.createSAXException(Digester.java:3363).......
Caused by: java.lang.NoSuchMethodException: assignment3.Employee.<init>()
at java.lang.Class.getConstructor0(Unknown Source)
... 17 more.....
答案 0 :(得分:2)
Caused by: java.lang.NoSuchMethodException: assignment3.Employee.<init>()
at java.lang.Class.getConstructor0(Unknown Source)
在编译的代码中找不到带有签名assignment3.Employee.<init>()
的方法,这就是JVM引发java.lang.NoSuchMethodException
例外的原因。
在您的类中,您创建了参数化构造函数,当您创建参数化构造函数时,编译器将不会创建默认构造函数,因此您还必须实现默认构造函数。
答案 1 :(得分:1)
您的员工类需要一个默认构造函数:
public class Employee {
private int empId;
private String fName;
private String lName;
private String location;
private String skill;
public Employee(){}; //Default constructor
public Employee(int empId, String fName, String lName, String location,
String skill) {
this.empId = empId;
this.fName = fName;
this.lName = lName;
this.location = location;
this.skill = skill;
}
@Override
public String toString() {
return "Employee [empId=" + empId + ", fName=" + fName + ", lName="
+ lName + ", location=" + location + ", skill=" + skill + "]";
}
...
}