我有以下带有测量部分的xml,我希望将其转换为java对象。但是我对本节的设计POJO的对象有疑问。你能帮我吗?感谢
<root_element>
<element>some_value</element>
<element>some_value</element> // SOME OTHERS ELEMENT'S
<element>some_value</element>
<measurement> // BEGINING OF THE SECTION WHERE I HAVE A PROBLEM
<component>SO2</component>
<averaged_time>
<averaged_hours>1</averaged_hours>
<value>9.054</value>
</averaged_time>
<component>NO2</component>
<averaged_time>
<averaged_hours>1</averaged_hours>
<value>14.155</value>
</averaged_time>
</measurement> // END OF THE SECTION WHERE I HAVE A PROBLEM
<element>some_value</element>
</root_element>
我有类似的东西(我没有在这里写其他参数,那些工作没有问题)。 JAXB将我的测量结果归还给最后一个&#34;组件&#34;和&#34; averaged_time&#34;元素。我知道ArrayList <Measurement>
是不好的做法。
@XmlRootElement
public class root_element {
private ArrayList<Measurement> measurements;
public ArrayList<Measurement> getMeasurements() {
return measurements;
}
@XmlElement (name = "measurement")
public void setMeasurement(ArrayList<Measurement> measurements) {
this.measurements = measurements;
}
}
测量对象
public class Measurement {
private String component;
private AveragedTime averagedTime;
public String getComponent() {
return component;
}
@XmlElement (name = "component")
public void setComponent(String component) {
this.component = component;
}
public AveragedTime getAveragedTime() {
return averagedTime;
}
@XmlElement (name = "averaged_time")
public void setAveragedTime(AveragedTime averagedTime) {
this.averagedTime = averagedTime;
}
}
AveragedTime对象
public class AveragedTime {
private int averaged_hours;
private double value;
public int getAveraged_hours() {
return averaged_hours;
}
@XmlElement (name = "averaged_hours")
public void setAveraged_hours(int averaged_hours) {
this.averaged_hours = averaged_hours;
}
public double getValue() {
return value;
}
@XmlElement (name = "value")
public void setValue(double value) {
this.value = value;
}
答案 0 :(得分:2)
我用XML看到的问题应该是
<root_element>
<element>some_value</element>
<element>some_value</element> // SOME OTHERS ELEMENT'S
<element>some_value</element>
<measurement> // BEGINING OF THE SECTION WHERE I HAVE A PROBLEM
<component>SO2</component>
<averaged_time>
<averaged_hours>1</averaged_hours>
<value>9.054</value>
</averaged_time>
</measurement>
<measurement>
<component>NO2</component>
<averaged_time>
<averaged_hours>1</averaged_hours>
<value>14.155</value>
</averaged_time>
</measurement> // END OF THE SECTION WHERE I HAVE A PROBLEM
<element>some_value</element>
</root_element>
休息所有POJO似乎没问题,即根据上面建议的xml同步。
否则,如果您需要继续使用现有XML,请按以下步骤更改类:
@XmlRootElement
public class root_element {
private Measurement measurements;
//getter-setters
}
测量类
public class Measurement {
private List<String> component;
private List<AveragedTime> averagedTime;
//getter-setters
}
休息似乎没问题。