History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: XSTR-62
Type: New Feature New Feature
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: Unassigned
Reporter: Ahmet A. Akin
Votes: 13
Watchers: 8
Operations

If you were logged in you would be able to see more operations.
XStream

Make an option to create primitive and String properties as attributes.

Created: 09/May/04 09:43 PM   Updated: 07/Aug/06 09:34 PM
Component/s: None
Affects Version/s: None
Fix Version/s: 1.2

File Attachments: 1. Java Source File AttributeAwareReflectionConverter.java (14 kb)
2. Java Source File CompactReflectionConverter.java (8 kb)

Issue Links:
Related


 Description  « Hide
Currently, the xml XStream is created is quite straight forward and sometimes it is producing unnecessary tags for the object properties. For example:

class Student
{
  String name="blah";
  String lastname = "bloh";
}

public class School
{
   String name = "My school";
   int numberOfStudents = 1234;
   List students;
}

AFAIK, current XSteam is producing this XML for such classes (lets assume we make necessary aliases)

<school>
  <name>My School</name>
  <numberOfStudents>1234</numberOfStudents>
  <students>
    <student>
      <name>blah</name>
      <surname>bloh</name>
    </student>
    ....
  </students>
</school>

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">
  <students>
    <student name="blah" surname="bloh" />
    ....
  </students>
</school>

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.

 All   Comments   Change History      Sort Order: Ascending order - Click to sort in descending order
Brian Alexander Lee - 19/Aug/04 12:08 PM
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.

Zsombor G. - 09/Sep/04 06:06 PM
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 ;-)

Zsombor G. - 09/Sep/04 06:07 PM
The converter

Arron Ferguson - 29/Jan/05 08:34 AM
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.




Arron

Eugene Kuleshov - 12/Mar/05 12:18 AM
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.

J. Matthew Pryor - 30/Mar/05 08:38 AM
modified version of ReflectionConverter that will marshall any type with an AbstractBasicConverter as an attribute

Richard Sullivan - 27/Jun/05 09:59 AM
hi folks,

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 ?
Thanks,
Richard



Scott McFarland - 19/Dec/05 04:49 PM
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)

Joerg Schaible - 03/Mar/06 03:43 PM
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.