1. xml结构
<?xml version="1.0" encoding="UTF-8"?>
<tomails>
<tomail>
<type>java</type>
<toMailaddress></toMailaddress>
<title></title>
<sendExtend>你好</sendExtend>
<psfilename></psfilename>
<fromAddress></fromAddress>
<showName>/showName>
</tomail>
<tomail>
<type>java</type>
<toMailaddress></toMailaddress>
<title></title>
<sendExtend>你好</sendExtend>
<psfilename></psfilename>
<fromAddress></fromAddress>
<showName></showName>
</tomail>
</tomails>
程序中使用:
public static List<SendModel> getSendModel(String path){
XStream xstream = new XStream(new DomDriver());
xstream.alias("tomails", ToMails.class);
xstream.alias("tomail",SendModel.class);
xstream.addImplicitCollection(ToMails.class, "tomaillist");
try {
ToMails mails = (ToMails) xstream.fromXML(new FileReader(new File(path)));
List<SendModel> tomails = mails.getTomaillist();
return tomails;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
其中tomaillist 是Tomails中用来存放SendModel list 的对象名。
xml中的属性必须与对象的属性相一致