尝试解组 xml 时出现类强制转换异常? [英] 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 时出现类强制转换异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文