I have the following XML structure:
<MNBCurrentExchangeRates> <Day date="2016-03-25"> <Rate unit="1" curr="AUD">211,37000</Rate> <Rate unit="1" curr="BGN">160,27000</Rate> </Day> </MNBCurrentExchangeRates> I have to generate XML strings just like this. To do so I specified the following class structure:
This is the root, called MNBCurrentExchangeRates
package Contracts.Mnb; import com.thoughtworks.xstream.annotations.*; import java.util.ArrayList; import java.util.List; @XStreamAlias("MNBCurrentExchangeRates") public class MNBCurrentExchangeRates { @XStreamAlias("Day") @XStreamImplicit protected List<Day> day; public List<Day> getDay() { if (day == null) { day = new ArrayList<Day>(); } return this.day; } } The Day Node:
package Contracts.Mnb; import com.thoughtworks.xstream.annotations.*; import java.util.ArrayList; import java.util.List; @XStreamAlias("Day") public class Day { @XStreamAlias("Rate") @XStreamImplicit protected List<Rate> rate; @XStreamAlias("date") @XStreamAsAttribute protected String date; public List<Rate> getRate() { if (rate == null) { rate = new ArrayList<Rate>(); } return this.rate; } public String getDate() { return date; } public void setDate(String value) { this.date = value; } } The Rate node:
package Contracts.Mnb; import com.thoughtworks.xstream.annotations.*; @XStreamAlias("Rate") public class Rate { protected String value; @XStreamAlias("unit") @XStreamAsAttribute protected String unit; @XStreamAlias("curr") @XStreamAsAttribute protected String curr; public String getValue() { return value; } public void setValue(String value) { this.value = value; } public String getUnit() { return unit; } public void setUnit(String value) { this.unit = value; } public String getCurr() { return curr; } public void setCurr(String value) { this.curr = value; } } For serialization I use the following code:
XStream stream = new XStream(); stream.processAnnotations(MNBCurrentExchangeRates.class); String rates = stream.toXML(exchangeRates); Where exchangeRates is an instance of the MNBCurrentExchangeRates class.
Serializing it returns the following XML string:
<MNBCurrentExchangeRates> <Day date="2016-03-25"> <Rate unit="1" curr="EUR"> <value>300</value> </Rate> </Day> </MNBCurrentExchangeRates> I don't need the value node. I want the actual Rate to be the value of the Rate node like in the example. How can I achieve this?
Thank you.
No comments:
Post a Comment