aspose file tools*
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 Java 8 in Action this week in the Java 8 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
Author

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

Gill Clover
Greenhorn

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>
<location>Scotland</location>
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.
Brazil.
Gill Clover
Greenhorn

Joined: Aug 10, 2002
Posts: 28
Claudio,
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,
Gillian
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8898

There is a ResultBuilder class from JDOM. This may help u.
http://otn.oracle.com/oramag/oracle/02-sep/o52jdom.html
[ October 27, 2002: Message edited by: Pradeep Bhat ]

Groovy
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12683
    
    5
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.
Bill


Java Resources at www.wbrogden.com
Gill Clover
Greenhorn

Joined: Aug 10, 2002
Posts: 28
Thanks very much for your input, it's very much appreciated and has given me a good start!!
Gillian
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!!
Gillian

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can a servlet make XML tags on the fly...?
 
Similar Threads
HTML content for a Markup
XML files to Excel Files
How to generate HTML to display XML
javax.naming.NamingException: Could not dereference object
ClasspathResource with property placeholders?