• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should add to your xstream instance the following line of code

 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that you can't do that
 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
all right.. Thanks for reply...
 
Paul Clapham
Sheriff
Posts: 20992
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a guess: you would have to create a UserData class to hold that "name" attribute.
 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Paul in this case of-course it would work ;)
 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope, It does not work

Bellow is the output






Any :idea: :?:

 
Paul Clapham
Sheriff
Posts: 20992
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try using this code





 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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

Thanks a lot omar.. .

 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome any time ;)
 
uthaman udayakumar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Omar,

it worked...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic