@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>
Getting error like this
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)
Your class should be something like this. Give @xmlElement to fields and remove @xmlElement from setEmployeeDetails method. By default access type is for public member, so you can change access modifier of EmpId to public which will work without @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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.