Thanks, Jörg! That was it. One thing I noticed is the referencing only
works if I unmarshal from the root node. If I descend into any of the
children (moveDown), I again get the 'invalid reference' error. Not sure
if this is by design. For anyone having a similiar issue, my new working
code is below:
public static void main(String[] args)
{
try
{
String xml = StringUtils.join(new String[] {
"<list>",
" <typeA/>",
" <typeA/>",
" <typeB>",
" <items>",
" <typeA reference=\"../../../typeA[2]\"/>",
" </items>",
" </typeB>",
"</list>"
}, "\n");
// Read objects.
XStream xstream = new XStream();
xstream.alias("typeA", TypeA.class);
xstream.alias("typeB", TypeB.class);
HierarchicalStreamReader xstreamReader = new XppReader(new
StringReader(xml), XmlPullParserFactory.newInstance().newPullParser());
for (Object nextObject : (List)xstream.unmarshal(xstreamReader))
{
LOG.info("read: {}", nextObject);
}
}
catch (Throwable t)
{
LOG.error(t.getMessage(), t);
}
}
Michael