users@jaxb.java.net

JAXB with interfaces - not working

From: lightbulb432 <veerukrishnan_at_hotmail.com>
Date: Mon, 12 Nov 2007 18:58:28 -0800 (PST)

The following is a sample of trying to use interfaces rather than refer to
classes directly, and it isn't working. In the debugger, the Parent object
isn't populated with a List of Child implementations - however, if Parent
refers to ChildImpl class rather than the Child interface, it works. What
am I doing wrong?

@XmlRootElement(name="parent")
public class Parent {

  private List<Child> children = new ArrayList<Child>();

  @XmlElementWrapper
  @XmlAnyElement
  public List<Child> getChildren() {
    return children;
  }

  public void setChildren(List<Child> children) {
    this.children = children;
  }
}

public interface Child {

  public abstract String getId();

  public abstract void setId(String id);

}

@XmlRootElement(name="child")
public class ChildImpl implements Child {
  
  private String id;

  @XmlAttribute
  public String getId() {
    return id;
  }

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

}

jaxb.index contains the following two lines, and is in the same package as
both the Parent and ChildImpl classes:
Parent
ChildImpl

The xml file being unmarshalled follows:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<parent>
  <children>
    <child id="one" />
    <child id="two" />
  </children>
</parent>

The classes are all in one package, the interfaces are all in another - but
this doesn't seem to be the cause of the problem. Could the cause be that I
need to put annotations on the Child interface? Either on the class itself,
or on the abstract getter method?

Any thoughts on how to make this work? Thanks.
-- 
View this message in context: http://www.nabble.com/JAXB-with-interfaces---not-working-tf4795511.html#a13719000
Sent from the java.net - jaxb users mailing list archive at Nabble.com.