2017-01-26 46 views
1

我正在使用SimpleXML框架来解析我的Android应用程序中的xml。我在正确解析@ElementList时遇到问题。Android - SimpleXML框架无法解析@ElementList

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> 

我的类看起来:

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 
    } 

我用它解析方法:

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

但每次我得到一个异常时,下令元素“TransactionTotals”缺少,即使1)不要求2)它在解析XML

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

存在当我从@Order评论'TransactionTotals'时,xml被解析而没有异常,但是结果中的TransactionTotals是空的。我在这里错过了什么?

回答

0

我发现在阅读的答案在这里类似的问题是什么是一个问题: https://sourceforge.net/p/simple/mailman/message/25699359/

我用的entryname insted的。因此ElementList属性应该看起来像这样:

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

现在它完美地工作。