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

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

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&lt;CurrencyBase&gt; currency = null;
try {
currency = xmlMapper.readValue(
new URL(&quot;https://www.lb.lt/webservices/FxRates/FxRates.asmx/getCurrentFxRates?tp=eu&quot;),
new TypeReference&lt;List&lt;CurrencyBase&gt;&gt;(){});
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 = &quot;Tp&quot;)
private String type;
@JacksonXmlProperty(localName = &quot;Dt&quot;)
private String date;
@JacksonXmlProperty(localName = &quot;CcyAmt&quot;)
@JacksonXmlElementWrapper(useWrapping = false)
private List&lt;CurrencyAmount&gt; currencyAmount;
}

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:

确定