开发者

Jaxb 2 mapping different element to the same property

I wonder if this is possible in jaxb2. I have a xml that can be either < element > or < element2 > it will never apear at the some time. They are both of type String and I want it to map to the same property in my java class. Is ther开发者_开发问答e a way in jaxb2 to configure that?

Thanks Charlie


What would happen if you tried to serialize back to XML? Which element name would be used?

Assuming you only need to deserialize from XML to Java, then you can do this by annotating your setter methods instead of your fields:

public class Bean {

   private String value;

   @XmlElement(name="element")
   public void setA(String value) {
      this.value = value;
   }

   @XmlElement(name="element2")
   public void setB(String value) {
      this.value = value;
   }
}

You might also have to add getA() and getB() methods in order for JAXB to recognise the setters properly.


You can do something like that:

@XmlElements({
    @XmlElement(name="command", type=CommandVO.class, namespace="http://chains.projetox.com.br/"),
    @XmlElement(name="script", type=ScriptVO.class, namespace="http://chains.projetox.com.br/")
})
private List<SubjectVO> commands;

Where:

public interface SubjectVO {}
public class CommandVO implements SubjectVO {}
public class ScriptVO implements SubjectVO {}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜