2010-06-03 180 views
0

嗨,我是很新的XML,Web服務和但我從SOAP Web服務,看起來有點像這樣recieving XML:解組對象的數組和數組

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

我用Netbeans的創建結合JAXB這似乎工作

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); 
    } 

結合被生成,具有「ArrayOfCreditCardSettlement」,「CreditCardSettlement」和ObjectFactory的等。

現在,我可以在這裏做的唯一事情就是投向JAXBElement

即JAXBElement a =(JAXBElement)unmarshaller.unmarshal(new File(「... response3.xml」));

也許這就是我應該有的,但從那我該怎麼用這個?

我想要那個數組/列表使用,但我不知道下一步該怎麼做。

AdditionalInfo: 結合從一個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; 
} 

爲CreditCardSettlement類相似生成。

對象工廠擁有XmlElementDecl將的明確命名的命名空間等來回

回答

1
  1. 我敢肯定,你不能有一個集合作爲頂級對象。
  2. 您是否設置了POJO和ObjectFactory?您應該能夠直接解組到您的目標對象。例如

    CreditCardSettlementContainer ccsc = (CreditCardSettlementContainer) unmarshaller.unmarshal(yourFile); 
    

參考:JAXB tutorial

上,如果你是剛剛開始,我強烈建議Simple XML這是更容易理解和使用,更不要說被更靈活另一方面。

+0

感謝你的鏈接,我試圖直接解組爲目標對象,但無論出於何種原因沒有奏效。 我沒有奢侈的選擇SimpleXML,但感謝您的回覆 – fogedi 2010-06-03 12:43:38