wood burning stoves 2.0*
The moose likes XML and Related Technologies and the fly likes XStream + variable as an attribute to another variable which is tag. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XStream + variable as an attribute to another variable which is tag. " Watch "XStream + variable as an attribute to another variable which is tag. " New topic
Author

XStream + variable as an attribute to another variable which is tag.

Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 753
Hi

I am using XStream API For Object to XML Conversion.

Bellow is my

POJO




and i am getting the following output in XML



but i want the <name> as an attribute to <userdata> tag.

like bellow.



can anybody help me out solving it.
I am clueless.


Jigar Naik


Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
you should add to your xstream instance the following line of code



Omar Al Kababji - Electrical & Computer Engineer
[SCJP - 90% - Story] [SCWCD - 94% - Story] [SCBCD - 80% - Story] | My Blog
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 753
Ya that i tried but then it gave me the bellow output which is logically correct, but I want the name attribute to the <userdata> node.

Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
I think that you can't do that
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 753
all right.. Thanks for reply...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

As a guess: you would have to create a UserData class to hold that "name" attribute.
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
Yes Paul in this case of-course it would work ;)
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 753
Nope, It does not work

Bellow is the output






Any :idea: :?:

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You are calling item.setUserData and item.setUserdata??? Besides being confusing to people who might have to maintain your code in the future, that sets two different things.

Besides, you don't even have your data model straight yet. You want your <userdata> element to contain that URL as text and to have a "name" attribute. So the Userdata object ought to contain those data items, no? Why would you put one of them in the Item object and expect it to come out in the <userdata> element?
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
Yes it works but remove the userData object of type string and if you need some other information encloseit in the UserData object this way it should work, if i didn't understond wrong please providean xml thaylt shows what is exactly your desired output.
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 753
I think either i am too dumb or there is a missudnerstanding..

I am trying to get the following xml



If you dont mind,can you please make changes in my main method which is as bellow


currently giving me following output



I am still wondering how it is possible. :?

Thanks...
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
try using this code





Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 753
Hi Omar,

Thanks a lot for the code.

I executed the code, and it gave me the bellow output.



instead of



I think what i want is not posible using XStream.

Anyways... Thanks a lot for your time.
Regards,
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
Hi again nothing is impossible, you should use a Converter llike this code:




and then add this Converter to your xstream instance




Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 753


Mannn... you did it... you are right.. nothing is impossible... quite motivating reply...

Thanks a lot omar.. .

Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
Welcome any time ;)
uthaman udayakumar
Greenhorn

Joined: Sep 25, 2008
Posts: 3
Hi Omar,

This converter can be used only for the UserData class, what if i have some more nodes with the same case, do i have to write seperate converters for and every nodes.. or is there any way can we genralize this converter... Please see below for the sample xml for this case...


Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
Hi, you should write a converter for each separate class, however if you make both classes extend the same base class, that contains the name attribute, i think that you can avoid writing a converter for each class.
uthaman udayakumar
Greenhorn

Joined: Sep 25, 2008
Posts: 3
Thanks Omar,

it worked...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XStream + variable as an attribute to another variable which is tag.
 
Similar Threads
Append child + Xstream
Spring 3.0 + REST Annotations
Problem while Tag lib Up gradation
Xml File generation - xstream
Problem in using xstream annotations for complex java objects