简体   繁体   中英

XStream - com.thoughtworks.xstream.converters.ConversionException

I'm trying to parse an XML document, here's the structure of the document

    <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>

but I'm getting this error

    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

Does anyone know how to fix it, below are the classes and the parsing part

Students.class

@XStreamAlias("students")
public static class Students{
    @XStreamImplicit(itemFieldName = "student")
    public List<Student> student = new ArrayList<Student>();
}

Student.class

    @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

@XStreamAlias("marks")
public static class Marks{
    int first;
    int second;
    int third;
    int forth;
}

executed code

    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);

Thanks a lot!

please try after adding @XStreamAlias("firstname") and @XStreamAlias("lastname") for the respective fields in the Student class.


[updated]

okay, couple of changes...

1) As I understand your code 'marks' in Student is not an array, so change it to type 'Marks' in Student.

@XStreamAlias("student")
public class Student {
    String firstname;
    String lastname;

    @XStreamAlias("marks")
    private Marks marks;

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

2) In your code, replace xstream.alias("student", Student.class); with xstream.addImplicitArray(Students.class, "student", Student.class);

or

try this code to read xml

        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);

hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM