This week's giveaway is in the Java/Jakarta EE forum. We're giving away four copies of Java EE 8 High Performance and have Romain Manni-Bucau on-line! See 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
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.
posted 15 years ago
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
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
posted 15 years ago
Thanks very much for your input, it's very much appreciated and has given me a good start!! Gillian