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 Servlets and the fly likes Can a servlet make XML tags on the fly...? 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 » Java » Servlets
Bookmark "Can a servlet make XML tags on the fly...?" Watch "Can a servlet make XML tags on the fly...?" New topic

Can a servlet make XML tags on the fly...?

Gill Clover

Joined: Aug 10, 2002
Posts: 28
Hi there folks,
I'm not sure if I should be posting this question here or in the XML forum, sorry if I am posting to the wrong place.
I'm very new to XML - have a grasp for what it can do but have never properly used it. Been learning Java for a few years but have only recently been learning about servlets.
What I'd like to be able to do is code a servlet to generate an undetermined number of XML *tags* on the fly, based on a resultset obtained by querying a database.
For example, I have a table called 'unit' in Oracle (a 'unit' is just a chunk of text). The text contained in the unit is in a field called 'unittext', and each unit also has a name ('unitname').

Assuming I have queried this table from my servlet and got back say, three unitnames and their associated unittext:
UnitName UnitText
greeting Hello there!
contact Joe Bloggs
location Scotland
I'd like to know if it is possible for my servlet to *make XML tags* from each of the unitnames (and then insert the corresponding unittext in between the tags).
So the output that my servlet would generate would look like this:
<greeting>Hello there!</greeting>
<contact>Joe Bloggs</contact>
Any answers/help/pointers to tutorials would be gratefully recieved.
Thanks in advance,
Gillian Klee
Claudio Gualberto
Ranch Hand

Joined: Oct 13, 2002
Posts: 47
hi Gillian,
I've done something like this on a session manager Servlet, but i did it by hand, i mean i wrote all the code that generates the output.
I just think that there is no suport in the Java XML API's (JAXP) that could help you, but you can search in the SAX or DOM API's for something that generates XML trought java objects.
Claudio Gualberto.
Gill Clover

Joined: Aug 10, 2002
Posts: 28
Thanks for your reply. Can you give me an example of how you coded this by hand? That's what I was planning on doing since I didn't think there'd be any ready-made classes/methods to do this.
I'm just a bit out of my depth here since I don't know much XML, but am working on my dissertation and need to find out quickly if this strategy I've come up with is feasible.
Also, once I've coded the servlet to output XML, I will need to use XSLT to output HTML. All I want to end up with is a very simple webpage which displays these 'units' one after another down the page. I was thinking it'd be possible for the XSLT to look at my dynamically generated XML tags and just produce HTML which would display the units on the webpage as described above, but now I'm not so sure. Did you do something like this with your XML document?
Kind regards,
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8903

There is a ResultBuilder class from JDOM. This may help u.
[ October 27, 2002: Message edited by: Pradeep Bhat ]

William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12756
Let me suggest that you put the xml generation and xslt transformation steps in helper classes, NOT in a servlet derived class. You will find it MUCH easier to debug in an off-line environment.
There are lots of ways to generate XML - you can just write the text of a full XML document with print statements to a stream, create a DOM in memory and then serialize it, or create SAX events as if an XML document was being read by a SAX parser.
The Apache Cocoon project is all about generating web pages from XML followed by XSLT transformation. Cocoon is probably more than you want to get into at this stage of your XML programming.
Gill Clover

Joined: Aug 10, 2002
Posts: 28
Thanks very much for your input, it's very much appreciated and has given me a good start!!
Fred Grott
Ranch Hand

Joined: Oct 05, 2002
Posts: 346
Originally posted by Gillian Klee:
Thanks very much for your input, it's very much appreciated and has given me a good start!!

and then there are helper classes like TinyXML as well..since you wnat only non validating xml here its a simpel way to do it..

MobileBytes blog - Sharing Technology - My Programming Knols
jQuery in Action, 2nd edition
subject: Can a servlet make XML tags on the fly...?
Similar Threads
How to generate HTML to display XML
HTML content for a Markup
ClasspathResource with property placeholders?
javax.naming.NamingException: Could not dereference object
XML files to Excel Files