Xml解析为Java对象

时间:2016-05-04 11:44:58

标签: java xml parsing

我有这样的xml:

 <EVENTSTREAM id="2016050408105300002" name="LATESTWINNERS" rc="0">
    <ROUND id="2016050309342400001" nyxid="2016050309342400003">
    <TIMESTAMP>2016-05-03T09:34:24.000</TIMESTAMP>
    <PLAYERINFO accountid="112307829" firstname="Simon" lastname="Schork" nickname="SimonSchork" zip="76133" operatorid="275" currency="EUR" language="" country="DE" city="SimonSchork" sex="M" universe="Default"/>
    <CHANNEL>I</CHANNEL>
    <BALANCE>110.34</BALANCE>
    <WONAMOUNT>3.00</WONAMOUNT>
    <BONUSWONAMOUNT>0.00</BONUSWONAMOUNT>
    <SUBSYSTEMID>3</SUBSYSTEMID>
    <CAT1>1</CAT1>
    <CAT2>1</CAT2>
    <CAT3>30136</CAT3>
    <OUTCOME>W</OUTCOME>
    <CLIENT>flash</CLIENT>
    <BETAMOUNT>1.00</BETAMOUNT>
    <BONUSBETAMOUNT>0.00</BONUSBETAMOUNT>
    <BASECURRENCY>EUR</BASECURRENCY>
    </ROUND>

我创建了这样的模型,LastWinnersModel

@Root(name = "EVENTSTREAM", strict = false)
public class LastWinnersModel {

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

    @Attribute(name = "rc")
    private int rc;

    @Attribute(name = "id")
    private long id;

    @ElementList(name = "ROUND", entry = "ROUND", inline = true, required = false)
    private List<RoundModel> lastWinners = new ArrayList<>();

RoundModel

@Root(name = "ROUND", strict = false)
public class RoundModel {

    @Attribute(name = "id")
    private long id;

    @Attribute(name = "nyxid")
    private long nyxId;

    @Element(required = false)
    private LastWinnerModel lastWinnerModel;

和LastWinnerModel

public class LastWinnerModel {

    @Attribute(name = "TIMESTAMP")
    private String date;

    @Attribute(name = "CHANNEL")
    private String channel;

    @Attribute(name = "BALANCE")
    private BigDecimal balance;

    @Attribute(name = "WONAMOUNT")
    private BigDecimal wonAmonut;

    @Attribute(name = "BONUSWONAMOUNT")
    private BigDecimal bonusWonAmount;

    @Attribute(name = "SUBSYTEMID")
    private int subsystemId;

    @Attribute(name = "CAT1")
    private int cat1;

    @Attribute(name = "CAT2")
    private int cat2;

    @Attribute(name = "CAT3")
    private int cat3;

    @Attribute(name = "OUTCOME")
    private String outcome;

    @Attribute(name = "CLIENT")
    private String client;

    @Attribute(name = "BETAMOUNT")
    private BigDecimal betAmount;

    @Attribute(name = "BONUSBETAMOUNT")
    private BigDecimal bonusBetAmount;

    @Attribute(name = "BASECURRENCY")
    private String baseCurrency;

    @Attribute(name = "PLAYERINFO")
    private PlayerInfoModel playerInfo;

在reposne中

@Element(required = false)
        private LastWinnerModel lastWinnerModel;
尽管我从外部服务收到一些数据,但

始终为空。我想问题是LastWinnerModel没有root或者我的模型中有错误吗?感谢您的回复。

1 个答案:

答案 0 :(得分:1)

似乎您没有使用JAXB进行XML绑定,而是使用类似(但不完全相同的注释)的类似Framework。我只能回答JAXB,但很可能与你的框架有同样的问题:

似乎,模型的嵌套并不反映XML中的嵌套。 在XML中,TIMESTAMP标记是ROUND标记的直接子标记,即正确缩进XML是这样的:

<EVENTSTREAM...
    <ROUND...
        <TIMESTAMP...

因此JAXB会尝试将TIMESTAMP标记映射到RoundModel类中的变量。但是,这样的变量不存在,因此TIMESTAMP的XML值根本不会映射到您的模型中而丢失(对于CHANEL,BALANCE ......也是如此)。

您定义的模型将映射到此结构的XML:

<EVENTSTREAM ...
    <ROUND...
        <lastWinnerModel...
            <TIMESTAMP...

但是如果你不能改变XML,在JAXB中,我不知道通过添加另一个神奇的注释来映射模型的一种非常简单的方法。 (例如,您可以在JAXB中使用XmlAdapter类。)因此,如果您坚持使用该XML,那么将RoundModel和LastWinnerModel合并到一个类中是最简单的方法。