XMLファイルをオブジェクトに変換するときにnull値を処理する際に問題が発生します。
次の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;
}
}
今私が抱えている問題は、私のオブジェクトが常にnullであるということです。私はXStreamの経験があまりないので、少しの助けが非常に役に立ちます。
ありがとうございました