gpt4 book ai didi

java - Android - SimpleXML 框架无法解析@ElementList

转载 作者:太空宇宙 更新时间:2023-11-04 11:50:38 24 4
gpt4 key购买 nike

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

XML fragment :

<?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>

我的类(class)看起来:

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 为空。我在这里缺少什么?

最佳答案

我在阅读类似问题的答案时发现了问题所在: https://sourceforge.net/p/simple/mailman/message/25699359/

我使用的是name而不是entry。因此 ElementList 属性应如下所示:

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

现在可以完美运行了。

关于java - Android - SimpleXML 框架无法解析@ElementList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41871684/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com