2014-03-26 91 views
0

我正在使用JAXB將java對象轉換爲xml和xml對象。 但是我得到了JAXBException,我該如何解決它?如何使用JAXB Marshall和Unmarshall多於一個POJO類

MainPojo:

@XmlRootElement(name = "PCM") 
@XmlAccessorType(XmlAccessType.FIELD) 
public class MBCASAAccountDetailsData { 

@XmlElementWrapper(name = "SvcRq") 
private ArrayList<MBCASAAccountData> casaAccountData; 

@XmlElementWrapper(name = "BalInqRq") 
@XmlElement(name = "DepAcctId") 
private ArrayList<MBTestAccountData> accountData; 

public MBCASAAccountDetailsData(){ 

} 


public ArrayList<MBCASAAccountData> getCasaAccountData() { 
    return casaAccountData; 
} 

public void setCasaAccountData(ArrayList<MBCASAAccountData> casaAccountData) { 
    this.casaAccountData = casaAccountData; 
} 


public ArrayList<MBTestAccountData> getAccountData() { 
    return accountData; 
} 

public void setAccountData(ArrayList<MBTestAccountData> accountData) { 
    this.accountData = accountData; 
} 
} 

SubPojo:

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlRootElement(namespace = "mbank.product.account.data.MBCASAAccountDetailsData") 
public class MBCASAAccountData { 

@XmlElement(name = "ChannelId") 
private String channelId; 
@XmlElement(name = "SvcCode") 
private String serviceCode; 
@XmlElement(name = "SvcRqId") 
private String serviceReqId; 
@XmlElement(name = "Timestamp") 
private String timeStamp; 
@XmlElement(name = "TimeoutPeriod") 
private int timeOutPeriod; 

public MBCASAAccountData(){ 

} 
public MBCASAAccountData(String channelId,String serviceCode,String serviceReqId,String timeStamp,int timeOutPeriod){ 
    super(); 
    this.channelId = channelId; 
    this.serviceCode = serviceCode; 
    this.serviceReqId = serviceReqId; 
    this.timeStamp = timeStamp; 
    this.timeOutPeriod = timeOutPeriod; 
} 


public String getChannelId() { 
    return channelId; 
} 
public void setChannelId(String channelId) { 
    this.channelId = channelId; 
} 


public String getServiceCode() { 
    return serviceCode; 
} 
public void setServiceCode(String serviceCode) { 
    this.serviceCode = serviceCode; 
} 


public String getServiceReqId() { 
    return serviceReqId; 
} 
public void setServiceReqId(String serviceReqId) { 
    this.serviceReqId = serviceReqId; 
} 


public String getTimeStamp() { 
    return timeStamp; 
} 
public void setTimeStamp(String timeStamp) { 
    this.timeStamp = timeStamp; 
} 


public int getTimeOutPeriod() { 
    return timeOutPeriod; 
} 
public void setTimeOutPeriod(int timeOutPeriod) { 
    this.timeOutPeriod = timeOutPeriod; 
} 
} 

二SubPojo:

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlRootElement(namespace = "mbank.product.account.data.MBCASAAccountDetailsData") 
public class MBTestAccountData extends BusinessData { 

@XmlElement(name = "AcctId") 
private String accountNo; 
@XmlElement(name = "AcctType") 
private String accountType; 

public static final String ACCOUNT_NUMBER = "accountNo"; 
public static final String ACCOUNT_TYPE = "accountType"; 

public MBTestAccountData(){ 

} 
public MBTestAccountData(String accountNo,String accountType){ 
    super(); 
    this.accountNo = accountNo; 
    this.accountType = accountType; 
} 


public String getAccountNo() { 
    return this.accountNo; 
} 

public void setAccountNo(String accountNo) { 
    set(ACCOUNT_NUMBER, accountNo); 
    this.accountNo = accountNo; 
} 


public String getAccountType() { 
    return this.accountType; 
} 

public void setAccountType(String accountType) { 
    set(ACCOUNT_TYPE, accountType); 
    this.accountType = accountType; 
} 
} 

主類(識別TestClass):

String customerID = "56789"; 

     MBCASAAccountData casaAccountData = new MBCASAAccountData(CommonConstants.CHANNEL_ID, AccountSummaryConstants.CASA_DETAILS_SERVICE_ID, MBankUtill.getServiceId(customerID.length())+customerID,DateUtil.getStringFromDate(new Date(), AccountSummaryConstants.DATE_FORMAT),AccountSummaryConstants.TIMEOUT_PERIOD_IN_SECONDS); 
ArrayList<MBCASAAccountData> test = new ArrayList<MBCASAAccountData>(); 
     test.add(casaAccountData); 

     MBTestAccountData accountData = new MBTestAccountData("123456","26"); 
ArrayList<MBTestAccountData> test2 = new ArrayList<MBTestAccountData>(); 
     test2.add(accountData); 

     MBCASAAccountDetailsData accountDetailsData = new MBCASAAccountDetailsData(); 
     accountDetailsData.setCasaAccountData(test); 
     accountDetailsData.setAccountData(test2); 

     try { 
      JAXBContext jaxbContext = JAXBContext.newInstance(mbank.product.account.data.MBCASAAccountDetailsData.class); 
      Marshaller marshaller = jaxbContext.createMarshaller(); 
      marshaller.marshal(jaxbContext, System.out); 
     } catch (JAXBException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

例外:

javax.xml.bind.JAXBException: class com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl nor any of its super class is known to this context. 
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getBeanInfo(Unknown Source) 
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(Unknown Source) 
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(Unknown Source) 
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(Unknown Source) 
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(Unknown Source) 
at mbank.accounts.testng.test.TestClass.main(TestClass.java:282) 

輸出:

<?xml version="1.0"?> 

<PCM> 

<SvcRq> 

<ChannelId>*RIB1</ChannelId> 

<SvcCode>AS_CSDETINQ_N</SvcCode> 

<SvcRqId>20140311522xxx</SvcRqId> 

<Timestamp>110314022518</Timestamp> 

<TimeoutPeriod>45</TimeoutPeriod> 

</SvcRq> 

<BalInqRq> 

<DepAcctId> 

    <AcctId>003573081xxx</AcctId> 

    <AcctType>26</AcctType> 

</DepAcctId> 

</BalInqRq> 

</PCM> 

嗨,我需要輸出格式的上方。我能做什麼? 請幫幫我。

在此先感謝。

+0

你能請註明你的代碼到底在哪被拋出的錯誤? – Arjit

回答

0

兩個提示:

JAXBContext.newInstance(MBCASAAccountDetailsData.class, 
    MBCASAAccountDetailsData.class) 

...

marshaller.marshal(casaAccountData, System.out); 
+0

謝謝你的回答。 –