JAXB - 如何将xml数组映射到对象列表

时间:2014-09-27 06:59:11

标签: java xml xml-parsing jaxb

我有一个包含此内容的xml文件。每笔交易以trans_开头,以交易号

结束
<trans_1>
    <date>2013/10/10</date>
    <code>109129</code>
    <amount>+000000024000000</amount>
    <balance>+000000024000000</balance>
</trans_1>
<trans_2>
    <date>2013/10/10</date>
    <code>109149</code>
    <amount>+000000025000000</amount>
    <balance>+000000049000000</balance>
</trans_2>
<trans_3>
    <date>2013/10/10</date>
    <code>109161</code>
    <amount>+000000005000000</amount>
    <balance>+000000054000000</balance>
</trans_3>

我想将此数组映射到以下对象列表。

class Transaction{
    string date;
    string code;
    string amount;
    string balance;


   /**** getter and setter ***/
}

1 个答案:

答案 0 :(得分:0)

我找到了@XmlElements

的解决方案
@XmlAccessorType(XmlAccessType.FIELD)
public class DepositLast3CycleResponse extends BaseResponseBean {

    @XmlElements(
            {
                    @XmlElement(name = "trans_1"),
                    @XmlElement(name = "trans_2"),
                    @XmlElement(name = "trans_3")
            }
    )
    private List<TransactionInfo> transactions;


    /****** getters and setters ****/
}