views:

652

answers:

3

How do I remove the class=”Something ” attributes in Xstream .

I use Xstream with annotations

+1  A: 

Can you give some example output? I think this usually happens when using Collections. Without seeing the output, my best guess is that you need to register aliases:

xstream.alias("blog", Blog.class);

See http://xstream.codehaus.org/alias-tutorial.html for more in-depth coverage. Again, paste in some sample output.

Dave
+2  A: 

Indeed the problem is not as clearly phrased as it should. My guess is that you are using a non-standard collection or using a field of an interface type for which XStream needs to store the actual class.

In the second case you can just use alias:

xstream.alias("field name", Interface.class, ActualClassToUse.class);

See http://markmail.org/message/gds63p3dnhpy3ef2 for more details.

Christopher Oezbek
<edit> I had a similar problem and this turned out to be the problem. I was worried that I would have to use alias javacode instead of annotations, but they turned out to complement eachother nicely as I can determine the actual class to use at runtime.
Caoilte
+1  A: 

Use something of this sort to remove the class attribute completely rather than aliasing it with something else:

private String generateResponse(final XStream xStream)
{
    StringWriter writer = new StringWriter();
    xStream.marshal(this, new PrettyPrintWriter(writer) {
        @Override
        public void addAttribute(final String key, final String value)
        {
            if (!key.equals("class"))
            {
                super.addAttribute(key, value);
            }
        }
    });
    return writer.toString();
}
MiKu