I used JAXB in some projects at the university. As far as I remember, you should return an object, for example, ItemList , and then request this object to retrieve the contained elements.
So your xml should look something like this:
<itemlist> <item> <code>..</code> <name>..</name> <price>..</price> </item> <item> <code>..</code> <name>..</name> <price>..</price> </item> . . </itemlist>
At this point, your Java code will look like this:
final Unmarshaller u = jc.createUnmarshaller(); final File f = new File("D:\\item.xml"); final JAXBElement element = (JAXBElement) u.unmarshal(f); final ItemList itemList = (ItemList) element.getValue();
source share