Ok, I modified the code to the following custom marshalling strategy. I am
able get the reference to the first object resolved, but not the second.
I.e:
<typeA reference="../../../typeA[1]"/> works, but
<typeA reference="../,./../typeA[2]"/> gives invalid reference.
public class ReusingReferenceByXPathMarshallingStrategy implements
MarshallingStrategy
{
//~ Instance fields
----------------------------------------------------------
private ReferenceByXPathMarshaller marshaller;
private ReferenceByXPathUnmarshaller unmarshaller;
//~ Methods
------------------------------------------------------------------
@Override
public void marshal(HierarchicalStreamWriter writer, Object obj,
ConverterLookup converterLookup, Mapper mapper, DataHolder dataHolder)
{
if (marshaller == null)
{
marshaller = new ReferenceByXPathMarshaller(writer, converterLookup,
mapper, ReferenceByXPathMarshallingStrategy.RELATIVE);
}
marshaller.start(obj, dataHolder);
}
@Override
public Object unmarshal(Object root, HierarchicalStreamReader reader,
DataHolder dataHolder, ConverterLookup converterLookup, Mapper mapper)
{
if (unmarshaller == null)
{
unmarshaller = new ReferenceByXPathUnmarshaller(root, reader,
converterLookup, mapper);
}
return unmarshaller.start(dataHolder);
}
}
On Fri, Feb 17, 2012 at 8:52 AM, Jörg Schaible <[email protected]>wrote:
> Hi Michael,
>
> Michael Mogley wrote:
>
> > 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);
> > }
> > }
>
> The MarshallingStrategy is still the old one, so references do not work
> between the calls. There's no real difference to your first code snippet.
> You have to look at the last test that uses the custom MarshallingStrategy.
>
> Cheers,
> Jörg
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
> http://xircles.codehaus.org/manage_email
>
>
>