aspose file tools*
The moose likes XML and Related Technologies and the fly likes jaxb be converting bean to xml 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 "jaxb be converting bean to xml" Watch "jaxb be converting bean to xml" New topic
Author

jaxb be converting bean to xml

James Peter
Ranch Hand

Joined: Sep 15, 2010
Posts: 115
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

Joined: Apr 16, 2008
Posts: 2187
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

Joined: Sep 15, 2010
Posts: 115
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

Joined: Apr 16, 2008
Posts: 2187
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

Joined: Sep 15, 2010
Posts: 115
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

Joined: Apr 16, 2008
Posts: 2187
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

Joined: Sep 15, 2010
Posts: 115
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

Joined: Apr 16, 2008
Posts: 2187
Example of generic method:




Example of a bean's toXML() method:

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: jaxb be converting bean to xml