jaxb unmarshalling creates duplicate list of objects : How to unmarshall objects in lists? -
jaxb unmarshalling creates duplicate list of objects : how unmarshall objects in lists?
xml file , code shown below while unmarshalling reading elements getting duplicate list using jaxb annotations , final out put duplicate list
<data_reading> <load_survey> <interval_settings value="30" xunit="mins" /> <measurement name="energy_real" xunit="kwh" /> <reading interval="00" value="000.010" /> <reading interval="01" value="000.000" /> <reading interval="02" value="000.050" /> <reading interval="03" value="000.080" /> <reading interval="04" value="000.010" /> </load_survey> </data_reading> these classes @xmlaccessortype(xmlaccesstype.field) public class loadsurvey { @xmlelement(name="interval_settings") private intervalsettings interval_settings; @xmlelement(name="measurement") private measurement measurement; @xmlelement(name="reading", type = reading.class) private list<reading> readings; //setter , getters } @xmlrootelement(name="data_reading") @xmlaccessortype(xmlaccesstype.field) public class datareading { @xmlelement(name="load_survey") private loadsurvey load_survey; } code here getting following output [data_reading [load_survey=loadsurvey [interval_settings=intervalsettings [value=30, xunit=mins], measurement=measurement [name=energy_real, xunit=kwh], readings=[reading [interval=00, value=0.23], reading [interval=01, value=0.23], reading [interval=02, value=0.22], reading [interval=03, value=0.21], reading [interval=04, value=0.23], reading [interval=05, value=0.24], reading [interval=00, value=0.23], reading [interval=01, value=0.23], reading [interval=02, value=0.22], reading [interval=03, value=0.21], reading [interval=04, value=0.23], reading [interval=05, value=0.24]]]] getting readings list duplicates jaxb please provide solution
the way getting duplicate items in list if have mapped both field (instance) variable , corresponding property (get/set method). since have specified xmlaccesstype.field
make sure have not annotated get
method list property.
for more information
- http://blog.bdoughan.com/2010/09/jaxb-collection-properties.html
- http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html
your example worked fine me. have included did below:
java model
below partial model focusing on part observed problem.
datareading
import javax.xml.bind.annotation.*; @xmlrootelement(name="data_reading") @xmlaccessortype(xmlaccesstype.field) public class datareading { @xmlelement(name="load_survey") private loadsurvey load_survey; }
loadsurvey
import java.util.list; import javax.xml.bind.annotation.*; @xmlaccessortype(xmlaccesstype.field) public class loadsurvey { @xmlelement(name="reading") private list<reading> readings; }
reading
import javax.xml.bind.annotation.*; @xmlaccessortype(xmlaccesstype.field) public class reading { @xmlattribute private string interval; @xmlattribute private string value; }
demo code
demo
import java.io.file; import javax.xml.bind.*; public class demo { public static void main(string[] args) throws exception { jaxbcontext jc = jaxbcontext.newinstance(datareading.class); unmarshaller unmarshaller = jc.createunmarshaller(); file xml = new file("src/forum15833602/input.xml"); datareading datareading = (datareading) unmarshaller.unmarshal(xml); marshaller marshaller = jc.createmarshaller(); marshaller.setproperty(marshaller.jaxb_formatted_output, true); marshaller.marshal(datareading, system.out); } }
input.xml/output
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <data_reading> <load_survey> <reading interval="00" value="000.010"/> <reading interval="01" value="000.000"/> <reading interval="02" value="000.050"/> <reading interval="03" value="000.080"/> <reading interval="04" value="000.010"/> </load_survey> </data_reading>
Comments
Post a Comment