I have next class
@Root
@Convert(RecordConverter.class)
public class Record {
public static final String DATE = "Date";
public static final String ID = "Id";
public static final String NOMINAL = "Nominal";
public static final String VALUE = "Value";
@Attribute(name = DATE)
String date;
@Attribute(name = ID)
String id;
@Element(name = NOMINAL)
int nominal;
@Element(name = VALUE)
String value;
}
And I want to use converter for my 'Value', because it comes like String but I need it as BigDecimal. But @Converter or 'Converter' doesn`t applys to @Element, only for @Attributes.
Example of Record XML
<Record Date="05.03.2020" Id="R01235">
<Nominal>1</Nominal>
<Value>66,0784</Value>
</Record>
RecordConverter class is
public class RecordConverter implements Converter<Record> {
Record record;
public Record read(InputNode node) throws Exception {
record = new Record();
record.setDate(node.getAttribute(DATE).getValue());
record.setId(node.getAttribute(ID).getValue());
if (node.getAttribute(NOMINAL) != null) {
record.setNominal(Integer.parseInt(node.getAttribute(NOMINAL).getValue()));
} else {
record.setNominal(1);
}
if (node.getAttribute(VALUE) != null) {
record.setValue(node.getAttribute(VALUE).getValue());
} else {
record.setValue("qw");
}
return record;
}
public void write(OutputNode node, Record value) throws Exception {
throw new UnsupportedOperationException("Not ready converter yet");
}
}
But, I can not get any 'Nominal' or 'Value' here.
My question is How can I convert my Value from String 66,0784 to BigDecimal 66.0784 ?
Though I have never used Simple XML serialization framework by myself. But looking on its API, you can traverse
InputNodeby yourself inConverterusingInputNode#getNext()orInputNode#getNext(String).For example:
Note, that default values can be also set directly in the model
Recordclass, like so:Record.java