【问题标题】:JAXB Unmarshalling with Groovy使用 Groovy 进行 JAXB 解组
【发布时间】:2014-10-22 05:56:10
【问题描述】:

我正在尝试为以下 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>

这是我的 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;
  }
}

这是我的 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
  }
}

这是我的 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
    }
}

下面是我的解组类 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())
}

我可以得到 Success 但错误列表没有出现,它显示错误列表大小为 0。输出如下:

成功:N 错误列表大小:0

谁能帮我理解,我在哪里失踪?

谢谢

【问题讨论】:

    标签: xml groovy jaxb unmarshalling


    【解决方案1】:

    您已将Error 元素命名为Errors(请注意末尾的s)。请参阅Errors.errorList。因此 JAXB 不会处理您的 Error 元素。

    顺便说一句,您不一定需要Errors 类。您可以改用@XmlElementWrapper(name="Errors")

    【讨论】:

      【解决方案2】:

      我现在可以解决问题了。

      我所做的更改如下:

      我删除了 Errors.class

      按照我在 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
        }
      }
      

      更改后,我也可以得到如下错误列表:

      成功:否

      错误列表的大小:2

      错误编号:29002

      errorMessage:控件 xml 中缺少关键字段

      错误编号:29004

      errorMessage:此时无法接受消息

      【讨论】:

      • 干得好,非常好,您报告了完整的解决方案。
      猜你喜欢
      • 2014-04-07
      • 1970-01-01
      • 2013-05-30
      • 2014-02-10
      • 1970-01-01
      • 2013-11-19
      • 1970-01-01
      • 2013-01-17
      相关资源
      最近更新 更多