File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes constructing xml through java bean 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 "constructing xml through java bean" Watch "constructing xml through java bean" New topic

constructing xml through java bean

srinu reddy

Joined: Oct 21, 2007
Posts: 12
I have java bean in session, please, let me kow how can i construct a xml file with ben properties and values as xml nodes.
which is the best approach to take.
i have to do it in jsp or in a class.
provide me details.

Thanks & Regards
srinivas reddy
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33107

You want to do this in a Java class, not in the JSP. That would be way too much logic in a JSP.

It's just a matter of writing a method on the java bean that writes out XML. You need to decide on what the XML should look like. What should be the node names, etc.

If you are just doing this once, it is easiest to hand code. If you are doing it a lot, there are Java/XML mapping libraries that can generate the XML.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Pavel Cherkashin
Ranch Hand

Joined: Mar 04, 2005
Posts: 47
Have something to add.
If there is no requirement to xml structure and the main idea - convert bean to xml and back populate to bean, then the XStream is very usable. Refer this two minutes tutorial
May be that is the best choice in your situation.

Pavel Cherkashin - <br />SCJP, SCWCD, SCDJWS, SCBCD, SCEA, ...<br />
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
If the beans truely conform to the JavaBeans spec, then the easiest would be to use the java.beans.XMLEncoder/XMLDecoder classes. No extra library to use and learn - very handy!
I agree. Here's the link:
subject: constructing xml through java bean
It's not a secret anymore!