Anzar
Anzar

Reputation: 151

JAXB Unmarshalling with Groovy

I am trying to create model class for below xml:

<Response>
    <Success>N</Success>
    <Errors>
        <Error>
            <Number>29002</Number>
            <Message>A key field was missing from the control xml</Message>
        </Error>
        <Error>
            <Number>29004</Number>
            <Message>Unable to accept messages at this time</Message>
        </Error>
    </Errors>
</Response>

This is my Response.class

@XmlRootElement (name="Response")
@XmlAccessorType( XmlAccessType.FIELD )
class Response {

  @XmlElement(name="Success")
  private String success

  @XmlElement(name="Errors")
  private Errors errors

  public String getSuccess() {
    return success
  }

  public Errors getErrors() {
    return errors;
  }
}

This is my Errors.class

@XmlRootElement(name="Errors")
@XmlAccessorType(XmlAccessType.FIELD)
class Errors {

  public Errors() {
    errorList = new ArrayList<Error>()
  }

  @XmlElement(name = "Errors")
  private List<Error> errorList;

  public List<Error> getErrorList() {
    return errorList
  }
}

And this is my Error.class

@XmlRootElement(name="Error")
@XmlAccessorType(XmlAccessType.FIELD)
class Error {

    @XmlElement(name="Number")
    private int number

    @XmlElement(name="Message")
    private String message


    public int getNumber() {
      return number
    }

    public String getMessage() {
      return message
    }
}

Below is my unmarshall class UnmarshallResponse.class

try {
//XML and Java binding
  JAXBContext jaxbContext = JAXBContext.newInstance(Response.class)

  //class responsible for the process of de-serializing
  //XML data into Java object
  Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
  Source source = new StreamSource(new java.io.StringReader(myXml))
  //log.info("source: "+myXml.toString())

  Response response = (Response) jaxbUnmarshaller.unmarshal(source)

  //print the response for debugging
  log.info("Success: " + response.getSuccess())

  Errors errors = response.getErrors()
  List<Error> errorList = errors.getErrorList()
  log.info("Size of Error List: " + errorList.size())
  errorList.each {
    Error element
    log.info("errorNumber: " + element.getNumber())
    log.info("errorMessage: " + element.getMessage())
  }
  log.info("End of XML.")
}catch (JAXBException e) {
  log.error("JAXBException: "+e.getMessage())
}

I am able to get the value of Success but Error list is not coming, it is showing error list size 0. The output is coming as below:

Success: N Size of Error List: 0

Can anybody help me to understand, where I am missing?

Thanks

Upvotes: 2

Views: 2011

Answers (2)

lexicore
lexicore

Reputation: 43671

You have named your Error elements as Errors (mind the s at the end). See the Errors.errorList. Therefore JAXB does not process your Error elements.

By the way, you do not necessarily need the Errors class. You can use @XmlElementWrapper(name="Errors") instead.

Upvotes: 5

Anzar
Anzar

Reputation: 151

I am able to resolve the issue, now.

Below changes I have done:

I removed the Errors.class

Following changes I have done in my Response.class:

@XmlRootElement (name="Response")
@XmlAccessorType( XmlAccessType.FIELD )
class Response {

  public Response() {
    errorList = new ArrayList<Error>()
  }

  @XmlElement(name="Success")
  private String success

  @XmlElementWrapper(name = "Errors")
  @XmlElement(name = "Error")
  private List<Error> errorList;


  public String getSuccess() {
    return success
  }

  public List<Error> getErrorList() {
    return errorList
  }
}

After this change, I am able to get the error list too as below:

Success: N

Size of Error List: 2

errorNumber: 29002

errorMessage: A key field was missing from the control xml

errorNumber: 29004

errorMessage: Unable to accept messages at this time

Upvotes: 2

Related Questions