Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Unmarshal list of objects with a class wrapper with JAXB

Tags:

java

xml

jaxb

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

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

like image 795
SagittariusA Avatar asked Jan 10 '23 04:01

SagittariusA


1 Answers

For your use case you do not need the @XmlElementWrapper annotation. This is because the ProtocolList element corresponds to your @XmlRootElement annotation. Then you need the @XmlElement annotation on the property to grab each of the list items.

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


UPDATE

You need to make sure your JAXBContext is aware of the root class. You can change your JAXBContext creation code to be the following:

JAXBContext jaxbContext = JAXBContext.newInstance(ProtocolloList.class);
like image 98
bdoughan Avatar answered Jan 17 '23 17:01

bdoughan