【发布时间】:2026-01-23 02:20:08
【问题描述】:
考虑以下使用 simple-xml 注释的枚举:
@Root(name="days")
public enum DaysOfWeek {
SUNDAY("blue", 30),
MONDAY("green", 60),
TUESDAY("yellow", 50),
WEDNESDAY("red", 45),
THURSDAY("black", 45),
FRIDAY("white", 65),
SATURDAY("brown", 40);
@Attribute(name="color")
private String color;
@Element(name="mins")
private int minutes;
DaysOfWeek(String color, int minutes){
this.color = color;
this.minutes = minutes;
}
DaysOfWeek(){
/*
* Default constructor
*/
}
public void setColor(String color){
this.color = color;
}
public void setMinutes(int minutes){
this.minutes = minutes;
}
public String getColor(){
return this.color;
}
public int getMinutes(){
return this.minutes;
}
}
并且,使用简单框架将其序列化为 XML 的代码:
StringWriter writer = new StringWriter();
try {
serializer.write(DaysOfWeek.TUESDAY, writer);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(writer.toString());
使用 simple-2.6,我看到了这个输出 - 这是我所期望的:
<days color="yellow">
<mins>50</mins>
</days>
但是,相同的代码,当使用 simple-2.6.7 序列化时,会给出:
<daysOfWeek>TUESDAY</daysOfWeek>
基本上,在 simple-2.6.7 中,枚举的各个成员(及其上的 simple-xml 注释)被忽略,并且始终使用枚举常量的名称进行序列化。
这是故意的吗?如何在考虑枚举的各个成员的同时获取最新版本的 simple-xml 来序列化枚举?
【问题讨论】:
-
我刚刚试用了最新版的 Simple XML (2.7.0)。问题似乎仍然存在。
-
问题仍然存在于 2.7.1 中。
标签: java enums xml-serialization simple-framework