Search code examples
javaxmldeserializationxstream

Repeating element deserialize using XStream


I am using XStream 1.4.3 to move between Java and XML documents when messaging to another process. Most everything works. However, I can't seem to get one reply document to deserialize properly. Here is the reply:

<AddToBatchResponse>
    <MerchantOrderNumber>1525675</MerchantOrderNumber>
    <MerchantOrderNumber>1525676</MerchantOrderNumber>
    <ResponseReasonCode>100</ResponseReasonCode>
    <AuthResponseType>S</AuthResponseType>
</AddToBatchResponse>

When XStream gets to the 2nd MerchantOrderNumber, it gives an error saying "Duplicate field MerchantOrderNumber". I've tried different designs, but it just won't work. Here is the relevant Java code:

Snippet from calling class

xstream.alias("AddToBatchResponse", AddToBatchResponse.class);
xstream.alias("MerchantOrderNumber", OrderNumber.class);
xstream.addImplicitCollection(AddToBatchResponse.class, "orderNumbers");
response = (AddToBatchResponse)xstream.fromXML(responseXml);

AddToBatchResponse.java (leaving out getters and setters)

public class AddToBatchResponse {
protected List<OrderNumber> orderNumbers;
protected String ResponseReasonCode; 
protected String AuthResponseType;  
    ...

OrderNumber.java

public class OrderNumber {
protected String MerchantOrderNumber;
...

Can someone tell me what I'm doing wrong? Thanks.


Solution

  • Well, no answer - so here's what I did:

    I used regex + String manipulation to pull out and process the extra elements in my code. Not elegant not desirable, but it works. If anyone ever finds an answer to this question, please share.