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



## sboehm (27. Okt 2011)

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:


```
@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

```
@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


----------



## nrg (28. Okt 2011)

where is the problem with one class and an optional attribute lane?


----------



## sboehm (28. Okt 2011)

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


----------

