simpleframework convert element

130 Views Asked by At

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 ?

1

There are 1 best solutions below

1
hradecek On BEST ANSWER

Though I have never used Simple XML serialization framework by myself. But looking on its API, you can traverse InputNode by yourself in Converter using InputNode#getNext() or InputNode#getNext(String).

For example:

public Record read(InputNode node) throws Exception {
    final Record record = new Record();
    record.setDate(node.getAttribute(DATE).getValue());
    record.setId(node.getAttribute(ID).getValue());

    // Default values
    record.setNominal(1);
    record.setValue(BigDecimal.ONE);

    for (InputNode next = node.getNext(); next != null; next = node.getNext()) {
        switch (next.getName()) {
            case NOMINAL:
                record.setNominal(Integer.parseInt(next.getValue()));
                break;
            case VALUE:
                record.setValue(new BigDecimal(next.getValue()));
                break;
        }
    }
    return record;
}

Note, that default values can be also set directly in the model Record class, like so:

Record.java

    ...
    @Element(name = NOMINAL)
    int nominal = 1;
    @Element(name = VALUE)
    BigDecimal value = BigDecimal.ONE;
    ...