I've defined my xstream like so:
public static final String listToXMLTree(List<?> list,
Class<?> domainClass, Converter evtConverter ) {
String xml = "";
StringBuffer buff = new StringBuffer(1000);
buff.append("<?xml version='1.0' encoding='iso-8859-1'?>");
XStream xstream = new XStream(new DomDriver());
if (list != null && list.size() > 0) {
xstream.registerConverter(evtConverter);
xstream.alias("rows", List.class);
xstream.alias("row", Event.class );
xstream.aliasField("child", Event.class, "hasChildren");
xml = xstream.toXML(list);
} else {
buff.append("<rows/>");
}
xml = buff.append(xml).toString();
System.out.println(xml);
return xml;
}
But the xml that pops out doesn't have any alias for the "hasChildren" variable - why so? The xml looks like this:
<?xml version='1.0' encoding='iso-8859-1'?>
<rows>
<row id="Puerto Rico692014-04-30 00:00:00.02014-07-29 00:00:00.0" xmlkids="1">
<cell></cell>
<cell>Puerto Rico</cell>
<cell>103415</cell>
</row>
</rows>
EDIT
This is the event class that I have - (on a seperate note I tried using the XStream aliases and removed the code above that creates them manually but it didn't work either):
public class Event
{
// Event parameters
private String region;
private boolean hasChildren;
public boolean isHasChildren() {
return hasChildren;
}
public void setHasChildren(boolean hasChildren) {
this.hasChildren = hasChildren;
}
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
}
The evtConverter
is a converter that maps the xml that Xstream spits out onto a DHTMLx grid.
Thanks
Credit for this answer goes to Vertex - the converter needed to be added after the aliases in order to work properly - I actually refactored my answer and have the object making use of the Xstream annotations instead of defining them in the method. Here's the code:
/**
* Method to convert list objects to XML using XStream API.
* <p>
*
* @param list
* @param domainClass
* @param columnIds
*/
public static final String listToXML(List<?> list, Class<?> domainClass,
Converter converter) {
String xml = "";
StringBuffer buff = new StringBuffer(1000);
buff.append("<?xml version='1.0' encoding='UTF-8'?>");
XStream xstream = new XStream(new DomDriver());
if (list != null && list.size() > 0) {
xstream.alias("rows", List.class);
xstream.processAnnotations(domainClass);
xstream.registerConverter(converter);
xml = xstream.toXML(list);
} else {
buff.append("<rows/>");
}
xml = buff.append(xml).toString();
return xml;
}