跪求高手帮忙解决这个异常!

kevin123de 2012-10-17 04:35:38
这是用jaxb绑定一个xml文件所出现的异常:
Exception in thread "main" java.lang.ClassCastException: javax.xml.bind.JAXBElement cannot be cast to com.part.jaxp.Users
at com.part.jaxp.JavaTest.main(JavaTest.java:15)


package com.part.jaxp;

import java.io.File;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class JavaTest {

public static void main(String[] args) throws JAXBException {
JAXBContext jc = JAXBContext.newInstance("com.part.jaxp");
Unmarshaller unmarshaller = jc.createUnmarshaller();
Users users= (Users)unmarshaller.unmarshal(new File( "src/user.xml"));
//Books books = collection.getBooks();
List<User> userList = users.getUser();
for (User user : userList) {
System.out.println(user.getName());
}
}

}
...全文
185 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
kevin123de 2012-10-19
  • 打赏
  • 举报
回复
嗯 !谢了!已经解决了!
brightyq 2012-10-18
  • 打赏
  • 举报
回复
有没有导入类
javax.xml.bind.JAXBElement

另外,看你环境里,编译、运行时用JDK版本一致不
kevin123de 2012-10-17
  • 打赏
  • 举报
回复
这种方法还是不行!它会报这个错:Description Resource Path Location Type
JAXBElement cannot be resolved to a type JavaTest.java /eee/src/com/part/jaxp line 15 Java Problem
brightyq 2012-10-17
  • 打赏
  • 举报
回复

Users users= (Users)unmarshaller.unmarshal(new File( "src/user.xml"));

改成下面试试

Users users = (Users)((JAXBElement) unmarshaller.unmarshal(new File("src/user.xml"))).getValue();

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧