pixie
pixie

Reputation: 517

null values when deserializing xstream

I have a problem dealing with null values when converting an XML file to an Object.

I have the following XML input:

<?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>

And I want to convert this into an Object called tvSeries, this way:

XStream xstream = new XStream();
xstream.alias("Results", TVSeries.class);
xstream.alias("show", Show.class);
tvSeries = (TVSeries) xstream.fromXML(file);

where the class TVSeries.java has the following content:

public class TVSeries {

private ArrayList<Show> showList;

public TVSeries(){
    showList = new ArrayList<>();
}

public int size(){
    return showList.size();
}

}

and the class Show.java the following content:

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;
}
}

Now the problem I have is that my Object is always null. I don't have that much experience with XStream so a little help would be very useful.

Thank you

Upvotes: 2

Views: 3252

Answers (2)

Gerrit
Gerrit

Reputation: 435

Use JAXB to marshall and unmarshall you xml data. and use http://docs.oracle.com/javase/6/docs/technotes/tools/share/xjc.html to create your java classes using the xsd schema:

create MyXML class: xjc -d src -pcom.foo.myapp MyXML.xsd

to unmarshall xmlFile to MyXML object:

MyXML myXML = JaxbMarshalUnmarshalUtil.unmarshal( xsdFile, xmlFile, MyXML.class );

And side note: if you don't have a schema you are doin it wrong!

Upvotes: 1

atomman
atomman

Reputation: 2506

After some trail and error I found by adding these (and fixing the xml of course) worked.

xstream.alias("Results", TVSeries.class);
xstream.alias("show", Show.class);
xstream.addImplicitCollection(TVSeries.class, "showList");
xstream.aliasType("genre", String.class);

Just some extra hint when using XStream, if you find yourself in these kind of situation it often helps building a test structure in java and serialize it to XML just to see what the xml looks like.

Upvotes: 3

Related Questions