简体   繁体   中英

How to Map different type of element which contains same into single java class objetc using jaxb

My Problem is that client sendind xml request which contains different type of element with same name and i need to have convert into java object. I am adding request below. First request is :-

 <root>
  <params>
    <game>1</game>
  </params>
</root>

And second is :-

<root>
    <params>
         <game>
            <id>1</id>
            <name>Lucky 7</name>
            <translation>Lucky 7</translation>
        </game>
    </params>
</root>

I have looke up on stakeoverflow but i didn't get solution.Hoping some one can help out to add into single java class rather then use multiple java class.

parent element have both child element and text value, use the @XmlMixed annotation for extract the text value of parent element with JAXB unmarshalling

try with following example,

game.xml

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <game>1</game>  <!-- text value -->
    <game>
        <id>1</id>  <!-- child element -->
        <name>Lucky 7</name>
        <translation>Lucky 7</translation>
    </game>
</root>

Root.java

@XmlRootElement
public class Root {

    private List<Game> listGame;

    @XmlElement(name="game")
    public List<Game> getListGame() {
        return listGame;
    }

    public void setListGame(List<Game> listGame) {
        this.listGame = listGame;
    }
}

Game.java

@XmlRootElement
public class Game {

    private List<String> textValue;
    private String id;
    private String name;
    private String translation;

    @XmlMixed
    public List<String> getTextValue() {
        return textValue;
    }
    public void setTextValue(List<String> textValue) {
        this.textValue = textValue;
    }
    @XmlElement
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    @XmlElement
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @XmlElement
    public String getTranslation() {
        return translation;
    }
    public void setTranslation(String translation) {
        this.translation = translation;
    }
}

unmarshalling with JAXB,

File file = new File("game.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Root.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Root root = (Root) jaxbUnmarshaller.unmarshal(file);

for (int i = 0; i < root.getListGame().size(); i++) {
    System.out.println("Game Object "+(i+1));
    if(root.getListGame().get(i).getTextValue().size()>1){
        System.out.println("ID :"+root.getListGame().get(i).getId());
        System.out.println("Name :"+root.getListGame().get(i).getName());
        System.out.println("Translation :"+root.getListGame().get(i).getTranslation());
    }else{
        System.out.println("Value :"+root.getListGame().get(i).getTextValue().get(0));
    }
    System.out.println("------------------------------------");
}

output,

Game Object 1
Text Value :1
------------------------------------
Game Object 2
ID :1
Name :Lucky 7
Translation :Lucky 7
------------------------------------

for more info, https://docs.oracle.com/javaee/5/api/javax/xml/bind/annotation/XmlMixed.html

Thanks,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM