W A is correct that your JAXB implementation is doing exactly what you told it to do (see: http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-xsitype.html). If you don't want to change your XML schema, below is how you can change your mappings.
Java Model
Result
We will use the @XmlElement
annotation on the content
field to indicate that the real type is Job
(see: http://blog.bdoughan.com/2011/05/jaxb-and-interface-fronted-models.html).
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlType(propOrder = { "state", "content" })
@XmlAccessorType(XmlAccessType.FIELD)
public class Result {
protected State state;
@XmlElement(type=Job.class)
protected Content content;
}
Content
We will use the @XmlTransient
annotation on the Content
class to remove it from the inheritance hierarchy (see: http://blog.bdoughan.com/2011/06/ignoring-inheritance-with-xmltransient.html).
import javax.xml.bind.annotation.XmlTransient;
@XmlTransient
public class Content {
}
Job
Job
is mapped normally.
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class Job extends Content {
protected String status;
}
Demo Code
Demo
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Result.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum17993543/input.xml");
Result result = (Result) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(result,System.out);
}
}
input.xml/Output
<?xml version="1.0" encoding="UTF-8"?>
<result>
<state>
<tag>value</tag>
</state>
<content>
<status>ok</status>
</content>
</result>