1

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

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
4

1 回答 1

1

在列表中获取重复项的唯一方法是,如果您已经映射了字段(实例)变量和相应的属性(get/set 方法)。由于您已指定XmlAccessType.FIELD,请确保您没有get为 list 属性注释方法。

了解更多信息


你的例子对我来说很好。我在下面包括了我所做的:

JAVA模型

下面是一个部分模型,重点关注您观察到问题的部分。

数据读取

import javax.xml.bind.annotation.*;

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

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

}

负载调查

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;

}

演示代码

演示

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);
    }

}

输入.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>
于 2013-04-05T11:02:55.913 回答