Search code examples
javaandroidxmlxml-parsingsimple-framework

Android - SimpleXML framework cannot parse @ElementList


I am using SimpleXML framework to parse xmls in my Android application. I have a problem with getting @ElementList parsed correctly.

A fragment of xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<SaleToPOIResponse>
    <MessageHeader (...) />
    <ReconciliationResponse  ReconciliationType="SaleReconciliation">
        <Response Result="Success"/>
        <TransactionTotals PaymentInstrumentType="Card">
            <PaymentTotals TransactionType="Debit" TransactionCount="182" TransactionAmount="4.17"/>
            <PaymentTotals TransactionType="Credit" TransactionCount="1" TransactionAmount="2.01"/>
        </TransactionTotals>
    </ReconciliationResponse>
</SaleToPOIResponse>

My classes look:

ReconciliationResponseType.java:

 @Root
    @Order(elements = {
            "Response",
            "TransactionTotals"
    })
    public class ReconciliationResponseType {

        @Element(name = "Response", required = true)
        protected ResponseType response;
        @ElementList(name = "TransactionTotals", inline = true, required = false)
        protected List<TransactionTotalsType> transactionTotals;
        @Attribute(name = "ReconciliationType", required = true)
        protected String reconciliationType;

    // getters and setters
    }

TransactionTotalsType.java:

  @Root
    @Order(elements = {
           "PaymentTotals",
           "LoyaltyTotals"
    })
    public class TransactionTotalsType {

        @ElementList(name = "PaymentTotals", inline = true, required = false)
        protected List<PaymentTotalsType> paymentTotals;
        @Attribute(name = "PaymentInstrumentType", required = true)
        protected String paymentInstrumentType;

    // getters and setters
    }

I parse it using method:

public static SaleToPOIResponse fromXMLString(String xmlResponse) {
    Reader reader = new StringReader(xmlResponse);

    Serializer serializer = new Persister();
    try {
        SaleToPOIResponse response = serializer.read(SaleToPOIResponse.class, reader, false);
        return response;
    } catch (Exception e) {
        Log.e(TAG, "Exception during parsing String XML to SaleToPOIResponse: ", e);
    }
    return null;
}

But every time I get an exception, that ordered element 'TransactionTotals' is missing, even though 1) it is not required 2) it does exist in the parsed xml

org.simpleframework.xml.core.ElementException: Ordered element 'TransactionTotals' missing for class pl.novelpay.epas.generated.saletopoimessages.ReconciliationResponseType

When I comment the 'TransactionTotals' from @Order the xml is parsed without an exception, but the TransactionTotals filed in result is empty. What am I missing here?


Solution

  • I found what was a problem while reading answer to a similar problem here: https://sourceforge.net/p/simple/mailman/message/25699359/

    I was using name insted of entry. So an ElementList attribute should look like this:

     @ElementList(entry= "PaymentTotals", inline = true, required = false)
     protected List<PaymentTotalsType> paymentTotals;
    

    Now it works perfectly.