Java - JAXB - Unmarshalling:如何实现正确的设置?

时间:2016-02-23 12:22:46

标签: java xml jaxb unmarshalling

我目前正在尝试将大量XML转换为Java对象,但我一直陷入困境。我曾尝试在网上复制许多不同的例子,但我似乎永远无法找到正确的方法而且我发现它很难调试。

我的XML看起来像这样

<?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>
...

所以我只有一个{I}列表,我想保留为Team对象。我的Team类的Java代码目前就像这样

TeamLeagueStanding

我的团队课程只是为了保存团队列表就像这样

@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;
    }

}

我的主要功能是这样的

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

    @XmlElement
    List<Team> teamList;

    public Teams () {

    }

}

我已经尝试了很多这样的方法,我总是得到一个空指针异常或各种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()); } } 。如果有人知道我哪里出错了,我会非常感谢任何指针!

谢谢,

西蒙

3 个答案:

答案 0 :(得分:0)

我认为你应该添加@XmlAccessorType(XmlAccessType.FIELD)注释

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

  }

答案 1 :(得分:0)

您的根元素似乎是XMLSOCCER.COM
1。创建xsd
2.使用xjc命令创建java bean类,或者可以使用IDE创建对象 3.在src下创建xml bean对象 4.然后你将xml传递给unmarshelled
5.您将获得可以访问的分层转换对象。

答案 2 :(得分:0)

  1. 将默认构造函数添加到Team

  2. 添加

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

    Teams课程;

  3. <强> 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 () {
    
        }
    
    }