jaxb unmarshalling创建重复的对象列表:如何解组列表中的对象?

时间:2013-04-05 08:36:55

标签: jaxb

jaxb unmarshalling创建重复的对象列表:如何解组列表中的对象?

xml文件和代码如下所示,同时解组读取元素获取重复列表我正在使用jaxb注释,我的最终输出是重复列表

<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

1 个答案:

答案 0 :(得分:1)

如果您已经映射了field(instance)变量和相应的属性(get / set方法),那么在列表中获取重复项的唯一方法就是。由于您已指定XmlAccessType.FIELD,因此请确保您没有为list属性添加get方法。

了解更多信息


你的例子对我来说很好。我已经包括了我在下面做的事情:

JAVA模型

下面是一个部分模型,侧重于您观察问题的部分。

<强> 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;

}

<强>读

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Reading {

    @XmlAttribute
    private String interval;

    @XmlAttribute
    private String value;

}

DEMO CODE

<强>演示

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中/输出

<?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>