解组对象和数组的数组

时间:2010-06-03 11:57:03

标签: java xml-serialization jaxb

您好我是XML和Web服务的新手,但我从SOAP Web服务中收到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显式命名命名空间等等

2 个答案:

答案 0 :(得分:1)

  1. 我很确定你不能将Collection作为顶级对象。
  2. 您是否设置了POJO和ObjectFactory?您应该能够直接解组到目标对象中。 e.g。

    CreditCardSettlementContainer ccsc = (CreditCardSettlementContainer) unmarshaller.unmarshal(yourFile);
    
  3. 参考:JAXB tutorial

    另一方面,如果您刚开始使用,我强烈建议Simple XML 更容易理解和使用,更不用说更灵活了

答案 1 :(得分:0)

JAXBElement有一个值,应该是ArrayOfCreditCardSettlement对象。

这是一个可以提供一些提示的代码示例: http://www.java2s.com/Code/Java/XML/UnmarshalltoJAXBElement.htm