简体   繁体   English

JAXB XML Unmarshaller错误。 获取所有对象的空值

[英]JAXB XML Unmarshaller error. Getting null value for all Object

I get the following error when I tried to parse the below string 尝试解析以下字符串时出现以下错误

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

Code snippet for Unmarshalling is given below 下面给出了反编组的代码片段

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 class is given below 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;

I always get a plain object with null values. 我总是得到一个带有空值的普通对象。

To enable JAXB unmarshaller to work fine, you should simulate your XML into right structure of JAVA classes ... Follow the following steps and it will work with you. 为了使JAXB解组器正常工作,您应该将XML模拟为正确的JAVA类结构...请按照以下步骤进行操作,它将与您一起工作。

  1. Your XML should be like : 您的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> 
  2. We will create 2 necessary classes in Java : 我们将使用Java创建2个必需的类:

    2.1 create class named "CC5Response.java" like : 2.1创建名为“ CC5Response.java”的类,如下所示:

     @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 create class named "Extra.java" like : 2.2创建名为“ Extra.java”的类,例如:

      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; } } 
  3. Main Method should be like : 主要方法应为:

      /** * @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(); } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM