将 XML 文件转换为对象时,我在处理空值时遇到问题。
我有以下 XML 输入:
<?xml version="1.0" encoding="UTF-8" ?>
<Results>
<show>
<showid>10353</showid>
<name>Film Buff Of The Year</name>
<link>http://www.tvrage.com/shows/id-10353</link>
<country>UK</country>
<started>1982</started>
<ended>1986</ended>
<seasons>1</seasons>
<status>Canceled/Ended</status>
<classification>Game Show</classification>
<genres></genres>
</show>
<show>
<showid>2930</showid>
<name>Buffy the Vampire Slayer</name>
<link>http://www.tvrage.com/Buffy_The_Vampire_Slayer</link>
<country>US</country>
<started>1997</started>
<ended>2003</ended>
<seasons>7</seasons>
<status>Canceled/Ended</status>
<classification>Scripted</classification>
<genres><genre>Action</genre><genre>Adventure</genre><genre>Comedy</genre<genre>Drama</genre<genre>Mystery</genre><genre>Sci-Fi</genre></genres>
</show>
</Results>
我想通过这种方式将其转换为一个名为 tvSeries 的对象:
XStream xstream = new XStream();
xstream.alias("Results", TVSeries.class);
xstream.alias("show", Show.class);
tvSeries = (TVSeries) xstream.fromXML(file);
其中 TVSeries.java 类具有以下内容:
public class TVSeries {
private ArrayList<Show> showList;
public TVSeries(){
showList = new ArrayList<>();
}
public int size(){
return showList.size();
}
}
和类 Show.java 如下内容:
public class Show {
private String showid, name, link, country, started, ended, seasons,status,classification;
ArrayList<String> genres;
public Show(){
genres = new ArrayList<>();
}
public Show(String showid, String name, String country, String status, String link, String started, String ended, String classification, String seasons, ArrayList<String> genres){
this.showid = showid;
this.name = name;
this.country = country;
this.status = status;
this.link = link;
this.started = started;
this.ended = ended;
this.seasons = seasons;
this.classification = classification;
this.genres = genres;
}
}
现在我遇到的问题是我的对象始终为空。我对 XStream 没有太多经验,所以一点帮助会非常有用。
谢谢