Guillaume Belrose
Guillaume Belrose

Reputation: 2518

Add attribute when serializing object with XStream

I am using XStream to serialize Java objects to XML. Is it possible to customize XStream so that when it serializes an object it inserts an attribute in the root XML element?

Let's say I have

class A{
 int foo = 1;
}

I want XStream to serialize instances of A to look like:

<A type="text/xml">
 <foo>1</foo>
</A>

Where the attribute text/xml is automatically added to the root element.

My use case is serializing my java object and insert it as the content element inside Atom entry documents. The end result would look like:

<feed>
<content type="text/xml">
<foo>1</foo>
</content>
</feed>

I do not require being able to unmarshall the feed. I need a generic solution that is agnostic to the class of the object I am serializing.

Can I achieve this with XStream?

Upvotes: 0

Views: 1360

Answers (1)

Guillaume
Guillaume

Reputation: 5553

The only way are the the XStream.useAttributeFor(...) methods.

This would force you to configure XStream for each object type you are using though, thus not agnostic.

So I don't think XStream is the tool you need.

Upvotes: 1

Related Questions