Android - SimpleXML 框架无法解析@ElementList
Android - SimpleXML framework cannot parse @ElementList
我正在使用 SimpleXML 框架在我的 Android 应用程序中解析 xml。我无法正确解析 @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 为空。我在这里错过了什么?
我在阅读此处类似问题的答案时发现了问题所在:
https://sourceforge.net/p/simple/mailman/message/25699359/
我使用的是 name
而不是 entry
。所以 ElementList 属性应该如下所示:
@ElementList(entry= "PaymentTotals", inline = true, required = false)
protected List<PaymentTotalsType> paymentTotals;
现在完美运行了。
我正在使用 SimpleXML 框架在我的 Android 应用程序中解析 xml。我无法正确解析 @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 为空。我在这里错过了什么?
我在阅读此处类似问题的答案时发现了问题所在: https://sourceforge.net/p/simple/mailman/message/25699359/
我使用的是 name
而不是 entry
。所以 ElementList 属性应该如下所示:
@ElementList(entry= "PaymentTotals", inline = true, required = false)
protected List<PaymentTotalsType> paymentTotals;
现在完美运行了。