如何使用Java Spring从XML创建对象列表?

huangapple go评论95阅读模式
英文:

How to make an object list from XML with Java Spring?

问题

  1. 我想从一个[API][1]中获取数据它会给我XML格式的数据类似于[这样][2]
  2. 我可以将最后一个映射到一个对象请参见下面的代码
  3. public void getCurrencies() {
  4. XmlMapper xmlMapper = new XmlMapper();
  5. Currency currency = null;
  6. try {
  7. currency = xmlMapper.readValue(
  8. new URL("https://www.lb.lt/webservices/FxRates/FxRates.asmx/getCurrentFxRates?tp=eu"),
  9. Currency.class);
  10. CurrencyBase c = currency.getCurrencyBase();
  11. CurrencyAmount d = c.getCurrencyAmount();
  12. System.out.println(d.getName());
  13. } catch (Exception e) {
  14. System.out.println(e);
  15. }
  16. }
  17. 但是如何将XML数据解析为对象列表呢
  18. 我的货币模型
  19. @JacksonXmlRootElement(localName = "FxRates")
  20. public class Currency {
  21. @JacksonXmlProperty(localName = "FxRate")
  22. @JacksonXmlElementWrapper(useWrapping = true)
  23. private CurrencyBase currencyBase;
  24. public class CurrencyBase {
  25. @JacksonXmlProperty(localName = "Tp")
  26. private String type;
  27. @JacksonXmlProperty(localName = "Dt")
  28. private String date;
  29. @JacksonXmlProperty(localName = "CcyAmt")
  30. @JacksonXmlElementWrapper(useWrapping = true)
  31. private CurrencyAmount currencyAmount;
  32. public class CurrencyAmount {
  33. @JacksonXmlProperty(localName = "Ccy")
  34. private String Name;
  35. @JacksonXmlProperty(localName = "Amt")
  36. private String ConvertionRate;
  37. [1]: https://www.lb.lt/webservices/FxRates/FxRates.asmx?op=getCurrentFxRates
  38. [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:

  1. public void getCurrencies() {
  2. XmlMapper xmlMapper = new XmlMapper();
  3. Currency currency = null;
  4. try {
  5. currency = xmlMapper.readValue(
  6. new URL("https://www.lb.lt/webservices/FxRates/FxRates.asmx/getCurrentFxRates?tp=eu"),
  7. Currency.class);
  8. CurrencyBase c = currency.getCurrencyBase();
  9. CurrencyAmount d = c.getCurrencyAmount();
  10. System.out.println(d.getName());
  11. } catch (Exception e) {
  12. System.out.println(e);
  13. }
  14. }

But how do I parse the XML data to a list of objects?

My model for the currency:

  1. @JacksonXmlRootElement(localName = "FxRates")
  2. public class Currency {
  3. @JacksonXmlProperty(localName = "FxRate")
  4. @JacksonXmlElementWrapper(useWrapping = true)
  5. private CurrencyBase currencyBase;
  6. public class CurrencyBase {
  7. @JacksonXmlProperty(localName = "Tp")
  8. private String type;
  9. @JacksonXmlProperty(localName = "Dt")
  10. private String date;
  11. @JacksonXmlProperty(localName = "CcyAmt")
  12. @JacksonXmlElementWrapper(useWrapping = true)
  13. private CurrencyAmount currencyAmount;
  14. public class CurrencyAmount {
  15. @JacksonXmlProperty(localName = "Ccy")
  16. private String Name;
  17. @JacksonXmlProperty(localName = "Amt")
  18. private String ConvertionRate;

答案1

得分: 2

你需要使用 TypeReference 来映射对象的列表。你拥有的是 CurrencyBase 的对象列表,而不是 Currency 的对象列表。以下是使其正常工作的代码更改:

  1. public void getCurrencies() {
  2. XmlMapper xmlMapper = new XmlMapper();
  3. List<CurrencyBase> currency = null;
  4. try {
  5. currency = xmlMapper.readValue(
  6. new URL("https://www.lb.lt/webservices/FxRates/FxRates.asmx/getCurrentFxRates?tp=eu"),
  7. new TypeReference<List<CurrencyBase>>(){});
  8. System.out.println(currency);
  9. } catch (Exception e) {
  10. System.out.println(e);
  11. }
  12. }

还需要更改 @JacksonXmlElementWrapper(useWrapping = false),并将 CurrencyAmount 更改为 List<CurrencyAmount>

  1. public class CurrencyBase {
  2. @JacksonXmlProperty(localName = "Tp")
  3. private String type;
  4. @JacksonXmlProperty(localName = "Dt")
  5. private String date;
  6. @JacksonXmlProperty(localName = "CcyAmt")
  7. @JacksonXmlElementWrapper(useWrapping = false)
  8. private List<CurrencyAmount> currencyAmount;
  9. }
英文:

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:

  1. public void getCurrencies() {
  2. XmlMapper xmlMapper = new XmlMapper();
  3. List&lt;CurrencyBase&gt; currency = null;
  4. try {
  5. currency = xmlMapper.readValue(
  6. new URL(&quot;https://www.lb.lt/webservices/FxRates/FxRates.asmx/getCurrentFxRates?tp=eu&quot;),
  7. new TypeReference&lt;List&lt;CurrencyBase&gt;&gt;(){});
  8. System.out.println(currency);
  9. } catch (Exception e) {
  10. System.out.println(e);
  11. }
  12. }

Also have to change @JacksonXmlElementWrapper(useWrapping = false) and change CurrencyAmount to List<CurrencyAmount> :

  1. public class CurrencyBase {
  2. @JacksonXmlProperty(localName = &quot;Tp&quot;)
  3. private String type;
  4. @JacksonXmlProperty(localName = &quot;Dt&quot;)
  5. private String date;
  6. @JacksonXmlProperty(localName = &quot;CcyAmt&quot;)
  7. @JacksonXmlElementWrapper(useWrapping = false)
  8. private List&lt;CurrencyAmount&gt; currencyAmount;
  9. }

huangapple
  • 本文由 发表于 2020年8月22日 22:20:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63537190.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定