[英]JAXB inheritance + element rename
I have the following construction: 我有以下构造:
@XMLTransient
public abstract class Foo {
protected String name;
}
@XmlRootElement
@XmlType(propOrder={"name"})
public class BarX extends Foo {
public String getXThing() {
return name;
}
public void setXThing(String thing) {
name = thing;
}
}
@XmlRootElement
@XmlType(propOrder={"name"})
public class BarY extends Foo {
public String getYBlah() {
return name;
}
public void setYBlah(String blah) {
name = blah;
}
}
Within the XML I need for BarX instead of name
the tag thing
and for BarY I would like to have blah
instead of name
. 在XML内,我需要BarX而不是name
标签的thing
而对于BarY,我想拥有blah
而不是name
。 Is it possible and how I can get this? 有可能吗,我怎么能得到呢?
You can do the following (you were already pretty close): 您可以执行以下操作(您已经很接近了):
Foo 富
package forum11340316;
import javax.xml.bind.annotation.XmlTransient;
@XmlTransient
public abstract class Foo {
protected String name;
}
BarX BarX
package forum11340316;
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlType(propOrder={"XThing"})
public class BarX extends Foo {
@XmlElement(name="thing")
public String getXThing() {
return name;
}
public void setXThing(String thing) {
name = thing;
}
}
BarY 巴里
package forum11340316;
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlType(propOrder={"YBlah"})
public class BarY extends Foo {
@XmlElement(name="blah")
public String getYBlah() {
return name;
}
public void setYBlah(String blah) {
name = blah;
}
}
Demo 演示
package forum11340316;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(BarX.class, BarY.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
BarX barX = new BarX();
barX.setXThing("XThing");
marshaller.marshal(barX, System.out);
BarY barY = new BarY();
barY.setYBlah("YBlah");
marshaller.marshal(barY, System.out);
}
}
Output 产量
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<barX>
<thing>XThing</thing>
</barX>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<barY>
<blah>YBlah</blah>
</barY>
For More Information 欲获得更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.