我見過的語法如下各種堆棧溢出的帖子和博客條目:兩個的Unmarshaller.unmarshal()的元素形式
JAXBElement<SomeClass> sc = unmarshaller.unmarshal(is, SomeClass.class);
那麼,爲什麼日食給我一個編譯錯誤,當我嘗試使用此句法?爲什麼這個語法不在api中,你可以閱讀at this link?
以下是編譯錯誤:
The method unmarshal(Node, Class<T>) in the type Unmarshaller
is not applicable for the arguments (FileInputStream, Class<SomeClass>)
這裏是一個將使用上述語法的完整方法:
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();}
}
此語法在開發者需要組XML實例因爲不包含已定義的根元素。一個例子是Sayantam的回答to this question。
什麼是編譯錯誤? – Denise 2014-10-22 00:16:40
請同時發佈你看到這個例子的地方(鏈接)。 – 2014-10-22 00:32:44