简体   繁体   English

Java使用JAXB的类包装器解组对象列表

[英]Java Unmarshal list of objects with a class wrapper with JAXB

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. 我需要使用JAXB将此结果转换为Protocollo对象列表,以便我可以使用JList显示它们。 Thus, following one of the discussions here I've declared the following classes: 因此,在这里的一个讨论后我宣布了以下类:

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? 你能帮我吗?

For your use case you do not need the @XmlElementWrapper annotation. 对于您的用例,您不需要@XmlElementWrapper注释。 This is because the ProtocolList element corresponds to your @XmlRootElement annotation. 这是因为ProtocolList元素对应于@XmlRootElement注释。 Then you need the @XmlElement annotation on the property to grab each of the list items. 然后,您需要在属性上使用@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 UPDATE

You need to make sure your JAXBContext is aware of the root class. 您需要确保您的JAXBContext知道根类。 You can change your JAXBContext creation code to be the following: 您可以将JAXBContext创建代码更改为以下内容:

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

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

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