参考我之前的帖子。我有以下代码从文件中读取 XML。
logger.log(Level.INFO, "testSchemaChange");
JAXBContext jaxbContext = JAXBContext.newInstance("com.store.web.ws.v1.model");
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
JAXBElement<BookType> jeBookType = (JAXBElement<BookType>) unmarshaller.unmarshal(new File (BOOK_XML));
BookType bookType = jeBookType.getValue();
logger.log(Level.INFO, "Book recieved: " + bookType.getISBN());
我正在播种一个 XMLBook
作为我的根元素,而不是BookList
. 只要我将包的完整路径保留在JAXBContext.newInstance
.
不久,我有没有改变
JAXBContext jaxbContext = JAXBContext.newInstance("com.store.web.ws.v1.model");
到
JAXBContext jaxbContext = JAXBContext.newInstance(BookType.class);
我开始遇到异常SEVERE: unexpected element (uri:"", local:"book"). Expected elements are (none)
有人可以解释为什么会这样吗?
PS:为方便起见,我的测试类和 JAXB 生成的类在同一个包中,即com.store.web.ws.v1.model