gpt4 book ai didi

java - 使用 XML 返回选定的日期以便 XMLElements

转载 作者:行者123 更新时间:2023-11-30 06:35:50 25 4
gpt4 key购买 nike

我正在尝试使用返回XML的api。API 网址:api

这就是我的 XML 类的外观:

@XmlRootElement(name="ArrayOfExchangeRatesTable")
@XmlAccessorType(XmlAccessType.FIELD)
public class RootElement {

@XmlElement(name="ExchangeRatesTable")
private TableRateModel tableRateModel;

@XmlAccessorType(XmlAccessType.FIELD)
public class TableRateModel {

@XmlElement(name="Table")
private String table;
@XmlElement(name="EffectiveDate")
private Date effectiveDate;
@XmlElement(name="Rates")
private List<RateModel> rates;

@XmlAccessorType(XmlAccessType.FIELD)
public class RateModel {

@XmlElement(name="Currency")
private Currency currency;
@XmlElement(name="Code")
private String code;
@XmlElement(name="Mid")
private Double mid;

最后我打印了它,得到的结果是:

RootElement{tableRateModel=TableRateModel{table='A', effectiveDate=Thu Jul 20 00:00:00 CEST 2017, rates=[RateModel{currency=null, code='null', mid=null}]}}

为什么费率列表为空?

最佳答案

问题在于 XML 元素 Rate 包装在名为 Rates 的元素内。我测试了以下类(class)并使其正常工作

@XmlRootElement(name = "ArrayOfExchangeRatesTable")
@XmlAccessorType(XmlAccessType.FIELD)
public class RootElement {
@XmlElement(name = "ExchangeRatesTable")
private TableRateModel tableRateModel;
}

@XmlAccessorType(XmlAccessType.FIELD)
class TableRateModel {
@XmlElement(name = "Table")
private String table;
@XmlElement(name = "EffectiveDate")
private Date effectiveDate;
@XmlElement(name = "Rates")
private Rates rates;
}

@XmlAccessorType(XmlAccessType.FIELD)
class Rates {
@XmlElement(name = "Rate")
private List<Rate> rates;
}

@XmlAccessorType(XmlAccessType.FIELD)
class Rate {
@XmlElement(name = "Currency")
private String currency;
@XmlElement(name = "Code")
private String code;
@XmlElement(name = "Mid")
private Double mid;
}

Currency 显然不可序列化,因为它没有无参数构造函数。一种解决方法是添加一个返回从字符串解析的 Currency 的 getter:

@XmlAccessorType(XmlAccessType.FIELD)
class Rate {
@XmlElement(name = "Currency")
private String currency;
@Xml Element(name = "Code")
private String code;
@XmlElement(name = "Mid")
private Double mid;

public Currency getCurrency() {
return Currency.getInstance(currency);
}
}

关于java - 使用 XML 返回选定的日期以便 XMLElements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45220953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com