当我尝试解析下面的字符串
时出现以下错误<?xml version="1.0" encoding="UTF-8"?>
<CC5Response>
<OrderId>ORDER-17305KXSH11966</OrderId>
<GroupId>ORDER-173053333KXSH11966</GroupId>
<Response>Approved</Response>
<AuthCode>0293333584</AuthCode>
<HostRefNum>73051033333011833</HostRefNum>
<ProcReturnCode>00</ProcReturnCode>
<TransId>17305K33245XSH11968</TransId>
<ErrMsg></ErrMsg>
<Extra>
<SETTLEID>1</SETTLEID>
<TRXDATE>20171101 10:23:18</TRXDATE>
<ERRORCODE></ERRORCODE>
<CARDBRAND>VISA</CARDBRAND>
<CARDISSUER>CDM</CARDISSUER>
<NUMCODE>00</NUMCODE>
</Extra>
</CC5Response>
Unmarshalling的代码片段如下所示
JAXBContext jaxbContext = JAXBContext.newInstance(CC5Response.class);
XMLInputFactory xif = XMLInputFactory.newFactory();
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
XMLStreamReader xsr = xif.createXMLStreamReader(IOUtils.toInputStream(sb.toString(), "UTF-8"));
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
CC5Response res = (CC5Response) jaxbUnmarshaller.unmarshal(xsr);
System.out.println("*************"+res.toString());
下面给出了bean类
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType
@XmlRootElement(name = "CC5Response")
public class CC5Response {
@XmlAttribute
private String ProcReturnCode;
// @XmlAttribute
// private Extra Extra;
/*public Extra getExtra() {
return Extra;
}
public void setExtra(Extra extra) {
Extra = extra;
}*/
@XmlAttribute
private String AuthCode;
@XmlAttribute
private String OrderId;
@XmlAttribute
private String TransId;
@XmlAttribute
private String ErrMsg;
@XmlAttribute
private String Response;
@XmlAttribute
private String HostRefNum;
@XmlAttribute
private String GroupId;
我总是得到一个空值的普通对象。
答案 0 :(得分:1)
要使JAXB unmarshaller正常工作,您应该将XML模拟到JAVA类的正确结构中......按照以下步骤操作,它将与您合作。
您的XML应该是:
<?xml version="1.0" encoding="UTF-8"?>
<CC5Response>
<OrderId>ORDER-17305KXSH11966</OrderId>
<GroupId>ORDER-173053333KXSH11966</GroupId>
<Response>Approved</Response>
<AuthCode>0293333584</AuthCode>
<HostRefNum>73051033333011833</HostRefNum>
<ProcReturnCode>00</ProcReturnCode>
<TransId>17305K33245XSH11968</TransId>
<ErrMsg>error message</ErrMsg>
<Extra>
<SETTLEID>1</SETTLEID>
<TRXDATE>20171101 10:23:18</TRXDATE>
<ERRORCODE>0000</ERRORCODE>
<CARDBRAND>VISA</CARDBRAND>
<CARDISSUER>CDM</CARDISSUER>
<NUMCODE>00</NUMCODE>
</Extra>
</CC5Response>
我们将在Java中创建2个必要的类:
2.1创建名为&#34; CC5Response.java&#34;喜欢:@XmlRootElement(name = "CC5Response")
public class CC5Response {
private String ProcReturnCode;
private String AuthCode;
private String OrderId;
private String TransId;
private String ErrMsg;
private String Response;
private String HostRefNum;
private String GroupId;
private List<Extra> Extra;
/**
* @return the ProcReturnCode
*/
@XmlElement(name="ProcReturnCode")
public String getProcReturnCode() {
return ProcReturnCode;
}
/**
* @param ProcReturnCode the ProcReturnCode to set
*/
public void setProcReturnCode(String ProcReturnCode) {
this.ProcReturnCode = ProcReturnCode;
}
/**
* @return the AuthCode
*/
@XmlElement(name="AuthCode")
public String getAuthCode() {
return AuthCode;
}
/**
* @param AuthCode the AuthCode to set
*/
public void setAuthCode(String AuthCode) {
this.AuthCode = AuthCode;
}
/**
* @return the OrderId
*/
@XmlElement(name="OrderId")
public String getOrderId() {
return OrderId;
}
/**
* @param OrderId the OrderId to set
*/
public void setOrderId(String OrderId) {
this.OrderId = OrderId;
}
/**
* @return the TransId
*/
@XmlElement(name="TransId")
public String getTransId() {
return TransId;
}
/**
* @param TransId the TransId to set
*/
public void setTransId(String TransId) {
this.TransId = TransId;
}
/**
* @return the ErrMsg
*/
@XmlElement(name="ErrMsg")
public String getErrMsg() {
return ErrMsg;
}
/**
* @param ErrMsg the ErrMsg to set
*/
public void setErrMsg(String ErrMsg) {
this.ErrMsg = ErrMsg;
}
/**
* @return the Response
*/
@XmlElement(name="Response")
public String getResponse() {
return Response;
}
/**
* @param Response the Response to set
*/
public void setResponse(String Response) {
this.Response = Response;
}
/**
* @return the HostRefNum
*/
@XmlElement(name="HostRefNum")
public String getHostRefNum() {
return HostRefNum;
}
/**
* @param HostRefNum the HostRefNum to set
*/
public void setHostRefNum(String HostRefNum) {
this.HostRefNum = HostRefNum;
}
/**
* @return the GroupId
*/
@XmlElement(name="GroupId")
public String getGroupId() {
return GroupId;
}
/**
* @param GroupId the GroupId to set
*/
public void setGroupId(String GroupId) {
this.GroupId = GroupId;
}
/**
* @return the Extra
*/
@XmlElement(name="Extra")
public List<Extra> getExtra() {
return Extra;
}
/**
* @param Extra the Extra to set
*/
public void setExtra(List<Extra> Extra) {
this.Extra = Extra;
}
}
2.2创建名为&#34; Extra.java&#34;喜欢:
public class Extra {
private String SETTLEID;
private String TRXDATE;
private String ERRORCODE;
private String CARDBRAND;
private String CARDISSUER;
private String NUMCODE;
/**
* @return the SETTLEID
*/
public String getSETTLEID() {
return SETTLEID;
}
/**
* @param SETTLEID the SETTLEID to set
*/
public void setSETTLEID(String SETTLEID) {
this.SETTLEID = SETTLEID;
}
/**
* @return the TRXDATE
*/
public String getTRXDATE() {
return TRXDATE;
}
/**
* @param TRXDATE the TRXDATE to set
*/
public void setTRXDATE(String TRXDATE) {
this.TRXDATE = TRXDATE;
}
/**
* @return the ERRORCODE
*/
public String getERRORCODE() {
return ERRORCODE;
}
/**
* @param ERRORCODE the ERRORCODE to set
*/
public void setERRORCODE(String ERRORCODE) {
this.ERRORCODE = ERRORCODE;
}
/**
* @return the CARDBRAND
*/
public String getCARDBRAND() {
return CARDBRAND;
}
/**
* @param CARDBRAND the CARDBRAND to set
*/
public void setCARDBRAND(String CARDBRAND) {
this.CARDBRAND = CARDBRAND;
}
/**
* @return the CARDISSUER
*/
public String getCARDISSUER() {
return CARDISSUER;
}
/**
* @param CARDISSUER the CARDISSUER to set
*/
public void setCARDISSUER(String CARDISSUER) {
this.CARDISSUER = CARDISSUER;
}
/**
* @return the NUMCODE
*/
public String getNUMCODE() {
return NUMCODE;
}
/**
* @param NUMCODE the NUMCODE to set
*/
public void setNUMCODE(String NUMCODE) {
this.NUMCODE = NUMCODE;
}
}
主要方法应该是:
/**
* @param args the command line arguments
* @throws javax.xml.bind.JAXBException
*/
public static void main(String[] args) throws JAXBException {
// TODO code application logic here
try {
File file = new File("file.xml");
if (file.exists()) {
JAXBContext jaxbContext = JAXBContext.newInstance(CC5Response.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
CC5Response response = (CC5Response) jaxbUnmarshaller.unmarshal(file);
if (response != null) {
System.out.println("*************" + response.getAuthCode());
System.out.println("*************" + response.getErrMsg());
System.out.println("*************" + response.getGroupId());
System.out.println("*************" + response.getOrderId());
System.out.println("*************" + response.getResponse());
//you can get any field from Exta class
System.out.println("*************" + response.getExtra());
}
}
} catch (JAXBException e) {
e.printStackTrace();
}
}