I have the following XML file to unmarshall
<root>
<emp>Google</emp>
<emp>Yahoo</emp>
<xyz>random</xyz>
</root>
And i have used annotations in the following way,
@XmlRootElement(name = "root")
@XmlAccessorType(XmlAccessType.FIELD)
public class abc {
@XmlElement(name = "emp")
private String emp1;
@XmlElement(name = "emp")
private String emp2;
@XmlElement(name = "xyz")
private String xyz;
// added getters and setters for these fields
}
My problem is while i'm trying to get
obj.getEmp1(); // result is Yahoo instead of Google
obj.getEmp2(); // result is null.
Kindly clarify me, what am i doing wrong?
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
The standard JAXB (JSR-222) annotations do not support mapping 2 different properties to the same XML element.
You could use EclipseLink JAXB (MOXy)'s @XmlPath extension for this use case.
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlRootElement(name = "root")
@XmlAccessorType(XmlAccessType.FIELD)
public class abc {
@XmlPath("emp[1]/text()")
private String emp1;
@XmlPath("emp[2]/text()")
private String emp2;
@XmlElement(name = "xyz")
private String xyz;
// added getters and setters for these fields
}
For More Information
If for whatever reason you cannot use MOXy, another solution would be to map the emp element as a list
@XmlRootElement(name = "root")
@XmlAccessorType(XmlAccessType.FIELD)
public class abc {
@XmlElement(name = "emp")
private List<String> emp;
@XmlElement(name = "xyz")
private String xyz;
// added getters and setters for these fields
}
And then use the following code to get the values:
obj.getEmp().get(0);
obj.getEmp().get(1);
But Blaise's solution is more elegant
You could have a String[] field and have your current accessor methods access the String[].
import javax.xml.bind.annotation.*;
@XmlRootElement(name = "root")
@XmlAccessorType(XmlAccessType.FIELD)
public class abc {
private String[] emp = new String[2];
private String xyz;
public String getEmp1() {
return emp[0];
}
public void setEmp1(String emp1) {
this.emp[0] = emp1;
}
public String getEmp2() {
return emp[1];
}
public void setEmp2(String emp2) {
this.emp[1] = emp2;
}
public String getXyz() {
return xyz;
}
public void setXyz(String xyz) {
this.xyz = xyz;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With