This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jaxb be converting bean to xml

 
James Peter
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am using jaxb to convert a bean to xml string. below is my sample code.
my requirement is to make a generic method to which i can pass any type of bean class. for example below one is having student bean and i want to send employee bean also. I would like to know what is the best approach to handle this. Please let me know if anyone is having solution for this.



Rgds
Peter
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add a toXML() method to each bean class and when you want a XML-based representation of a bean, simply call toXML() to get one.


 
James Peter
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Thanks for reply,

do you mean like the below one.



if yes then i am getting error "The method toXML() is undefined for the type Address"

Rgds
Peter
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add a toXML() method to each bean class ...


You have not added a toXML() method to the Address class. You can't call a method if it does not exist.
 
James Peter
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that means i need to add this methods for each bean class.
But my requirement is to use a generic method which can be used by all bean classes.
any solution or technique for this?

Rgds
Peter
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are missing something. By adding a toXML() method to each bean class, you will then be able to easily create a generic method to call. This generic method will be called by client objects to get the XML representation of the beans...generically.

The generic method and the toXML() method are NOT the same.

my requirement is to make a generic method to which i can pass any type of bean class.


In other words, if each bean class has a toXML() method, then you can create a "generic method to which you can pass any type of bean [instance]" and get an XML-based representation.
 
James Peter
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

If possible please provide me sample code of generic method or any source so that i can understand it clearly and proceed.

Thanks in advance
Peter
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Example of generic method:




Example of a bean's toXML() method:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic