简体   繁体   中英

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

@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.

  1. Your XML should be like :

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

    2.1 create class named "CC5Response.java" like :

     @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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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