如何在 java SimpleXML 中反序列化嵌套对象
How to deserialize nested object in java SimpleXML
我有以下 xml,我想用 simpleXML 解析它。
<programme start="20161107190000 GMT+04:00" stop="20161107200000 GMT+04:00" channel="001">
<title lang="en_GB">Foo</title>
<desc lang="en_GB">Foobarbaz</desc>
</programme>
<programme start="20161107200000 GMT+04:00" stop="20161107203000 GMT+04:00" channel="001">
<title lang="en_GB">JLS</title>
<desc lang="en_GB">The Java Language Specification</desc>
</programme>
具有以下
@Root
public class Programme {
@Attribute(name = "channel", required = false)
private String channel;
@Attribute(name = "start", required = false)
private String start;
@Attribute(name = "stop", required = false)
private String stop;
@Element(name = "title", required = false)
private Title title;
@Element(name = "desc", required = false)
private String desc;
public Programme(String channel, String start, String stop, String title, String desc) {
this.channel = channel;
this.start = start;
this.stop = stop;
this.title = title;
this.desc = desc;
}
public String getChannel() {
return channel;
}
public void setChannel(String channel) {
this.channel = channel;
}
public String getStart() {
return start;
}
public void setStart(String start) {
this.start = start;
}
public String getStop() {
return stop;
}
public void setStop(String stop) {
this.stop = stop;
}
public Title getTitle() {
return title;
}
public void setTitle(Title title) {
this.title = title;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
@Root(name = "title")
public class Title {
@Attribute
private String lang;
private String text;
public Title(String lang, String text) {
this.lang = lang;
this.text = text;
}
public String getLang() {
return lang;
}
public void setLang(String lang) {
this.lang = lang;
}
@Text
public String getText() {
return text;
}
@Text
public void setText(String text) {
this.text = text;
}
}
}
给我错误 org.simpleframework.xml.core.ConstructorException: Can not construct inner class
。
有两点需要注意。
1) 如果您在内部元素中为属性嵌套了 class,嵌套的 class 必须是 static
.
2) 定义内部变量class必须注释如下
@Element(name = "title", type = Title.class, required = false)
private Title title;
3) 在构造函数内部,他们应该实例化一个空的 class
this.title = new Title();
这是一个完整的工作版本:
@Root
public class Programme {
@Attribute(name = "channel", required = false)
private final String channel;
@Attribute(name = "start", required = false)
private final String start;
@Attribute(name = "stop", required = false)
private final String stop;
@Element(name = "title", type = Title.class, required = false)
private Title title;
@Element(name = "desc", type = Desc.class, required = false)
private Desc desc;
public Programme(
@Attribute(name = "channel") String channel,
@Attribute(name = "start") String start,
@Attribute(name = "stop") String stop
) {
this.channel = channel;
this.start = start;
this.stop = stop;
this.title = new Title();
this.desc = new Desc();
}
public Title getTitle() {
return title;
}
public void setTitle(Title title) {
this.title = title;
}
public Desc getDesc() {
return desc;
}
public void setDesc(Desc desc) {
this.desc = desc;
}
@Root(name = "title")
public static class Title {
public String text;
@Attribute(name = "lang")
private String lang;
@Text
public String getText() {
return text;
}
@Text
public void setText(String text) {
this.text = text;
}
public String getLang() {
return lang;
}
}
@Root(name = "desc")
public static class Desc {
public String text;
@Attribute(name = "lang")
private String lang;
@Text
public String getText() {
return text;
}
@Text
public void setText(String text) {
this.text = text;
}
public String getLang() {
return lang;
}
}
}
我有以下 xml,我想用 simpleXML 解析它。
<programme start="20161107190000 GMT+04:00" stop="20161107200000 GMT+04:00" channel="001">
<title lang="en_GB">Foo</title>
<desc lang="en_GB">Foobarbaz</desc>
</programme>
<programme start="20161107200000 GMT+04:00" stop="20161107203000 GMT+04:00" channel="001">
<title lang="en_GB">JLS</title>
<desc lang="en_GB">The Java Language Specification</desc>
</programme>
具有以下
@Root
public class Programme {
@Attribute(name = "channel", required = false)
private String channel;
@Attribute(name = "start", required = false)
private String start;
@Attribute(name = "stop", required = false)
private String stop;
@Element(name = "title", required = false)
private Title title;
@Element(name = "desc", required = false)
private String desc;
public Programme(String channel, String start, String stop, String title, String desc) {
this.channel = channel;
this.start = start;
this.stop = stop;
this.title = title;
this.desc = desc;
}
public String getChannel() {
return channel;
}
public void setChannel(String channel) {
this.channel = channel;
}
public String getStart() {
return start;
}
public void setStart(String start) {
this.start = start;
}
public String getStop() {
return stop;
}
public void setStop(String stop) {
this.stop = stop;
}
public Title getTitle() {
return title;
}
public void setTitle(Title title) {
this.title = title;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
@Root(name = "title")
public class Title {
@Attribute
private String lang;
private String text;
public Title(String lang, String text) {
this.lang = lang;
this.text = text;
}
public String getLang() {
return lang;
}
public void setLang(String lang) {
this.lang = lang;
}
@Text
public String getText() {
return text;
}
@Text
public void setText(String text) {
this.text = text;
}
}
}
给我错误 org.simpleframework.xml.core.ConstructorException: Can not construct inner class
。
有两点需要注意。
1) 如果您在内部元素中为属性嵌套了 class,嵌套的 class 必须是 static
.
2) 定义内部变量class必须注释如下
@Element(name = "title", type = Title.class, required = false)
private Title title;
3) 在构造函数内部,他们应该实例化一个空的 class
this.title = new Title();
这是一个完整的工作版本:
@Root
public class Programme {
@Attribute(name = "channel", required = false)
private final String channel;
@Attribute(name = "start", required = false)
private final String start;
@Attribute(name = "stop", required = false)
private final String stop;
@Element(name = "title", type = Title.class, required = false)
private Title title;
@Element(name = "desc", type = Desc.class, required = false)
private Desc desc;
public Programme(
@Attribute(name = "channel") String channel,
@Attribute(name = "start") String start,
@Attribute(name = "stop") String stop
) {
this.channel = channel;
this.start = start;
this.stop = stop;
this.title = new Title();
this.desc = new Desc();
}
public Title getTitle() {
return title;
}
public void setTitle(Title title) {
this.title = title;
}
public Desc getDesc() {
return desc;
}
public void setDesc(Desc desc) {
this.desc = desc;
}
@Root(name = "title")
public static class Title {
public String text;
@Attribute(name = "lang")
private String lang;
@Text
public String getText() {
return text;
}
@Text
public void setText(String text) {
this.text = text;
}
public String getLang() {
return lang;
}
}
@Root(name = "desc")
public static class Desc {
public String text;
@Attribute(name = "lang")
private String lang;
@Text
public String getText() {
return text;
}
@Text
public void setText(String text) {
this.text = text;
}
public String getLang() {
return lang;
}
}
}