Baderous
Baderous

Reputation: 1069

Unmarshall with JAXB doesn't work

I have a simpe XML that I want to unmarshall into a model class. I have annotated the class with JAXB annotations for defining the access type (FIELD):

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;

@XmlAccessorType(XmlAccessType.FIELD)
public class DtoTest {

    private String name;

    public DtoTest() {}

    public DtoTest(String name) {
        super();
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "DtoTest [name=" + name + "]";
    }
}

This is my main class where I run an unmarshal method against a simple XML saved in a String variable:

public class Test {

    public static void main(String[] args) throws Exception {
        Object obj = new DtoTest();
        String testXML = "<dtoTest><name>example</name></dtoTest>";
        obj = unmarshal(obj, testXML);
        System.out.println(obj);
    }

    /* This is a generic unmarshall method which I've already used with success with other XML*/
    public static <T> T unmarshal(T obj, String xml) throws Exception {
        XMLInputFactory xif = XMLInputFactory.newFactory();
        XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(xml));

        Class<? extends Object> type = obj.getClass();
        JAXBContext jc = JAXBContext.newInstance(type);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        obj =  (T)unmarshaller.unmarshal(xsr, type).getValue();
        xsr.close();

        return obj;
    }
}

Whenever I run the code I get the same output:

DtoTest [name=null]

I don't understand what I'm doing wrong.

Upvotes: 1

Views: 2074

Answers (2)

bdoughan
bdoughan

Reputation: 148977

What you have in your question runs perfectly fine for me. One optimization you could make to it is to create an StreamSource instead of an XMLStreamReader.

import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;
import java.io.StringReader;

public class Test {

    public static void main(String[] args) throws Exception {
        Object obj = new DtoTest();
        String testXML = "<dtoTest><name>example</name></dtoTest>";
        obj = unmarshal(obj, testXML);
        System.out.println(obj);
    }

    public static <T> T unmarshal(T obj, String xml) throws Exception {
        StreamSource source = new StreamSource(new StringReader(xml));

        Class<? extends Object> type = obj.getClass();
        JAXBContext jc = JAXBContext.newInstance(type);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        obj =  (T)unmarshaller.unmarshal(source, type).getValue();

        return obj;
    }

}

Debugging Tip

When unmarshalling is not working as expected, populate your JAXB model and marshal it to XML to see what the expected XML looks like.

Upvotes: 1

Marek Smigielski
Marek Smigielski

Reputation: 361

I've just run your code on jdk1.7.0_67 and it works.

DtoTest [name=example]

Maybe you have some problem with included libraries? I've run it with just plain java.

Upvotes: 2

Related Questions