This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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
internet detective

Joined: May 26, 2003
Posts: 30068

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.

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, 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: 41063
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!

Ping & DNS - my free Android networking tools app
I agree. Here's the link:
subject: constructing xml through java bean
Similar Threads
java bean to xml
conversion from form bean to XML
How to convert xml file to javax.xml.soap.SOAPElement [ ] array?
To create an xml from a java bean