aspose file tools*
The moose likes XML and Related Technologies and the fly likes  xstream annotations for complex java objects (I am using xstream but other approaches are welcome) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark " xstream annotations for complex java objects (I am using xstream but other approaches are welcome)" Watch " xstream annotations for complex java objects (I am using xstream but other approaches are welcome)" New topic
Author

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

gabig Gabriel
Greenhorn

Joined: Oct 19, 2012
Posts: 6
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

Joined: Jan 18, 2011
Posts: 535
    
    3
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

Joined: Oct 19, 2012
Posts: 6
Many thanks!!!
gabig Gabriel
Greenhorn

Joined: Oct 19, 2012
Posts: 6
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

Joined: Jan 18, 2011
Posts: 535
    
    3
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

Joined: Oct 19, 2012
Posts: 6
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

Joined: Mar 22, 2000
Posts: 12803
    
    5
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

Joined: Jan 18, 2011
Posts: 535
    
    3
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: xstream annotations for complex java objects (I am using xstream but other approaches are welcome)