I have seen the following syntax in various stack overflow postings and in blog entries:
JAXBElement<SomeClass> sc = unmarshaller.unmarshal(is, SomeClass.class);
So why does eclipse give me a compilation error when I try to use this syntax? And why is this syntax not in the api, which you can read at this link?
Here is the compilation error:
The method unmarshal(Node, Class<T>) in the type Unmarshaller
is not applicable for the arguments (FileInputStream, Class<SomeClass>)
Here is a complete method that would use the above syntax:
public void unmarshal() throws FileNotFoundException{
Unmarshaller unmarshaller;
try {
JAXBContext ctx = JAXBContext.newInstance(ObjectFactory.class);
unmarshaller = ctx.createUnmarshaller();
FileInputStream is = new FileInputStream("path/to/some.xml");
JAXBElement<SomeClass> sc = unmarshaller.unmarshal(is, SomeClass.class);//error here
System.out.println("title is: "+sc.getValue().getTitle());
} catch (JAXBException e) {e.printStackTrace();}
}
This syntax is given in examples where a developer needs to unmarshal xml that does not contain a defined root element. One example is Sayantam's answer to this question.