微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Java-内联列表中具有属性的SimpleXML单深度元素

我想解析以下XML

<entry key="9" type="9">
  <value>
    <amount stake="10">50000000</amount>
    <amount stake="1">5000000</amount>
    <winner>0.0</winner>
    <description>9 Correct Numbers</description>
  </value>
</entry>

我尝试通过以下课程实现这一目标:

@Root(name="entry")
public class OddsEntryXMLObject {

    @Attribute(name="key")
    private String iKey;

    @Attribute(name="jackpot", required=false)
    private String iJackpot;

    @Attribute(name="type", required=false)
    private String iType;

    @Element(name="value")
    private OddsValueXMLObject iOddsValueXMLObject;
}

public class OddsAmountXMLObject {

    @Element(name="amount")
    private String iAmount;

    @Attribute(name="stake", required=false)
    private String iStake;
}

但是我得到以下异常:

java.lang.RuntimeException: org.simpleframework.xml.core.ValuerequiredException: Unable to satisfy @org.simpleframework.xml.Element(data=false, name=amount, required=true, type=void) on field 'iAmount' private java.lang.String OddsAmountXMLObject.iAmount for class OddsAmountXMLObject at line 1

有人知道如何解析吗?

解决方法:

提供的代码中没有OddsValueXMLObject类的声明.
假设声明是我的想法,
错误消息基本上意味着它期望在数量元素中包含一个数量元素,
像这样:

<amount>
    <amount></amount>
</amount>

当然,您拥有的XML中没有类似的东西.
您可以使用以下方法解析它:

@Root(name="entry")
public class OddsEntryXMLObject{
    @Attribute(name="key")
    private String key;

    @Attribute(name="jackpot", required=false)
    private String jackpot;

    @Attribute(name="type", required=false)
    private String type;

    @Element(name="value")
    private OddsValueXMLObject value;
}

@Root(name="value")
public class OddsValueXMLObject{
    @ElementList(inline=true)
    private java.util.List<OddsAmountXMLObject> amounts;

    @Element(name="winner", required=false)
    private String winner;

    @Element(name="description", required=false)
    private String description;
}

@Root(name="amount")
public class OddsAmountXMLObject{
    @Attribute(name="stake", required=false)
    private String stake; 

    @Text
    private String text;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。