JAXB 解组 XML 类强制转换异常

JAXB Unmarshalling XML Class Cast Exception(JAXB 解组 XML 类强制转换异常)
本文介绍了JAXB 解组 XML 类强制转换异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在使用这个 JAXB Collection Generics 来解组我的字符串 xml 并返回 List 类型.这是我使用的方法.

I'm using this JAXB Collection Generics to unmarshall my string xml and return the List type. Here's the methods I used.

public static <T> List<T> unmarshalCollection(Class<T> cl, String s)
        throws JAXBException {
    return unmarshalCollection(cl, new StringReader(s));
}

public static <T> List<T> unmarshalCollection(Class<T> cl, Reader r)
        throws JAXBException {
    return unmarshalCollection(cl, new StreamSource(r));
}


private static <T> List<T> unmarshalCollection(Class<T> cl, Source s)
        throws JAXBException {
    JAXBContext ctx = JAXBContext.newInstance(JAXBCollection.class, cl);
    Unmarshaller u = ctx.createUnmarshaller();
    JAXBCollection<T> collection = u.unmarshal(s, JAXBCollection.class).getValue();
    return collection.getItems();
}

getter 和 setter 示例:

Example getters and setters:

   @XmlRootElement(name = "person")
class Person{

    private String firstName;
    private String lastName;
    private String address;


    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Person [firstName ="+firstName+" , lastName = "+lastName+" , address = "+address+"]"; 
    }

}

主类:

public static void main(String[] args) throws JAXBException {
        String xml = "<?xml version="1.0" encoding="UTF-8"?><person><firstName>Foo</firstName><lastName>Bar</lastName><address>U.S</address></person>";
        List<Person> p = unmarshalCollection(Person.class,xml);
        for(Person person : p ){
            System.out.println(person);
        }
    }

抛出异常

Exception in thread "main" java.lang.ClassCastException: org.apache.xerces.dom.ElementNSImpl cannot be cast to  com.Person
at com.JAXBUtil.main(JAXBUtil.java:62)

我做错了什么?有什么想法吗?谢谢.

What did I do wrong? Any ideas?Thanks.

推荐答案

您的列表不是您期望的 Person 对象列表.由于 java 的泛型类型擦除,在您尝试在循环中强制转换为 person 之前,您不会看到错误.

Your list is not a list of Person objects as you expect. Due to java's generic type erasure, you're not seeing the error until you try to cast to person in the loop.

试试:

List<Person> p = unmarshalCollection(Person.class,xml);
for(Object person : p ){
   System.out.println(person.getClass());
}

参见 http://en.wikipedia.org/wiki/Generics_in_Java#Problems_with_type_erasure

这篇关于JAXB 解组 XML 类强制转换异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)