尝试取消编组xml时的类强制转换异常? [英] Class Cast Exception when trying to unmarshall xml?

查看:119
本文介绍了尝试取消编组xml时的类强制转换异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试通过此处的类投射异常:

Trying to get past a class cast exception here:

FooClass fooClass = (FooClass ) unmarshaller.unmarshal(inputStream);

抛出此异常:

java.lang.ClassCastException: javax.xml.bind.JAXBElement

I不明白这一点 - 因为这个类是由xjc.bat工具生成的 - 它生成的类我根本没有改变 - 所以这里应该没有转换问题 - unmarshaller应该真的给我一个类可以投射到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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆