我越来越
javax.xml.bind.JAXBException: "org.example.mypackage" doesnt contain ObjectFactory.class or jaxb.index
在尝试创建JAXBContext
using时JAXBContext.newInstance(String contextPath)
。我猜有一种“通常”的方式来创建和维护一个 jaxb.index 文件。
我越来越
javax.xml.bind.JAXBException: "org.example.mypackage" doesnt contain ObjectFactory.class or jaxb.index
在尝试创建JAXBContext
using时JAXBContext.newInstance(String contextPath)
。我猜有一种“通常”的方式来创建和维护一个 jaxb.index 文件。
jaxb.index 文件只是包含包中具有 JAXB 注释的类的列表。
文件中的每一行都是一个类的简单名称,而不是它的完全限定名称。
你可以在这里阅读更多:http: //cmaki.blogspot.com/2007/09/annotated-jaxb-classes.html
试试这个方法
JAXBContext context = JAXBContext.newInstance(new Class[] {your.package.Test.class});
此外,请确保您已添加@XmlRootElement
到 Test 类。
@XmlRootElement
class Test {
private String ...;
private int ......;
}
还要确保您使用的是 java 1.5
确保您将正确的类传递给该方法。假设您的 XML 根元素是 XMLRoot,您可以将其称为:
JAXBContext context = JAXBContext.newInstance(XMLRoot.class);
还要确保您使用正确版本的 JAXB 编译器 (xjc) 来运行您正在运行的 Java 版本。来自旧编译器的 JAXB 生成的类无法与 Java 6 的 JAXB 一起正常工作,从而产生相同的错误。