[英]Cant read data from XML file in Java with JAVAXB
@XmlRootElement(name = "fields")
public class Employee {
String Empname;
String EmpID;
String salary;
// @XmlElement
Employee()
{}
Employee(String Empname, String EmpID,String salary)
{ super();
this.Empname=Empname;
this.EmpID=EmpID;
this.salary=salary;
}
@XmlElement
public String getID()
{
return EmpID;
}
@XmlElement
public void setEmployeeDetails(String Empname, String EmpID,String salary)
{
this.Empname=Empname;
this.EmpID=EmpID;
this.salary=salary;
}
}
public class EmployeeProcess {
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
File file = new File("/EmployeePosition/src/EmployeeDemo/EmployeeMainData.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Employee que= (Employee) jaxbUnmarshaller.unmarshal(file);
System.out.println(que.getID());
}
catch (JAXBException e) {
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<fields>
<EmpName>ABC</EmpName>
<EmpID>319501</EmpID>
<salary>15000</salary>
</fields>
出現這樣的錯誤
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
JAXB annotation is placed on a method that is not a JAXB property
this problem is related to the following location:
at @javax.xml.bind.annotation.XmlElement(namespace=##default, name=##default, type=class javax.xml.bind.annotation.XmlElement$DEFAULT, required=false, defaultValue=
at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(Unknown Source)
at com.sun.xml.internal.bind.v2.ContextFactory.createContext(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at javax.xml.bind.ContextFinder.newInstance(Unknown Source)
at javax.xml.bind.ContextFinder.newInstance(Unknown Source)
at javax.xml.bind.ContextFinder.find(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at EmployeeDemo.EmployeeProcess.main(EmployeeProcess.java:16)
您的課程應該是這樣的。 將@xmlElement賦予字段,並從setEmployeeDetails方法中刪除@xmlElement。 默認情況下,訪問類型適用於public成員,因此您可以將EmpId的access修飾符更改為public,而無需使用@XmlElement即可使用。
@XmlRootElement(name = "fields")
public class Employee {
String Empname;
@XmlElement
String EmpID;
String salary;
// @XmlElement
Employee()
{}
Employee(String Empname, String EmpID,String salary)
{ super();
this.Empname=Empname;
this.EmpID=EmpID;
this.salary=salary;
}
@XmlElement
public String getID()
{
return EmpID;
}
public void setEmployeeDetails(String Empname, String EmpID,String salary)
{
this.Empname=Empname;
this.EmpID=EmpID;
this.salary=salary;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.