How to convert java object to xml using JAXB
to get the following xml:
<Case>
<Version>1.0</Version>
<Code>457123</Code>
<Meta uc="Sample" pip="116.0.1.1" lot="P"/>
</Case>
There are many answers regarding how to get XML. I have gone through all those. But my question is how to get the XML as what I have shown. It contains a self-closing tag which even contains attributes.
I am using Eclipse IDE. Please suggest a method.
This is my case class:
import auth.Res.Meta;
@XmlRootElement (name="Case")
public class Test {
private Meta mt;
private String version;
private String code;
@XmlRootElement
public class Meta {
@XmlAttribute
private String uc;
@XmlAttribute
private String pip;
public String getUc() {
return uc;
}
public void setUc(String uc) {
this.uc = uc;
}
public String getPip() {
return pip;
}
public void setPip(String pip) {
this.pip = pip;
}
}
public Meta getMt() {
return mt;
}
public void setMt(Meta mt) {
this.mt = mt;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
Solution:
I solved it by creating seperate class for Meta as suggested by LazerBanana in the first answer.
See Question&Answers more detail:os