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 时出现类强制转换异常?
基础教程推荐
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
