使用jaxb注释映射XML子子

时间:2012-05-07 21:26:56

标签: java spring-mvc annotations jaxb

我正在尝试解组以下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>

我编写了以下绑定类来帮助实现编组/解组:

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

第二个模型类

@XmlAccessorType(XmlAccessType.FIELD)
public class AuthorMeta {

@XmlElement
private Atomlink author;

public Atomlink getLink() {
    return author;
}

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

最后一个模型类,它实际上将与检索到的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;
}




}

但是在部署过程中我犯了一个错误:

  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

我不熟悉注释,但有人可以告诉我如何在这个特定场合使用它们。我想创建带有这样的注释:

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

1 个答案:

答案 0 :(得分:5)

异常保留答案。试试这个。

@XmlElement
private List<AuthorMeta> link;