【问题标题】:Java - JAXB - Unmarshalling : How to achieve the correct set up?Java - JAXB - Unmarshalling:如何实现正确的设置?
【发布时间】:2016-02-23 12:22:46
【问题描述】:

我目前正在尝试将大量 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>
...

所以我只有一个 TeamLeagueStanding 列表,我想将其保留为 Team 对象。我的 Team 类的 java 代码目前是这样的

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

}

我的 Teams 类就是用来保存 Teams 列表的

@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());

    }

}

我已经尝试了很多方法,但总是得到空指针异常或各种IllegalAnnotationExceptions。如果有人知道我哪里出错了,我将不胜感激!

谢谢,

西蒙

【问题讨论】:

  • 考虑指定一个 XSD 并使用 xjc 生成 JAXB 类。 (注意:根元素只在根有效)
  • 所以我认为问题出在我的 XML 而不是我的 Java - 我删除了 xmlns 属性并且下面的代码有效。

标签: java xml jaxb unmarshalling


【解决方案1】:

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

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

  }

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 其实我还是推荐通过maven-jaxb2-plugin调用xjc,默认也会生成target下的JAXB类。您不应该将它们签入您的 VCS。
    【解决方案3】:
    1. Team类添加默认构造函数

    2. 添加

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

      Teams类;


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

    【讨论】:

    • 感谢您的回复。我已经尝试过了,我在 teamList 上得到了 NullPointerEx
    猜你喜欢
    • 2011-04-20
    • 1970-01-01
    • 2020-10-11
    • 2016-02-15
    • 2013-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多