带有内联 ElementList 的 SimpleXML ValueRequiredException,即使列表不是必需的并且严格为 false

SimpleXML ValueRequiredException with inline ElementList, even though the list is not required and strict is false

注意:此问题的完整来源可在此 repo 中查看:https://github.com/jakelauer/BaseballTheaterAndroid/tree/master/app/src/main/java/com/jakelauer/baseballtheater/MlbDataServer/DataStructures/Innings

我正在开发一个下载 XML 文件(我不创建或控制)并将它们反序列化为 Java classes 的应用程序。您可以在此处查看我在这种情况下使用的 XML 文件的示例:https://gd.mlb.com/components/game/mlb/year_2017/month_06/day_22/gid_2017_06_22_arimlb_colmlb_1/inning/inning_all.xml

这是与问题相关的 XML 的一小段:

<game atBat="642162" deck="608596" hole="506560" ind="F">
    <inning num="1" away_team="ari" home_team="col" next="Y">
        <top>
            <atbat num="1" b="4" s="2" o="0" start_tfs="191031" start_tfs_zulu="2017-06-22T19:10:31Z" end_tfs_zulu="2017-06-22T19:14:02Z" batter="571757" stand="L" b_height="6-3" pitcher="622608" p_throws="R" des="Jeremy Hazelbaker walks. " des_es="Jeremy Hazelbaker recibe base por bolas. " event_num="13" event="Walk" event_es="Base por Bolas" play_guid="1d0aa470-e4c7-4736-ad91-b59c04cf14a8" home_team_runs="0" away_team_runs="0">
                <pitch des="Called Strike" des_es="Strike cantado" id="3" type="S" code="C" tfs="191054" tfs_zulu="2017-06-22T19:10:54Z" x="124.31" y="182.73" event_num="3" sv_id="170622_191059" play_guid="86089b60-ec6c-49d4-b2c0-1a2edd815cd4" start_speed="93.5" end_speed="85.8" sz_top="3.353940488752137" sz_bot="1.5283581954007417" pfx_x="-2.565769076580456" pfx_z="9.078788390129112" px="-0.17520982777496" pz="2.08403513760305" x0="-2.16181902610651" y0="49.9999999999999" z0="5.85297900032968" vx0="6.22576430601066" vy0="-135.647474124121" vz0="-7.28516923595343" ax="-4.80335370185824" ay="28.3485781581515" az="-15.1777287316417" break_y="23.8" break_angle="12.0" break_length="3.7" pitch_type="FF" type_confidence="2.000" zone="8" nasty="48" spin_dir="195.779" spin_rate="1888.991" cc="" mt="" />
                <pitch des="Ball" des_es="Bola mala" id="4" type="B" code="B" tfs="191108" tfs_zulu="2017-06-22T19:11:08Z" x="130.95" y="112.7" event_num="4" sv_id="170622_191113" play_guid="22752449-8f76-47d4-abce-abe0ded4e427" start_speed="94.7" end_speed="86.7" sz_top="3.3778310421410467" sz_bot="1.469692166057115" pfx_x="-1.1549210591353427" pfx_z="8.745194943929556" px="-0.351001163939361" pz="4.68350266164169" x0="-2.41267101485961" y0="49.9999999999999" z0="5.9184565100435" vx0="6.01926114542772" vy0="-137.561473360534" vz0="-0.543580304599218" ax="-2.2239931834547" ay="29.0987408853003" az="-15.333716665831" break_y="23.8" break_angle="3.6" break_length="3.3" pitch_type="FF" type_confidence="2.000" zone="11" nasty="31" spin_dir="187.521" spin_rate="1791.469" cc="" mt="" />
                <pitch des="Swinging Strike" des_es="Strike tirándole" id="5" type="S" code="S" tfs="191124" tfs_zulu="2017-06-22T19:11:24Z" x="130.64" y="151.27" event_num="5" sv_id="170622_191129" play_guid="39cd4eec-0deb-45c4-8785-f6cadaf9f50c" start_speed="95.3" end_speed="87.6" sz_top="3.305347300971924" sz_bot="1.3971657207664023" pfx_x="0.9303457203819229" pfx_z="8.252307222918" px="-0.356878305663863" pz="3.23649786934813" x0="-2.32678670886771" y0="50" z0="5.71446528744102" vx0="5.06861064335786" vy0="-138.4355985496" vz0="-3.86723211048484" ax="1.81868374351758" ay="28.7792513273701" az="-16.0420487374035" break_y="23.8" break_angle="-9.6" break_length="3.5" pitch_type="FF" type_confidence="2.000" zone="1" nasty="47" spin_dir="173.570" spin_rate="1701.312" cc="" mt="" />
                <pitch des="Ball" des_es="Bola mala" id="6" type="B" code="B" tfs="191147" tfs_zulu="2017-06-22T19:11:47Z" x="109.9" y="128.05" event_num="6" sv_id="170622_191152" play_guid="9fa0fa66-4bc4-4d7e-9078-bc18cc619ee9" start_speed="95.3" end_speed="87.5" sz_top="3.413203378487964" sz_bot="1.4932860583914263" pfx_x="-2.585074880702412" pfx_z="9.048553099227348" px="0.186085130432929" pz="4.11496070476484" x0="-2.32211750905271" y0="50" z0="5.85981126528278" vx0="7.79658471988327" vy0="-138.408327022057" vz0="-2.13943178079392" ax="-5.05208357268219" ay="28.7305177555245" az="-14.4902101460214" break_y="23.8" break_angle="13.0" break_length="3.2" pitch_type="FF" type_confidence="2.000" zone="12" nasty="25" spin_dir="195.943" spin_rate="1927.758" cc="" mt="" />
                <pitch des="Foul" des_es="Foul" id="7" type="S" code="F" tfs="191207" tfs_zulu="2017-06-22T19:12:07Z" x="125.14" y="170.35" event_num="7" sv_id="170622_191213" play_guid="068e7271-364f-41a2-bc2f-5acb3d471567" start_speed="89.0" end_speed="82.6" sz_top="3.3053472635938235" sz_bot="1.3971656832243298" pfx_x="0.47530291203460084" pfx_z="7.747496289220608" px="-0.202395676315462" pz="2.54715120094639" x0="-2.32840895591854" y0="50" z0="5.91883815545783" vx0="5.30793097952283" vy0="-129.465165595541" vz0="-5.0088251153945" ax="0.818557320704646" ay="23.3086468750926" az="-18.8314629250343" break_y="23.9" break_angle="-5.0" break_length="4.6" pitch_type="FF" type_confidence="2.000" zone="5" nasty="29" spin_dir="176.491" spin_rate="1497.971" cc="" mt="" />
                <pitch des="Ball" des_es="Bola mala" id="8" type="B" code="B" tfs="191246" tfs_zulu="2017-06-22T19:12:46Z" x="66.33" y="164.76" event_num="8" sv_id="170622_191251" play_guid="c35c98ce-2663-46aa-a733-d006a3ecd57c" start_speed="96.6" end_speed="89.9" sz_top="3.366040101804586" sz_bot="1.387111860850247" pfx_x="-2.74995954813972" pfx_z="7.87359686115" px="1.3247022142978" pz="2.74210300089655" x0="-2.37583692305477" y0="49.9999999999999" z0="5.65892137235119" vx0="11.3340185394337" vy0="-140.257011116037" vz0="-5.24923920318439" ax="-5.58945771012769" ay="25.6662510017697" az="-16.1704907180022" break_y="23.9" break_angle="11.8" break_length="3.5" pitch_type="FF" type_confidence="2.000" zone="12" nasty="25" spin_dir="199.251" spin_rate="1753.422" cc="" mt="" />
                <runner id="571757" start="" end="1B" event="Walk" event_num="13" />
            </atbat>
        </top>
    </inning>
</game>

这是我为反序列化创建的 classes:

@Root(name = "game", strict = false)
public class PlayByPlay
{
    @Attribute
    public String atBat;

    @Attribute
    public String deck;

    @Attribute
    public String hole;

    @Attribute
    public String ind;

    @ElementList(name = "inning", inline = true, required = false)
    public List<Inning> innings;
}



@Root(name = "inning", strict = false)
public class Inning
{
    @Attribute
    public String num;

    @Attribute
    public String away_team;

    @Attribute
    public String home_team;

    @Attribute
    public Next next;

    @Element(required = false)
    public InningHalf top;

    @Element(required = false)
    public InningHalf bottom;
}


@Root(strict = false)
public class InningHalf
{
    @ElementList(name = "atbat", inline = true, required = false)
    public List<AtBat> atbat;
}


@Root(name = "atbat", strict = false)
public class AtBat
{
    @Attribute
    public int num;

    @Attribute
    public int b;

    @Attribute
    public int s;

    @Attribute
    public int o;

    @Attribute
    public String start_tfs;

    @Attribute
    public String start_tfs_zulu;

    @Attribute
    public String end_tfs_zulu;

    @Attribute
    public int batter;

    //$todo
    public Player batterData;

    @Attribute
    public String stand;

    @Attribute
    public String b_height;

    @Attribute
    public String pitcher;

    @Attribute
    public String p_throws;

    @Attribute
    public String des;

    @Attribute
    public String event_num;

    @Attribute
    public String event;

    @Attribute
    public String home_team_runs;

    @Attribute
    public String away_team_runs;

    @ElementList(name = "pitch", inline = true, required = false)
    public List<Pitch> pitch;
}



public enum Next
{
    Y,
    N
}


@Root(name = "pitch", strict = false)
public class Pitch
{
    @Attribute
    public String ax;

    @Attribute
    public String ay;

    @Attribute
    public String az;

    @Attribute
    public String break_angle;

    @Attribute
    public String break_length;

    @Attribute
    public String break_y;

    @Attribute
    public String cc;

    @Attribute
    public String code;

    @Attribute
    public String des;

    @Attribute
    public String end_speed;

    @Attribute
    public String event_num;

    @Attribute
    public String id;

    @Attribute
    public String mt;

    @Attribute
    public String pfx_x;

    @Attribute
    public String pfx_z;

    @Attribute
    public String pitch_type;

    @Attribute
    public String play_guid;

    @Attribute
    public String px;

    @Attribute
    public String pz;

    @Attribute
    public String spin_dir;

    @Attribute
    public String spin_rate;

    @Attribute
    public String start_speed;

    @Attribute
    public String sv_id;

    @Attribute
    public String sz_bot;

    @Attribute
    public String sz_top;

    @Attribute
    public String tfs;

    @Attribute
    public String tfs_zulu;

    @Attribute
    public String type;

    @Attribute
    public String type_confidence;

    @Attribute
    public String vx0;

    @Attribute
    public String vy0;

    @Attribute
    public String vz0;

    @Attribute
    public String x;

    @Attribute
    public String x0;

    @Attribute
    public String y;

    @Attribute
    public String y0;

    @Attribute
    public String z0;

    @Attribute
    public String zone;
}

使用上面的 classes,我得到这个异常:

org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.Attribute(empty=, name=, required=true) on field 'ax' public java.lang.String com.jakelauer.baseballtheater.MlbDataServer.DataStructures.Innings.Pitch.ax for class com.jakelauer.baseballtheater.MlbDataServer.DataStructures.Innings.Pitch at line 1
     at org.simpleframework.xml.core.Composite.validate(Composite.java:644)
     at org.simpleframework.xml.core.Composite.readAttributes(Composite.java:416)

看来问题出在 AtBat class 的 List<Pitch> pitch 部分,但我不明白为什么。我曾尝试删除 inline = true,这确实消除了异常,但随后整个变量为空。

相同的格式适用于其他 class 中的 List<Inning> inningsList<AtBat> atbat 变量,所以我不确定这是怎么回事。

有什么想法吗?

编辑:

如果我删除 List<Pitch> pitch 变量的 @ElementList 属性的 inlinename 部分,我反而会得到这个错误:

 org.simpleframework.xml.core.PersistenceException: Element 'pitch' is already used with @org.simpleframework.xml.ElementList(data=false, empty=true, entry=, inline=false, name=, required=false, type=void) on field 'pitch' public java.util.List com.jakelauer.baseballtheater.MlbDataServer.DataStructures.Innings.AtBat.pitch at line 1
     at org.simpleframework.xml.core.Variable$Adapter.read(Variable.java:484)

除了将属性标记为 required = false.

之外,我无法确定答案