Type: New Feature
Affects Version/s: None
Fix Version/s: 1.2
Currently, the xml XStream is created is quite straight forward and sometimes it is producing unnecessary tags for the object properties. For example:
String lastname = "bloh";
public class School
String name = "My school";
int numberOfStudents = 1234;
AFAIK, current XSteam is producing this XML for such classes (lets assume we make necessary aliases)
But if you make primitive and String (and perhaps StringBuffer) properties of a class as attributes of an xml element, resulting xml qould be much shorter. like:
<school name="My School" numberOfStudents="1234">
<student name="blah" surname="bloh" />
i gues it does not disturb the background work of the XStream becuase there still is no configuration file whatsoever. only a set value like "MAKE_PRIMITIVES_ATTRIBUTES" would be enough for the user.
Ive made such a converter, it's not very nice and polished (I have to copy lot's of functionality of the inner working of the original converter)- but it's working. Feel free to use it, and incorporate in the next release
Specifically would be the feature of being able to specify some fields as attributes and some as child elements. It sounds like a lot of work - and it probably is. Off the top of my head I can't think of an elegant way to do this either.
I think it would offer a great amount of utility though.
Possible approach is to allow to specify list of simple types (int, String, Date, BigInteger, etc) that have registered convertors and will be converted into attributes.
modified version of ReflectionConverter that will marshall any type with an AbstractBasicConverter as an attribute
I just started using XStream today. I am very interested in using this extension (anything that can serialize Strings as attributes is good for me) but don't know quite how to go about actually using it.
I guess I need to make AttributeAwareReflectionConverter the default converter.
registerConverter() wants an instance of the Converter but I'm stuck since I've no idea of what the last two parameters for the constructor of
AttributeAwareReflectionConverter should look like.
Anyone got a simple example ?
Just in case anyone is working on this, please note some limitations of attributes because of the XML specification. Attributes are "normalized" to remove any tabs, newlines, or carriage returns. This could cause serious problems if you have a String with those characters that you would like to preserve.
The only solution is to wrap the attributes in [[CDATA]] and doing so might negate most of the saved space of using attributes instead of entities.
For more information see the spec: http://www.w3.org/TR/REC-xml/#AVNormalize
and the source of the MXParser: http://www.extreme.indiana.edu/viewcvs/~checkout~/XPP3/java/src/java/mxp1_min/org/xmlpull/mxp1/MXParser.java (look for the code that deals with tabs in the parseAttribute() method)
This is now available, but you have to register the types to alias yourself with the new XStream.aliasAttribute(alias,type) method. Reopen if this does not meet your requirements.
I also need such a feature. Preferably I would like to be able to register aliases for an element to specify that the instance vars should be attributes, not child elements.
I have looked at writing a new StringConverter, but it seems more complicated than that as there is something else that builds the xml String. If someone gives me a pointer I'll contribute the actual work.