XStream - com.thoughtworks.xstream.converters.ConversionException
XStream - com.thoughtworks.xstream.converters.ConversionException
我正在尝试解析 XML 文档,这是文档的结构
<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>
但是我遇到了这个错误
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
@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;
}
执行代码
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);
非常感谢!
请在 Student
class.
中的相应字段添加 @XStreamAlias("firstname")
和 @XStreamAlias("lastname")
后尝试
[更新]
好的,一些变化...
1) 据我了解,您在 Student 中的代码 'marks' 不是数组,因此请将其更改为在 Student 中键入 'Marks'。
@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
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);
希望对您有所帮助。
我正在尝试解析 XML 文档,这是文档的结构
<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>
但是我遇到了这个错误
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
@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;
}
执行代码
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);
非常感谢!
请在 Student
class.
@XStreamAlias("firstname")
和 @XStreamAlias("lastname")
后尝试
[更新]
好的,一些变化...
1) 据我了解,您在 Student 中的代码 'marks' 不是数组,因此请将其更改为在 Student 中键入 'Marks'。
@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
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);
希望对您有所帮助。