首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XStream - com.thoughtworks.xstream.converters.ConversionException

XStream - com.thoughtworks.xstream.converters.ConversionException
EN

Stack Overflow用户
提问于 2018-04-01 23:07:51
回答 1查看 5.4K关注 0票数 2

我试图解析一个XML文档,这是文档的结构

代码语言:javascript
复制
    <students>
        <student student_id="1">
            <firstname>Lily</firstname>
            <lastname>Brown</lastname>
            <marks>
                <first>62</first>
                <second>90</second>
                <third>94</third>
                <forth>93</forth>
            </marks>
        </student>
   </students>

但我发现了这个错误

代码语言:javascript
复制
    Exception in thread "main" com.thoughtworks.xstream.converters.ConversionException: 
    ---- Debugging information ----
    cause-exception     : com.thoughtworks.xstream.mapper.CannotResolveClassException
    cause-message       : firstname
    class               : java.util.ArrayList
    required-type       : java.util.ArrayList
    converter-type      : com.thoughtworks.xstream.converters.collections.CollectionConverter
    path                : /students/student/firstname
    line number         : 4
    class[1]            : xstream$Students
    converter-type[1]   : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
    version             : 1.4.10

有人知道如何修复它吗?下面是类和解析部分

Students.class

代码语言:javascript
复制
@XStreamAlias("students")
public static class Students{
    @XStreamImplicit(itemFieldName = "student")
    public List<Student> student = new ArrayList<Student>();
}

Student.class

代码语言:javascript
复制
    @XStreamAlias("student")
    public static class Student {
        String firstname;
        String lastname;
        @XStreamImplicit(itemFieldName = "marks")
        private List<Marks> marks;
        @XStreamAlias("student_id")
        @XStreamAsAttribute  
        int student_id;
    }

Marks.class

代码语言:javascript
复制
@XStreamAlias("marks")
public static class Marks{
    int first;
    int second;
    int third;
    int forth;
}

执行的代码

代码语言:javascript
复制
    FileReader fileReader = new FileReader("src/100.xml");  
    Class<?>[] classes = new Class[] { Students.class, Student.class, Marks.class };
    XStream xstream = new XStream();
    xstream.useAttributeFor(Student.class, "student_id");
    xstream.alias("students", Students.class);
    xstream.alias("student", Student.class);
    xstream.alias("marks", Marks.class);
    XStream.setupDefaultSecurity(xstream);
    xstream.allowTypes(classes);  
    Students students = (Students) xstream.fromXML(fileReader);

非常感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-01 23:18:52

在为@XStreamAlias("firstname")类中的相应字段添加Student@XStreamAlias("lastname")之后,请尝试。

已更新

好吧,几个变化..。

1)由于我理解在“学生”中的代码“标记”不是数组,所以将其更改为在“学生”中键入“标记”。

代码语言:javascript
复制
@XStreamAlias("student")
public class Student {
    String firstname;
    String lastname;

    @XStreamAlias("marks")
    private Marks marks;

    @XStreamAsAttribute
    @XStreamAlias("studentid")
    String student_id;
}

2)在代码中,将xstream.alias("student", Student.class);替换为xstream.addImplicitArray(Students.class, "student", Student.class);

尝试使用以下代码读取xml

代码语言:javascript
复制
        Class<?>[] classes = new Class[] { Students.class, Student.class, Marks.class };
        XStream xstream = new XStream();
        xstream.autodetectAnnotations(true);
        xstream.processAnnotations(Students.class);
        Students students = (Students) xstream.fromXML(xml);

希望这能帮上忙。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49603446

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档