i have following xml file unmarshall
<root> <emp>google</emp> <emp>yahoo</emp> <xyz>random</xyz> </root>
and have used annotations in 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 , setters these fields }
my problem while i'm trying get
obj.getemp1(); // result yahoo instead of google obj.getemp2(); // result null.
kindly clarify me, doing wrong?
if whatever reason cannot use moxy, solution map emp
element 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 , setters these fields }
and use following code values:
obj.getemp().get(0); obj.getemp().get(1);
but blaise's solution more elegant
you have string[]
field , have current accessor methods access 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; } }
Comments
Post a Comment