5 Stimmen

Mapping eines XML-Unterobjekts mit jaxb-Anmerkungen

Ich versuche, die folgende Xml-Datei zu entschachteln:

  <datas xmlns="http://www..." xmlns:atom="http://www.w3.org/2005/atom">
     <data>
         <atom:link rel="data" type="application/xml" href="http://www.../ckp/data/1"/>
     </data>
     <data>
       <atom:link rel="data" type="application/xml" href="http://www.../ckp/data/2"/>
     </data>
     <data>
        <atom:link rel="data" type="application/xml" href="http://www.../ckp/data/3"/>
     </data>
  </datas>

Und ich habe die folgenden Bindungsklassen geschrieben, um das Marshalling/Unmarshalling zu unterstützen:

public class Links{

@XmlAttribute
private String rel;
@XmlAttribute
private String type;
@XmlAttribute
private String href;

public String getRel() {
    return rel; 
}

public void setRel(String rel) {
    this.rel = rel;
}
public String getType() {
    return type;    
}

public void setType(String type) {
    this.type = type;
}
public String getHref() {
    return href;    
}

public void setHref(String href) {
    this.href = href;
}

zweite Modellklasse

@XmlAccessorType(XmlAccessType.FIELD)
public class AuthorMeta {

@XmlElement
private Atomlink author;

public Atomlink getLink() {
    return author;
}

public void setLink(Atomlink link) {
    this.author = link;
}

Die letzte Modellklasse, die tatsächlich mit der abgerufenen XML-Datei verknüpft werden soll

   @XmlRootElement(name="datas")
   @XmlAccessorType(XmlAccessType.FIELD)
   public class Datas{

    @XmlAttribute
    private String datas;
    @XmlElement
    private String data;
    @XmlValue
private List<DataMeta> link;

public String getDatas() {
    return datas;
}
public String getData() {
    return data;
}

public List<DataMeta> getAtom() {
    return link;
}

public void setDatas(String datas) {
    this.datas= datas;
}

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

public void setLink(List<DataMeta> link) {
    this.link = link;
}

}

Aber während des Einsatzes erhalte ich eine Fehlermeldung, die besagt:

  Caused by: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts  of IllegalAnnotationExceptions
  If a class has @XmlElement property, it cannot have @XmlValue property.
this problem is related to the following location:
    at private java.util.List org.client.model.Datas.link
    at org.client.model.Datas
this problem is related to the following location:
    at private java.lang.String org.client.model.Datas.data
    at org.client.model.Datas
   @XmlAttribute/@XmlValue need to reference a Java type that maps to text in XML.
this problem is related to the following location:
    at private java.util.List org.client.model.Datas.link
    at org.client.model.Datas

Ich kenne mich mit Anmerkungen nicht aus, aber kann mir jemand sagen, wie man sie in diesem speziellen Fall verwenden kann? Ich möchte mit den Anmerkungen etwas wie dieses erstellen:

<xmlattribute>
  <xmlelement>
    <xml sub-child/>
  </xmlelement>
  <xmlelement>
    <xml sub-child/>
  </xmlelement>
</xmlattribute>

5voto

Bitmap Punkte 11935

In der Ausnahme sind die Antworten enthalten. Versuchen Sie stattdessen dies.

@XmlElement
private List<AuthorMeta> link;

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X