My XML:
<autocomplete>
<url_template>http://api-public.netflix.com/catalog/titles/autocomplete?{-join|&|term}</url_template>
<autocomplete_item>
<title short="Star Wars: Episode V: The Empire Strikes Back: Original Theatrical Version"></title>
</autocomplete_item>
</autocomplete>
My Objects:
public class AutoCompleteList
{
public String url_template;
public List<AutocompleteItem> autocomplete_item;
}
public class AutocompleteItem
{
public Title title;
}
public class Title
{
@XStreamAlias("short")
public String Short;
}
My code:
XStream xstream = new XStream();
xstream.alias("autocomplete", AutoCompleteList.class);
xstream.alias("title", Title.class);
AutoCompleteList myObj = (AutoCompleteList)xstream.fromXML(stringFromStream);
I am unable to retrieve the "title short" value from the XML.
Also, if my XML has more than one set of autocomplete_item tags, xstream errors out complaining that there is a duplicate instance of autocomplete_item.
Any suggestions?
I have searched through the many questions here but nothing seemed to work for me.
For the short
attribute, try adding an @XStreamAsAttribute
annotation:
public class Title
{
@XStreamAlias("short")
@XStreamAsAttribute
public String Short;
}
and call xstream.processAnnotations(Title.class)
before you call fromXML
.
For the multiple autocomplete_item
issue you should use @XStreamImplicit
public class AutoCompleteList
{
public String url_template;
@XStreamImplicit(itemFieldName="autocomplete_item")
public List<AutocompleteItem> autocomplete_item;
}
Again you'll need to call xstream.processAnnotations(AutoCompleteList.class)
to tell XStream to read the annotations before you can call fromXML
.