英文:
How to make an object list from XML with Java Spring?
问题
我想从一个[API][1]中获取数据,它会给我XML格式的数据,类似于[这样][2]:
我可以将最后一个映射到一个对象。请参见下面的代码:
public void getCurrencies() {
XmlMapper xmlMapper = new XmlMapper();
Currency currency = null;
try {
currency = xmlMapper.readValue(
new URL("https://www.lb.lt/webservices/FxRates/FxRates.asmx/getCurrentFxRates?tp=eu"),
Currency.class);
CurrencyBase c = currency.getCurrencyBase();
CurrencyAmount d = c.getCurrencyAmount();
System.out.println(d.getName());
} catch (Exception e) {
System.out.println(e);
}
}
但是如何将XML数据解析为对象列表呢?
我的货币模型:
@JacksonXmlRootElement(localName = "FxRates")
public class Currency {
@JacksonXmlProperty(localName = "FxRate")
@JacksonXmlElementWrapper(useWrapping = true)
private CurrencyBase currencyBase;
public class CurrencyBase {
@JacksonXmlProperty(localName = "Tp")
private String type;
@JacksonXmlProperty(localName = "Dt")
private String date;
@JacksonXmlProperty(localName = "CcyAmt")
@JacksonXmlElementWrapper(useWrapping = true)
private CurrencyAmount currencyAmount;
public class CurrencyAmount {
@JacksonXmlProperty(localName = "Ccy")
private String Name;
@JacksonXmlProperty(localName = "Amt")
private String ConvertionRate;
[1]: https://www.lb.lt/webservices/FxRates/FxRates.asmx?op=getCurrentFxRates
[2]: https://www.lb.lt/webservices/FxRates/FxRates.asmx/getCurrentFxRates?tp=eu
英文:
I want to get data from an API it gives me XML data like this
I can map the last of these to an object. See the code bellow:
public void getCurrencies() {
XmlMapper xmlMapper = new XmlMapper();
Currency currency = null;
try {
currency = xmlMapper.readValue(
new URL("https://www.lb.lt/webservices/FxRates/FxRates.asmx/getCurrentFxRates?tp=eu"),
Currency.class);
CurrencyBase c = currency.getCurrencyBase();
CurrencyAmount d = c.getCurrencyAmount();
System.out.println(d.getName());
} catch (Exception e) {
System.out.println(e);
}
}
But how do I parse the XML data to a list of objects?
My model for the currency:
@JacksonXmlRootElement(localName = "FxRates")
public class Currency {
@JacksonXmlProperty(localName = "FxRate")
@JacksonXmlElementWrapper(useWrapping = true)
private CurrencyBase currencyBase;
public class CurrencyBase {
@JacksonXmlProperty(localName = "Tp")
private String type;
@JacksonXmlProperty(localName = "Dt")
private String date;
@JacksonXmlProperty(localName = "CcyAmt")
@JacksonXmlElementWrapper(useWrapping = true)
private CurrencyAmount currencyAmount;
public class CurrencyAmount {
@JacksonXmlProperty(localName = "Ccy")
private String Name;
@JacksonXmlProperty(localName = "Amt")
private String ConvertionRate;
答案1
得分: 2
你需要使用 TypeReference 来映射对象的列表。你拥有的是 CurrencyBase 的对象列表,而不是 Currency 的对象列表。以下是使其正常工作的代码更改:
public void getCurrencies() {
XmlMapper xmlMapper = new XmlMapper();
List<CurrencyBase> currency = null;
try {
currency = xmlMapper.readValue(
new URL("https://www.lb.lt/webservices/FxRates/FxRates.asmx/getCurrentFxRates?tp=eu"),
new TypeReference<List<CurrencyBase>>(){});
System.out.println(currency);
} catch (Exception e) {
System.out.println(e);
}
}
还需要更改 @JacksonXmlElementWrapper(useWrapping = false)
,并将 CurrencyAmount
更改为 List<CurrencyAmount>
:
public class CurrencyBase {
@JacksonXmlProperty(localName = "Tp")
private String type;
@JacksonXmlProperty(localName = "Dt")
private String date;
@JacksonXmlProperty(localName = "CcyAmt")
@JacksonXmlElementWrapper(useWrapping = false)
private List<CurrencyAmount> currencyAmount;
}
英文:
You need use TypeReference to map the List of Objects. You have list of CurrencyBase rather than Currency itself. Here is the code changes to make it work:
public void getCurrencies() {
XmlMapper xmlMapper = new XmlMapper();
List<CurrencyBase> currency = null;
try {
currency = xmlMapper.readValue(
new URL("https://www.lb.lt/webservices/FxRates/FxRates.asmx/getCurrentFxRates?tp=eu"),
new TypeReference<List<CurrencyBase>>(){});
System.out.println(currency);
} catch (Exception e) {
System.out.println(e);
}
}
Also have to change @JacksonXmlElementWrapper(useWrapping = false) and change CurrencyAmount to List<CurrencyAmount> :
public class CurrencyBase {
@JacksonXmlProperty(localName = "Tp")
private String type;
@JacksonXmlProperty(localName = "Dt")
private String date;
@JacksonXmlProperty(localName = "CcyAmt")
@JacksonXmlElementWrapper(useWrapping = false)
private List<CurrencyAmount> currencyAmount;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论