Xstream: removing class attribute
Asked Answered
A

5

14

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

I use Xstream with annotations

Arenaceous answered 5/1, 2010 at 17:59 Comment(2)
Where are you getting class="..." exactly? Can you post some generated XML?Rimple
markmail.org/message/t33x7hswlwsk766iKessiah
P
14

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.

Phile answered 23/2, 2010 at 23:23 Comment(2)
<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.Blockish
Is there a spring oxm alternative available for this?Floor
C
22

I read its code and found if your class is not mapper.defaultImplementationOf(fieldType) , it will add the default class attribute for you, unless the class attribute name is null;

So, set this can remove the class=”Something ” attributes

 xstream.aliasSystemAttribute(null, "class");
Cumberland answered 24/4, 2011 at 16:1 Comment(1)
Is there a spring oxm alternative available for this?Floor
P
14

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.

Phile answered 23/2, 2010 at 23:23 Comment(2)
<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.Blockish
Is there a spring oxm alternative available for this?Floor
V
2

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();
}
Vidal answered 20/4, 2010 at 13:25 Comment(0)
J
2

This attribute is shown, at least, when it's not obvious which class shall be used. Usage of interface is an example. In situations like that You can try:

xStream.addDefaultImplementation(YourDefaultImplementation.class, YourInterface.class);

.

Jeremyjerez answered 6/5, 2015 at 0:41 Comment(0)
W
1

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://x-stream.github.io/alias-tutorial.html for more in-depth coverage. Again, paste in some sample output.

Wiggle answered 6/1, 2010 at 19:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.