2017-01-26 3 views
1

Ich verwende SimpleXML-Framework, um Xmls in meiner Android-Anwendung zu analysieren. Ich habe ein Problem mit @ElementList korrekt geparst.Android - SimpleXML-Framework kann @ElementList nicht analysieren

Ein Fragment von 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> 

Meine Klassen aussehen:

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 
    } 

ich analysieren es mit Methode:

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; 
} 

Aber jedes Mal, wenn ich eine Ausnahme erhalten, das Element 'TransactionTotals' bestellt fehlt, auch wenn 1) ist es nicht 2 erforderlich ist) es

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

in der analysierten XML existiert Wenn ich die 'TransactionTotals' von @Order kommentiere, wird das XML ohne eine Ausnahme analysiert, aber die TransactionTotals, die in result abgelegt sind, sind leer. Was fehlt mir hier?

Antwort

0

Ich fand, was ein Problem war, während hier Antwort auf ein ähnliches Problem zu lesen: https://sourceforge.net/p/simple/mailman/message/25699359/

I name insted von entry benutzt. So sollte ein ElementList-Attribut wie folgt aussehen:

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

Jetzt funktioniert es perfekt.

Verwandte Themen