Unmarshalling using JAXB

╄→гoц情女王★ 提交于 2019-12-11 10:56:43

问题


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

Following the link below Marshalling a List of objects implementing a common interface, with JaxB

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

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


回答1:


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

}

For More Information

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



回答2:


It's easier to help, if you showed your code...

The problems seems to be, that you have getters and setters and which confuses JAXB, because it does not know how to use them to unmarshal the xml.

Try to use FIELD access type:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="Blubb", namespace=ServiceConstants.XML_NAMESPACE)
public class Blubb implements Serializable {

    @XmlElement(name="Bla", namespace=ServiceConstants.XML_NAMESPACE)
    private Bla bla;

    public Blubb () {

    }

    public void setBla(Bla bla) { this.bla = bla; }

    public Bla getBla() { return this.bla; }
}


来源:https://stackoverflow.com/questions/8266579/unmarshalling-using-jaxb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!