user1768246
user1768246

Reputation: 39

jaxb unmarshalling creates duplicate list of objects : How to unmarshall objects in lists?

jaxb unmarshalling creates duplicate list of objects : How to unmarshall objects in lists?

xml file and code is shown below while unmarshalling reading elements getting duplicate list i am using jaxb annotations and my final out put is 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 are my 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 and getters
}


@XmlRootElement(name="data_reading")
@XmlAccessorType(XmlAccessType.FIELD)
public class DataReading {


    @XmlElement(name="load_survey")
    private LoadSurvey load_survey;

}

This is my code Here i am 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 with jaxb please provide any solution

Upvotes: 1

Views: 1984

Answers (1)

bdoughan
bdoughan

Reputation: 149017

The only way you could be getting duplicate items in the list is if you have mapped both the field (instance) variable and the corresponding property (get/set method). Since you have specified XmlAccessType.FIELD make sure you have not annotated the get method for the list property.

For More Information


Your example worked fine for me. I have included what I did below:

JAVA MODEL

Below is a partial model focusing on the part where you observed the 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>

Upvotes: 1

Related Questions