2

目前我正在使用 Castor 框架将对象编组为 xml 文件,它工作得很好

Writer writer = new FileWriter("D:/out.xml");
Marshaller.marshal(test, writer);

但现在我正在使用 javax.xml.bind 来做同样的事情。

            Writer writer = new FileWriter("D:/out.xml");
        JAXBContext context =
            JAXBContext.newInstance(test.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.marshal(test, writer);

然后我遇到了这个错误信息:

无法将类型“package1.Testing”编组为元素,因为它缺少 @XmlRootElement 注释]

4

1 回答 1

2

添加XmlRootElement注释,您将不再收到错误消息。这应该添加到顶级或“根”类。

于 2010-01-12T02:29:43.480 回答