Problem with JAXB unmarshalling classes that have the same name in @XmlRootElement

sboehm

Neues Mitglied
Hi guys,

I am receiving some simple XML messages from a device. Here two examples:

[XML]
<Response Action="Initialize" Result="1" Lane=”1” />
[/XML]
and
[XML]
<Response Action="CancelTransaction" Result="0" />
[/XML]

Well, now I have to unmarshall these messages to two different Java objects. I am using JAXB's for this annotations:

Java:
@XmlRootElement(name = "Response")
public class InitializeResponseMessage {

	@XmlAttribute(name = "Action")
	private Integer action;

	@XmlAttribute(name = "Result")
	private Integer result;

	@XmlAttribute(name = "Lane")
	private Integer lane;

	public InitializeResponseMessage() {}
}
and
Java:
@XmlRootElement(name = "Response")
public class CancelTransactionResponseMessage {

	@XmlAttribute(name = "Action")
	private Integer action;

	@XmlAttribute(name = "Result")
	private Integer result;

	public InitializeResponseMessage() {}
}

The problem now is that the unmarshaller does not recognize that the two XML messages are different object. Of course not because they have the same XML tag. The unmarshaller used the Java type that is defined last when creating the JAXBContext instance.

It there a possibility to decide with the "Action" attribute which Java object the unmarshaller has to create? ???:L
 
the problem is that the unmarshaller creates objects only of one time (the last java class in the list for the JAXB context creation)

the attributes are set if both classes contains them (e.g. action) for a xml initialize message xml string a wrong java class is used and the lane attribute is not set because it does only exist in the initialize message class
 

Neue Themen


Zurück
Oben