简体   繁体   English

Android - SimpleXML框架无法解析@ElementList

[英]Android - SimpleXML framework cannot parse @ElementList

I am using SimpleXML framework to parse xmls in my Android application. 我正在使用SimpleXML框架来解析我的Android应用程序中的xmls。 I have a problem with getting @ElementList parsed correctly. 我有一个问题,正确解析@ElementList

A fragment of xml: 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: 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: 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 但每次我得到一个异常时,缺少有序元素'TransactionTotals',即使1)它不是必需的2)它确实存在于解析的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. 当我从@Order评论'TransactionTotals'时,xml被解析而没有异常,但结果中的TransactionTotals是空的。 What am I missing here? 我在这里错过了什么?

I found what was a problem while reading answer to a similar problem here: https://sourceforge.net/p/simple/mailman/message/25699359/ 我在这里阅读类似问题的答案时发现了什么问题: https//sourceforge.net/p/simple/mailman/message/25699359/

I was using name insted of entry . 我正在使用entry name So an ElementList attribute should look like this: 所以ElementList属性应如下所示:

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

Now it works perfectly. 现在它完美无缺。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM