JaxB中的@XmlValue和@XmlElement冲突

时间:2016-10-25 13:26:24

标签: jaxb

我正在阅读XML文件。

<MSS id="9B8F6188-9636-11E0-8718-88F89CFF4B22">
  <STANDARD id="1EBE0C64-9892-11E0-8388-5F489DFF4B22">
      <TOPIC> 
          Reading 
          <TOPIC> Literature </TOPIC>
           <TOPIC> Writing </TOPIC>
      </TOPIC>
  </STANDARD>
</MSS>

对于上面的xml,我创建了像POJO

@XmlRootElement(name="MSS")
@XmlAccessorType(XmlAccessType.FIELD)
public class MSS
{
    private String id;

    private STANDARD STANDARD;

    public String getId ()
    {
        return id;
    }

    public void setId (String id)
    {
        this.id = id;
    }

    public STANDARD getSTANDARD ()
    {
        return STANDARD;
    }

    public void setSTANDARD (STANDARD STANDARD)
    {
        this.STANDARD = STANDARD;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [id = "+id+", STANDARD = "+STANDARD+"]";
    }
}

STANDARD.java

@XmlAccessorType(XmlAccessType.FIELD)
    public class STANDARD
    {
        private String id;

        @XmlElement(name="TOPIC")
        private TOPIC TOPIC;

        public String getId ()
        {
            return id;
        }

        public void setId (String id)
        {
            this.id = id;
        }

        public TOPIC getTOPIC ()
        {
            return TOPIC;
        }

        public void setTOPIC (TOPIC TOPIC)
        {
            this.TOPIC = TOPIC;
        }

        @Override
        public String toString()
        {
            return "ClassPojo [id = "+id+", TOPIC = "+TOPIC+"]";
        }
    }

和TOPIC.java

@XmlAccessorType(XmlAccessType.NONE)
public class TOPIC
{
    @XmlValue
    private String content;

    @XmlElement(name="TOPIC")
    private String TOPIC;

    public String getContent ()
    {
        return content;
    }

    public void setContent (String content)
    {
        this.content = content;
    }

    public String getTOPIC ()
    {
        return TOPIC;
    }

    public void setTOPIC (String TOPIC)
    {
        this.TOPIC = TOPIC;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [content = "+content+", TOPIC = "+TOPIC+"]";
    }
}

当我尝试解组它时,我得到了像

这样的异常

如果某个类具有@XmlElement属性,则它不能具有@XmlValue属性

我在主题标签中有多个主题,我想要主题内容&#34;阅读&#34;值。

1 个答案:

答案 0 :(得分:0)

正如@mahi建议您可以使用List而不是String主题将@XmlMixed添加到内容中。问题解决了。