Unmarshalling an array of objects and arrays
Hi I'm very new to XML, and webservices but I'm recieving XML from a SOAP webservice that looks kind of like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ArrayOfCreditCardSettlement xmlns="http://schemas.datacontract.org/2004/07/Borgun.Library.Common">
<ns0:CreditCardSettlement xmlns="http://Borgun.Services.Gateway/2010/04/Settlement" xmlns:a="http://schemas.datacontract.org/2004/07/Borgun.Library.Common" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:msgns="http://Borgun.Services.Gateway/2010/04/Settlement" xmlns:ns0="http://schemas.datacontract.org/2004/07/Borgun.Library.Common" xmlns:ns1="http://j2ee.netbeans.org/wsdl/BorgunTestBPEL/entrypoint_getSettlements">
<a:amexAmount>**</a:amexAmount>
<a:amount>**</a:amount>
<a:batches>
<a:CreditCardBatch>
<a:batchdate>***</a:batchdate>
<a:batchnumber>***</a:batchnumber>
<a:currencyCode>***</a:currencyCode>
<a:merchantnumber>***</a:merchantnumber>
<a:settlementRunNumber>***</a:settlementRunNumber>
<a:settlementdate>***</a:settlementdate>
<a:slips>*</a:slips>
<a:sum>***</a:sum>
</a:CreditCardBatch>
.
.
more batches
<a:deductionItems>
<a:CreditCardSettlementDeduction>
<a:amount>***</a:amount>
<a:code>**</a:code>
<a:currencyCode>**</a:currencyCode>
<a:merchantnumber>***</a:merchantnumber>
<a:settlementrunnumber>***</a:settlementrunnumber>
<a:text>***</a:text>
</a:CreditCardSettlementDeduction>
.
. more deductionitems
</ns0:CreditCardSettlement>
.
. more Settlements
I used Netbeans to create a jaxb binding which seems to work
try
{
JAXBContext jc = JAXBContext.newInstance("is.skyrr.jaxbbinding");
Unmarshaller unmarshaller = jc.createUnmarshaller();
Object o = (Object) unmarshaller.unmarshal(new File("....response3.xml"));
} catch (JAXBException ex)
{
Logger.getLogger(TestDB.class.getName()).log(Level.SEVERE, null, ex);
}
The bindin开发者_开发知识库g is generated and has "ArrayOfCreditCardSettlement", "CreditCardSettlement" and objectfactory etc..
Now the only thing i can do here is cast to JAXBElement
that is ` JAXBElement a = (JAXBElement) unmarshaller.unmarshal(new File("...response3.xml"));
Maybe that's what i'm supposed to have but from that how am i supposed to use this?
I want that array/list to use but I've just got no idea what to do next.
AdditionalInfo: the binding is generated from an xsd
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ArrayOfCreditCardSettlement", propOrder = {
"creditCardSettlement"})
public class ArrayOfCreditCardSettlement {
@XmlElement(name = "CreditCardSettlement", nillable = true)
protected List<CreditCardSettlement> creditCardSettlement;
public List<CreditCardSettlement> getCreditCardSettlement() {
if (creditCardSettlement == null) {
creditCardSettlement = new ArrayList<CreditCardSettlement>();
}
return this.creditCardSettlement;
}
similar for the CreditCardSettlement class.
the object factory has XMLElementDecl's explicitly naming the namespace and so on and forth
- I'm pretty sure you can't have a Collection as the top level object.
Have you set up your POJOs and your ObjectFactory? You should be able to unmarshal directly into your target object. e.g.
CreditCardSettlementContainer ccsc = (CreditCardSettlementContainer) unmarshaller.unmarshal(yourFile);
Reference: JAXB tutorial
On the other hand if you're just getting started I highly recommend Simple XML It's much easier to understand and use, not to mention being more flexible.
The JAXBElement has a value and that should be an ArrayOfCreditCardSettlement
object.
Here is a code sample that could give some hints: http://www.java2s.com/Code/Java/XML/UnmarshalltoJAXBElement.htm
精彩评论