我使用JAXB将java对象转换为xml,将xml转换为对象。 但我得到了JAXBException,我该如何解决呢?
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 :(得分:0)
两个提示:
JAXBContext.newInstance(MBCASAAccountDetailsData.class,
MBCASAAccountDetailsData.class)
...
marshaller.marshal(casaAccountData, System.out);