看不懂的异常,求解答,使用过JAXB的进
使用JAXB解析XML文件
代码如下:
import java.io.File;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import com.topsoft.jaxb.BookType;
import com.topsoft.jaxb.Collection;
import com.topsoft.jaxb.Collection.Books;
public class JAXBTest {
public static void main(String[] args) throws JAXBException {
JAXBContext jc = JAXBContext.newInstance("com.topsoft.jaxp");
Unmarshaller unmarshaller = jc.createUnmarshaller();
Collection collection= (Collection)unmarshaller.unmarshal(new File( "src/books.xml"));
Books books = collection.getBooks();
List<BookType> bookList = books.getBook();
for (BookType bookType : bookList) {
System.out.println(bookType.getName());
}
}
}
异常如下:
Exception in thread "main" javax.xml.bind.JAXBException: "com.topsoft.jaxp" doesnt contain ObjectFactory.class or jaxb.index
at com.sun.xml.internal.bind.v2.ContextFactory.createContext(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at javax.xml.bind.ContextFinder.newInstance(Unknown Source)
at javax.xml.bind.ContextFinder.find(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at com.JAXBTest.main(JAXBTest.java:17)