带有 JAXB 类包装器的 Java 解组对象列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24934405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:49:40  来源:igfitidea点击:

Java Unmarshal list of objects with a class wrapper with JAXB

javaxmljaxb

提问by SagittariusA

From an XQuery performed by BaseX server I get a result like that:

从 BaseX 服务器执行的 XQuery 中,我得到如下结果:

<ProtocolloList>
  <protocollo>
    <numero>1</numero>
    <data>2014-06-23</data>
    <oggetto/>
    <destinatario/>
    <operatore/>
  </protocollo>
     ...
</ProtocolloList>

And I need to convert this result in a List of Protocollo objects with JAXB so that I can show them with JList. Thus, following one of the discussions hereI've declared the following classes:

我需要使用 JAXB 将此结果转换为 Protocollo 对象列表,以便我可以使用 JList 显示它们。因此,根据此处的讨论之一,我声明了以下类:

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "protocollo")
public class Protocollo {

  private int numero;
  private String data;
  private String oggetto;
  private String destinatario;
  private String operatore;

  public Protocollo(String d, String o, String des, String op) {
    this.data = d;
    this.oggetto = o;
    this.destinatario = des;
    this.operatore = op;
  }

  public Protocollo() {

  }

  @XmlElement
  public int getNumero() {
      return numero;
  }

  public void setNumero(int numero) {
      this.numero = numero;
  }

  @XmlElement
  public String getData() {
      return data;
  }

  public void setData(String data) {
      this.data = data;
  }

  @XmlElement
  public String getOggetto() {
      return oggetto;
  }

  public void setOggetto(String oggetto) {
      this.oggetto = oggetto;
  }

  @XmlElement
  public String getDestinatario() {
      return destinatario;
  }

  public void setDestinatario(String destinatario) {
      this.destinatario = destinatario;
  }

  @XmlElement
  public String getOperatore() {
      return operatore;
  }

  public void setOperatore(String operatore) {
      this.operatore = operatore;
  }

}

and

import java.util.ArrayList;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "ProtocolloList")
public class ProtocolloList {

  @XmlElementWrapper(name = "ProtocolloList")
  @XmlElement(name = "protocollo")
  private ArrayList<Protocollo> ProtocolloList;

  public ArrayList<Protocollo> getProtocolloList() {
      return ProtocolloList;
  }

  public void setProtocolloList(ArrayList<Protocollo> protocolloList) {
      ProtocolloList = protocolloList;
  }
}

and finally I execute the converion like that:

最后我执行这样的转换:

JAXBContext jaxbContext = JAXBContext.newInstance(Protocollo.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(this.resultXML);
protocolli = (ProtocolloList) unmarshaller.unmarshal(reader);

And I keep on getting this exception:

我不断收到此异常:

unexpected element (uri:"", local:"ProtocolloList"). Expected elements are <{}protocollo>

I suppose I'm making some mistakes with annotations. Can you help?

我想我在注释方面犯了一些错误。你能帮我吗?

采纳答案by bdoughan

For your use case you do not need the @XmlElementWrapperannotation. This is because the ProtocolListelement corresponds to your @XmlRootElementannotation. Then you need the @XmlElementannotation on the property to grab each of the list items.

对于您的用例,您不需要@XmlElementWrapper注释。这是因为该ProtocolList元素对应于您的@XmlRootElement注释。然后您需要@XmlElement属性上的注释来获取每个列表项。

@XmlRootElement(name = "ProtocolloList")
public class ProtocolloList {

  private ArrayList<Protocollo> ProtocolloList;

  @XmlElement(name = "protocollo")
  public ArrayList<Protocollo> getProtocolloList() {
      return ProtocolloList;
  }

}

Note:

笔记:

By default you should annotate the property. If you want to annotate the fields you should put @XmlAccessorType(XmlAccessType.FIELD)on your class.

默认情况下,您应该注释该属性。如果你想注释你应该放在@XmlAccessorType(XmlAccessType.FIELD)你的类上的字段。



UPDATE

更新

You need to make sure your JAXBContextis aware of the root class. You can change your JAXBContextcreation code to be the following:

您需要确保您JAXBContext知道根类。您可以将JAXBContext创建代码更改为以下内容:

JAXBContext jaxbContext = JAXBContext.newInstance(ProtocolloList.class);