Java java.lang.ClassCastException: javax.xml.bind.JAXBElement 在使用 IntelliJ 生成代码时无法转换异常
Java java.lang.ClassCastException: javax.xml.bind.JAXBElement cannot be cast exception when code generated using IntelliJ
我使用 IntelliJ 工具 > JAXB > 使用 JAXB 从 XML 模式生成 Java 代码...XSD 创建了 Java 个对象...
我基本上是在尝试从 XSD 生成 Java 对象,然后将符合此 XSD 的 XML 读入 Java对象。
对于我的练习,我使用的是 XSD 来自与 Credit Transfer Version 1.10 相关的六组网站。
但是,当我尝试 运行 以下代码时,我看到一个异常:Java.lang.ClassCastException:javax.xml.bind.JAXBElement 无法转换
public class Pain001Tester {
public static void main(String[] args) throws IOException {
String fileName = "pain_001_Beispiel_1.xml";
ClassLoader classLoader = new Pain001Tester().getClass().getClassLoader();
File file = new File(classLoader.getResource(fileName).getFile());
//File is found
System.out.println("File Found : " + file.exists());
//Read File Content
String content = new String(Files.readAllBytes(file.toPath()));
System.out.println(content);
JAXBContext jaxbContext;
try
{
jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
ObjectFactory xmlMessage = (ObjectFactory) jaxbUnmarshaller.unmarshal(new StringReader(content));
//ObjectFactory xmlMessage = (ObjectFactory) JAXBIntrospector.getValue(jaxbUnmarshaller.unmarshal(new StringReader(content)));
//JAXBElement<ObjectFactory> userElement = (JAXBElement<ObjectFactory>) jaxbUnmarshaller.unmarshal(new StringReader(content));
//ObjectFactory user = userElement.getValue();
System.out.println(xmlMessage);
}
catch (JAXBException e)
{
e.printStackTrace();
}
}
}
我认为我的问题与 XMLRootElement 有关,但不确定这是否是问题所在。我了解以下 Whosebug 问题与我的问题很接近,但无法使用 Whosebug 案例中突出显示的几个解决方案解决我的问题:No @XmlRootElement generated by JAXB
我假设错误发生在
ObjectFactory xmlMessage = (ObjectFactory) jaxbUnmarshaller.unmarshal(new StringReader(content));
jaxbUnmarshaller.unmarshal() 不是 return ObjectFactoryt 类型,因此您不能将结果转换为 ObjectFactory。它 return 是生成的 class 的实例,对应于 xml 文件的根元素。
我想我找到了问题(Heri 先按了按钮,所以他得到了 cookie)
我更改了以下内容:
ObjectFactory xmlMessage = (ObjectFactory) JAXBIntrospector.getValue(jaxbUnmarshaller.unmarshal(new StringReader(content)));
到
JAXBElement xmlMessage = (JAXBElement) jaxbUnmarshaller.unmarshal(new StringReader(content));
然后,我没有检索对 ObjectFactory class 的响应,而是使用了由 JAXB 生成的根元素 class 并且它起作用了。
我不确定为什么用 @XmlRootElement(name = "RootElement") 注释 ObjectFactory 不起作用,但我现在有一个可行的解决方案。
我使用 IntelliJ 工具 > JAXB > 使用 JAXB 从 XML 模式生成 Java 代码...XSD 创建了 Java 个对象...
我基本上是在尝试从 XSD 生成 Java 对象,然后将符合此 XSD 的 XML 读入 Java对象。
对于我的练习,我使用的是 XSD 来自与 Credit Transfer Version 1.10 相关的六组网站。
但是,当我尝试 运行 以下代码时,我看到一个异常:Java.lang.ClassCastException:javax.xml.bind.JAXBElement 无法转换
public class Pain001Tester {
public static void main(String[] args) throws IOException {
String fileName = "pain_001_Beispiel_1.xml";
ClassLoader classLoader = new Pain001Tester().getClass().getClassLoader();
File file = new File(classLoader.getResource(fileName).getFile());
//File is found
System.out.println("File Found : " + file.exists());
//Read File Content
String content = new String(Files.readAllBytes(file.toPath()));
System.out.println(content);
JAXBContext jaxbContext;
try
{
jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
ObjectFactory xmlMessage = (ObjectFactory) jaxbUnmarshaller.unmarshal(new StringReader(content));
//ObjectFactory xmlMessage = (ObjectFactory) JAXBIntrospector.getValue(jaxbUnmarshaller.unmarshal(new StringReader(content)));
//JAXBElement<ObjectFactory> userElement = (JAXBElement<ObjectFactory>) jaxbUnmarshaller.unmarshal(new StringReader(content));
//ObjectFactory user = userElement.getValue();
System.out.println(xmlMessage);
}
catch (JAXBException e)
{
e.printStackTrace();
}
}
}
我认为我的问题与 XMLRootElement 有关,但不确定这是否是问题所在。我了解以下 Whosebug 问题与我的问题很接近,但无法使用 Whosebug 案例中突出显示的几个解决方案解决我的问题:No @XmlRootElement generated by JAXB
我假设错误发生在
ObjectFactory xmlMessage = (ObjectFactory) jaxbUnmarshaller.unmarshal(new StringReader(content));
jaxbUnmarshaller.unmarshal() 不是 return ObjectFactoryt 类型,因此您不能将结果转换为 ObjectFactory。它 return 是生成的 class 的实例,对应于 xml 文件的根元素。
我想我找到了问题(Heri 先按了按钮,所以他得到了 cookie)
我更改了以下内容:
ObjectFactory xmlMessage = (ObjectFactory) JAXBIntrospector.getValue(jaxbUnmarshaller.unmarshal(new StringReader(content)));
到
JAXBElement xmlMessage = (JAXBElement) jaxbUnmarshaller.unmarshal(new StringReader(content));
然后,我没有检索对 ObjectFactory class 的响应,而是使用了由 JAXB 生成的根元素 class 并且它起作用了。
我不确定为什么用 @XmlRootElement(name = "RootElement") 注释 ObjectFactory 不起作用,但我现在有一个可行的解决方案。