• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

xstream annotations for complex java objects (I am using xstream but other approaches are welcome)

 
gabig Gabriel
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


I want to create this xml structure with xstream

<client clientid="id1" name="client name" status="status"> </client>

I have created the structure:

<client>
<clientid> 1 </clientid>
...
</client>

I don't know how to add attributes to a node. I have seen is possible with annotations but I haven't been able to create the structure I want. I am newby to XML parsing so other approaches are ok. I found xstream and found it very easy as a start but now I am lost.

Any help apreciated.

Many thanks
 
g tsuji
Ranch Hand
Posts: 669
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The basic setting to do that is to use the @XStreamAsAttribute. The tutorial has a section on it.
http://xstream.codehaus.org/annotations-tutorial.html
 
gabig Gabriel
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks!!!
 
gabig Gabriel
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again,

Now I am struggling with this structure:

<client clientid="id1" name="client name">
<product name ="name 1"/>
<product name ="name 2"/>
</client>

Is it possible using annotations or should I look at xstremconverter? I am new and I Am sure this is quite simple but can find the answer.
 
g tsuji
Ranch Hand
Posts: 669
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible using annotations or should I look at xstremconverter? I am new and I Am sure this is quite simple but can find the answer.

I wouldn't say the annotations tutorial does not discuss something just like what you're looking for. It does and it does nearly graphically.

If you have some difficulty in digesting all the details to arrive at that, I can understand. I can for this time show you how it is done.

First you can make two classes, one for Client, the other for Product.

[1] Client.java

[2] Product.java

[3] Then you draft a class for testing..., say Test.java.
 
gabig Gabriel
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks! Anyway I think xstream maybe is not the best way to deal with XML. I think there are easier libraries. For simple structures is ok though.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13073
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally, I just write a String toXML(){} method and build the XML representation in a StringBuilder.

On the parsing side I write a constructor that takes an XML element, expecting the representation the toXML method writes.

Bill
 
g tsuji
Ranch Hand
Posts: 669
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyway I think xstream maybe is not the best way to deal with XML.... For simple structures is ok though.

Maybe or maybe not... But wouldn't it be reasonable to know the framework (or at least covering "simple structure") before coming to or tossing such opinion? I am not saying XStream is best (but which framework is, unconditionally?) and its limitation is not a secret: but it has its own place in the serialization biz.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic