1

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.

Community
  • 1
  • 1
jatin mistry
  • 205
  • 3
  • 7
  • Have a look at the "official" examples: http://docs.oracle.com/javaee/5/tutorial/doc/bnbah.html – Kai Nov 25 '11 at 08:53

2 Answers2

3

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

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • Hello Mr.Blaise Doughan Code is as follows: It is almost same as what i have seen in the reference link : http://stackoverflow.com/questions/4144296/marshalling-a-list-of-objects-with-jaxb/8266451#8266451 but now i m doing unmarshalling... JAXBContext jaxbContext = JAXBContext.newInstance(Community.class); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); Community a = (Community) unmarshaller.unmarshal(new File("FILE path of XML")); and yes i have annotated getPeople and setPeople methods both. – jatin mistry Nov 25 '11 at 09:35
  • 1
    Hello Sir, You were on the spot...!! Everything worked smooth after i kept annotation only for getter and not for setter for Person Class Unmarshalling Code:- JAXBContext jaxbContext = JAXBContext.newInstance(Community.class); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); Community a = (Community) unmarshaller.unmarshal(new File("XMLFilePath")); List lstPeople = a.getPeople(); Iterator objIterator = lstPeople.iterator(); while(objIterator.hasNext()) { Person objPerson = objIterator.next(); System.out.println(objPerson.getName()); } – jatin mistry Nov 25 '11 at 09:54
  • backtracking to what went wrong...the console output gave the clue of what was wrong...but since i m new to java was not able to understand the compiler hint... Thanks for the help Sir @Blaise Doughan. Marking your comment as the Answer...! -jatin – jatin mistry Nov 25 '11 at 10:01
0

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; }
}
hage
  • 5,966
  • 3
  • 32
  • 42
  • Hello @hage Code is as follows: It is almost same as what i have seen in the reference link : stackoverflow.com/questions/4144296/… but now i m doing unmarshalling... JAXBContext jaxbContext = JAXBContext.newInstance(Community.class); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); Community a = (Community) unmarshaller.unmarshal(new File("FILE path of XML")); and yes i have annotated getPeople and setPeople methods both – jatin mistry Nov 25 '11 at 09:36