XML:
public class item {
private String user;
private String pw;
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPw() {
return pw;
}
public void setPw(String pw) {
this.pw = pw;
}
}
public class root {
private ArrayList root;
public ArrayList getBooks() {
return root;
}
public void setBooks(ArrayList root) {
this.root = root;
}
}
public class Book {
private String name;
private String author;
private String date ;
private Info info;
public String getName() {
return name;
}
public Info getInfo() {
return info;
}
public void setInfo(Info info) {
this.info = info;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
public class XStreamDemo {
public static void main(String[] args) {
XStreamDemo aa = new XStreamDemo();
aa.xmlToJavaBean();
}
public void xmlToJavaBean(){
File file = new File(this.getClass().getClassLoader().getResource("")
.getPath() + "books.xml");
String s= file.getPath();
XStream stream = new XStream(new CDATAXppDriver());
stream.alias("item",item.class);
stream.alias("book",Book.class);
stream.alias("root",root.class);
stream.addImplicitCollection(root.class, "root");
root books = null;
books = (root) stream.fromXML(file);
ArrayList bookList = books.getBooks();
System.out.println(s);
for(int i=0;i<bookList.size();i++){
Book book = (Book) bookList.get(i);
System.out.println("name��"+book.getName()+"��"+"author:"+book.getAuthor()+"��"+"date:"+book.getDate());
}
}
}
之前 xml 里,每个下只有一个,且 class Info 这样定义时,
private item item 时,是没有错误的,可是当下有多个标签时,相应的类 class info 我也改成了 privte ArrayList item ,时,程序就报错了,内容如下:Exception in thread "main" com.thoughtworks.xstream.converters.ConversionException: user : user
---- Debugging information ----
message : user
cause-exception : com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message : user
class : java.util.ArrayList
required-type : java.util.ArrayList
converter-type : com.thoughtworks.xstream.converters.collections.CollectionConverter
path : /root/book/info/item/user
line number : 9
class[1] : Book.Info
converter-type[1] : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
class[2] : Book.Book
class[3] : Book.root
version : 1.4.8
求高手帮忙。!!!!!!
xstream解析复杂Xml,xml 分为大的两层
XML:
图片说明