Class Cast Exception when trying to unmarshall xml?(尝试解组 xml 时出现类强制转换异常?)
问题描述
在这里尝试通过类转换异常:
Trying to get past a class cast exception here:
FooClass fooClass = (FooClass ) unmarshaller.unmarshal(inputStream);
抛出此异常:
java.lang.ClassCastException: javax.xml.bind.JAXBElement
我不明白这一点 - 因为该类是由 xjc.bat 工具生成的 - 它生成的类我根本没有改变 - 所以这里应该没有转换问题 - 解组器真的应该给我返回一个可以转换为 FooClass 的类.
I don't understand this - as the class was generated by the xjc.bat tool - and the classes it generated I have not altered at all - so there should be no casting problems here - the unmarshaller should really be giving me back a class that CAN be cast to FooClass.
关于我做错了什么有什么想法吗?
Any ideas as to what I am doing wrong?
推荐答案
FooClass有XmlRootElement注解吗?如果没有,请尝试:
Does FooClass have the XmlRootElement annotation? If not, try:
Source source = new StreamSource(inputStream);
JAXBElement<FooClass> root = unmarshaller.unmarshal(source, FooClass.class);
FooClass foo = root.getValue();
这基于 非官方 JAXB 指南.
这篇关于尝试解组 xml 时出现类强制转换异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:尝试解组 xml 时出现类强制转换异常?
基础教程推荐
- 将 Windows 证书导入 Java 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
- Maven:无效的目标版本:10 2022-01-01
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
- JPA惰性列表上的流 2022-01-01
- 将 double 转换为 Int,向下舍入 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
