I looking for a Java annotation which does the same like XmlInclude does in C#.
I get a XML structure over a socket. The structure looks like this:
<Answer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<FunctionReturnCode>0</FunctionReturnCode>
<AnswerObject xsi:type="Status">
<DoorOpen>79</DoorOpen>
</AnswerObject>
</Answer>
The corresponding Java Class is defined as follows:
@XmlRootElement(name="Answer")
@XmlType(propOrder = {"functionReturnCode", "answerObject"})
public class Answer
{
private Object m_answerObject = null;
private long m_uiFunctionReturnCode = 0;
@XmlElement(name="FunctionReturnCode")
public long getFunctionReturnCode(){ return this.m_uiFunctionReturnCode; }
public void setFunctionReturnCode(long _uiFunctionReturnCode) { this.m_uiFunctionReturnCode = _uiFunctionReturnCode; }
@XmlElement(name="AnswerObject")
public Object getAnswerObject() { return this.m_answerObject; }
public void setAnswerObject(Object _answerObject) { this.m_answerObject = _answerObject;}
}
In C# the class looks something like this:
[XmlInclude(typeof(SelStatus))]
<<< this seems to me the magic point public class Answer : ICloneable
{
private uint m_uiFunctionReturnCode = 0;
private object m_answerObject = null;
.....Setters/Getters here as well
}
The problem is, that "AnswerObject" can be any type of object. In my example "AnswerObject" is an object of type "Status", but it can be as well a string or what ever.
In C# I can use XmlSerializer to de-serialize the XML structure. In Java I use the following :
JAXBContext context = JAXBContext.newInstance(Answer.class);
Unmarshaller unmarschaller = context.createUnmarshaller();
C# and Java (as well) handles strings automatically. But in the case there is another object then a string I can announce C# with XmlInclude other known classes. Is there something similar in Java?