-
-
Notifications
You must be signed in to change notification settings - Fork 231
Closed
Milestone
Description
- I have the following xml to unmarshal
<?xml version="1.0" encoding="utf-8"?>
<customer xmlns="http://www.archer-tech.com/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<customerId>1</customerId>
<customerName>Michael Judy</customerName>
<account>
<accountId>100</accountId>
<accountName>Michael</accountName>
<postcode xsi:nil="true"></postcode>
</account>
<account>
<accountId>200</accountId>
<accountName>Judy</accountName>
<postcode xsi:nil="true"></postcode>
</account>
</customer>
- domains
@JacksonXmlRootElement(localName = "customer")
public class CustomerWithoutWrapper {
Long customerId;
String customerName;
@JacksonXmlElementWrapper(useWrapping = false)
List<Account> account = new ArrayList<>();
...setter/getter...
}
public class Account {
Long accountId;
String accountName;
String postcode;
...setter/getter...
}
- unmarshal process
ObjectMapper objectMapper = new XmlMapper();
CustomerWithoutWrapper customerWithOutWrapper =
objectMapper.readValue(getCustomerWithoutWrapperXml(), CustomerWithoutWrapper.class);
- result
there is not exception, however only the last account item is kept. the first account item is missing from account collection after unmarshalling
incorrect result
CustomerWithoutWrapper{customerId=1, customerName='Michael Judy', account=[Account{accountId=200, accountName='Judy', postcode=''}]}
- however when account items are wrapped as following, and change the domain mapping accordingly, then Jackson can unmarshall xml correctly.
e.g.
<?xml version="1.0" encoding="utf-8"?>
<customer xmlns="http://www.archer-tech.com/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<customerId>1</customerId>
<customerName>Michael Judy</customerName>
<accounts>
<account>
<accountId>100</accountId>
<accountName>Michael</accountName>
<postcode xsi:nil="true"></postcode>
</account>
<account>
<accountId>200</accountId>
<accountName>Judy</accountName>
<postcode xsi:nil="true"></postcode>
</account>
</accounts>
</customer>
@JacksonXmlRootElement(localName = "customer")
public class CustomerWithWrapper {
Long customerId;
String customerName;
@JacksonXmlElementWrapper(localName = "accounts")
List<Account> account = new ArrayList<>();
...
}
result - correct when accounts are wrapped.
CustomerWithWrapper{customerId=1, customerName='Michael Judy', account=[Account{accountId=100, accountName='Michael', postcode=''}, Account{accountId=200, accountName='Judy', postcode=''}]}
Metadata
Metadata
Assignees
Labels
No labels