doblo7
doblo7

Reputation: 11

Java - JAXB - Unmarshalling : How to achieve the correct set up?

I'm currently trying to change lots of XML into Java objects but I keep getting stuck. I have tried copying lots of different examples online but I can never seem to get the right way and I find it very hard to debug.

My XML looks like this

<?xml version="1.0" encoding="utf-8"?>
<XMLSOCCER.COM>
  <TeamLeagueStanding xmlns="http://xmlsoccer.com/LeagueStanding">
    <Team>Leicester</Team>
    <Team_Id>31</Team_Id>
    <Played>26</Played>
    <PlayedAtHome>12</PlayedAtHome>
    <PlayedAway>14</PlayedAway>
    <Won>15</Won>
    <Draw>8</Draw>
    <Lost>3</Lost>
    <NumberOfShots>464</NumberOfShots>
    <YellowCards>40</YellowCards>
    <RedCards>1</RedCards>
    <Goals_For>48</Goals_For>
    <Goals_Against>29</Goals_Against>
    <Goal_Difference>19</Goal_Difference>
    <Points>53</Points>
  </TeamLeagueStanding>
  <TeamLeagueStanding xmlns="http://xmlsoccer.com/LeagueStanding">
    <Team>Tottenham</Team>
    <Team_Id>21</Team_Id>
...

So I just have a list of TeamLeagueStanding s that I want to keep as Team objects. My java code for the Team class is currently like this

@XmlRootElement(name = "TeamLeagueStanding")
public class Team {

    @XmlElement(name = "Team")
    String teamName;
    @XmlElement(name = "Team_Id")
    int teamID;

    public Team (String team, int id) {
        super();
        this.teamName = team;
        this.teamID = id;
    }

}

My Teams class which is just to hold the list of Teams is like this

@XmlRootElement(name = "XMLSOCCER.COM")
public class Teams {

    @XmlElement
    List<Team> teamList;

    public Teams () {

    }

}

and my main function is like this

public class Main {

    public static void main(String[] args) throws Exception {

        File xml = new File("data/GetLeagueStandingsPrem1516.xml");
        JAXBContext jc = JAXBContext.newInstance(Teams.class);
        Unmarshaller um = jc.createUnmarshaller();

        Teams t = (Teams) um.unmarshal(xml);

        System.out.println(t.teamList.size());

    }

}

I've tried this so many ways and I always get either a null pointer exception or various IllegalAnnotationExceptions. If anyone has any idea where I'm going wrong I would greatly appreciate any pointers!

Thanks,

Simon

Upvotes: 1

Views: 413

Answers (3)

Roman Cherepanov
Roman Cherepanov

Reputation: 1805

  1. Add default constructor to Team class

  2. Add

    @XmlElement(name = "TeamLeagueStanding")
    List<Team> teamList;
    

    to Teams class;


Team.class:

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "TeamLeagueStanding")
public class Team {

    @XmlElement(name = "Team")
    String teamName;
    @XmlElement(name = "Team_Id")
    int teamID;

    public Team (){

    }

    public Team (String team, int id) {
        super();
        this.teamName = team;
        this.teamID = id;
    }
}

Teams.class

import javax.xml.bind.annotation.*;
import java.util.List;

@XmlRootElement(name = "XMLSOCCER.COM")
@XmlAccessorType(XmlAccessType.FIELD)
public class Teams {

    @XmlElement(name = "TeamLeagueStanding")
    List<Team> teamList;

    public Teams () {

    }

}

Upvotes: 0

Shiv B
Shiv B

Reputation: 1

Your root element seems to be XMLSOCCER.COM
1. create xsd
2. using xjc command create java bean classes or you can use IDE to create objects.
3.keep created xml bean objects under src
4. then you pass the xml to be unmarshelled
5. you will get converted objects hierarchically you can access.

Upvotes: 0

Zak FST
Zak FST

Reputation: 371

I think you should add @XmlAccessorType(XmlAccessType.FIELD) annotation

 @XmlRootElement(name = "TeamLeagueStanding")
 @XmlAccessorType(XmlAccessType.FIELD)
 public class Team {

  }

Upvotes: 1

Related Questions