Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping an XML sub-child with jaxb annotations

I am trying to Unmarshal the following xml:

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

And I wrote the following binding classes to aid achieve Marshalling/Unmarshalling:

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;
}

second model class

@XmlAccessorType(XmlAccessType.FIELD)
public class AuthorMeta {

@XmlElement
private Atomlink author;

public Atomlink getLink() {
    return author;
}

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

The last model class, this will actually be bind with the retrieved xml

   @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;
}




}

But during the deployement i'm taking an error which says:

  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

I'm not familiar with annotations but could anyone say to me how to use them in this specific occasion. I want to create with the annotations something like this:

<xmlattribute>
  <xmlelement>
    <xml sub-child/>
  </xmlelement>
  <xmlelement>
    <xml sub-child/>
  </xmlelement>
</xmlattribute>
like image 763
Alex Dowining Avatar asked May 07 '12 21:05

Alex Dowining


1 Answers

The exception hold the answers. Try this instead.

@XmlElement
private List<AuthorMeta> link;
like image 74
Bitmap Avatar answered Sep 25 '22 23:09

Bitmap