使用JAXB进行解组 [英] Unmarshalling using JAXB

查看:134
本文介绍了使用JAXB进行解组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是java的新手(来自c#.net背景)并尝试上面的示例来编组和解组。

I am new to java (coming from c#.net background) and was trying the above example to marshal and unmarshal.

点击下面的链接
使用JaxB编组实现公共接口的对象列表

使用Mr.Blaise Doughan先生提到的上述技术,我能够将java对象编组为xml。但是当我保存这个xml并尝试将xml解组回java对象时,我在控制台上得到以下内容:

using the above technique as mentioned by Mr.Blaise Doughan, I was able to marshal the java objects to xml. But when i save this xml and try to unmarshal the xml back to java object i get the following on the console:


com.sun。 xml.internal.bind.v2.runtime.IllegalAnnotationsException:2
IllegalAnnotationExceptions计数
@ javax.xml.bind.annotation.XmlElementRef注释位于两个
位置;一个就够了。此问题与以下
位置有关:at @javax.xml.bind.annotation.XmlElementRef(name = ## default,
required = true,type = class
javax.xml。 public.b $ b java.util.List中的bind.annotation.XmlElementRef $ DEFAULT,namespace =)社区的Community.getPeople()这个问题是
与以下位置有关:at
@javax。公共
中的xml.bind.annotation.XmlElementRef(name = ## default,
required = true,type = class
javax.xml.bind.annotation.XmlElementRef $ DEFAULT,namespace =) void社区的Community.setPeople(java.util.List)....

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions @javax.xml.bind.annotation.XmlElementRef annotation is found on two places; one would be suffice. this problem is related to the following location: at @javax.xml.bind.annotation.XmlElementRef(name=##default, required=true, type=class javax.xml.bind.annotation.XmlElementRef$DEFAULT, namespace=) at public java.util.List Community.getPeople() at Community this problem is related to the following location: at @javax.xml.bind.annotation.XmlElementRef(name=##default, required=true, type=class javax.xml.bind.annotation.XmlElementRef$DEFAULT, namespace=) at public void Community.setPeople(java.util.List) at Community ....

注意:我为类创建了getter / setter用于实现解组的男孩和女孩。

Note : I have created getters/setters for Class Boy and Class Girl for implementing unmarshalling.

推荐答案

看来你可能已经注释了getPeople和setPeople方法。 JAXB(和其他Java EE技术)只需要您注释一个。

It appears that you may have annotated both the getPeople and setPeople methods. JAXB (and other Java EE technologies) only require you annotate one.

public class Community {

    private List<Person> people;

    @XmlElementRef
    public List<Person> getPeople() {
        return people;
    }

    public void setPeople(List<Person> people) {
        this.people = people;
    } 

}

更多信息

  • http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-substitution.html
  • http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html

这篇关于使用JAXB进行解组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆