tags:

views:

66

answers:

1

Hi, I am trying to unmarshall a XML document created from jersey JAXB annotated class.

JAXBContext jaxbCtx = JAXBContext.newInstance(MyClass.class);
Unmarshaller m = jaxbCtx.createUnmarshaller();
MyClass result = (MyClass) m.unmarshal(in)

MyClass looks something like:

@XmlRootElement(name = "my-class")
@XmlSeeAlso(SomeOther.class)
public class MyClass {
    private Collection<SomeOther> result;
    private URI uri;
    private String errorMsg;

    @XmlElement
    public String getError() {
         return errorMsg;
    }
    @XmlElement
    public Collection<SomeOther> getResult() {
         return // some Set<SomeOther>;
    }
    @XmlAttribute
    public URI getUri() {
         return uri;
    }

The sample XML is as below:

<my-class uri="some uri">
    <error></error>
    <result>
         <some other information in tags>
    </result>
    ...
    <result>
    </result>
</my-class>

The object returned by jaxb unmarshaler contains all values as null; Could somebody help here? Thanks Nayn

+1  A: 

It is because you are missing the set methods. If you only provide the get methods, then JAXB considers this property to be write only.

If you do not wish to add set methods then you can add the following to your class:

@XmlAccessorType(XmlAccessType.FIELD)

And then annotate the fields instead of the properties.

Blaise Doughan
Could you please explain what are fields and what are properties here?
Nayn
Fields are the instance variables (such as uri) and properties are the get/set combinations (such as getUri()/setUri()).
Blaise Doughan
This worked well. Thanks a lot Doughan. It cleared my understanding about JAXB.
Nayn