• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JSP and XML

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
i am new to JSP and need ur help.
on the client end, i have created a HTML form. now, on the server, i want to use JSP to create a XML file that contains the user selections made via the HTML form. this XML file is later used by other servlets in the server to extract data.
how can this be done?
thanks in advance.
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use JAXP API or XML API by the Apache Xerces package to create DOM document.
Below shows an example for using JAXP. JAXP is the API provided with J2SE 1.4.
You can create a new DOM object by:

Then, by getting the data from your HTML form, we can:

Does this what you need?
Nick.
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forget to say, you need to import the following packages:
org.w3c.dom.*;
After creating the XML DOM object, you can then forward it to the Servlets, or JSPs for further processing.
Nick.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you found a way to actually write out the XML document using the Sun API?
Xerces offers a nice API for doing that, so you won't have to iterate over all the elements by hand and write them:

doc is a DocumentImpl
f is a File
logger is a Logger
I set the line separator to Windows format here for my own sanity.
The files will in practice be used only by the machine (mostly Unix machines) but during testing I want to look at them myself and I'm using Windows as a workstation (with the Java stuff running on Unix so not forcing it to Windows would yield invalid line separators when opening the XML in Windows).
 
Yell Srik
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI friends,
thank u very much for the quick reply. i feel i probably need a little more clarification. actually in the HTML form, instead of specific values like "name","age" etc, i have included an entire text file ie the tag looks like:
<selct name="sel" onchange="loadfile()">
<option value="D:\program\aaa.txt>file1
<option value="D:\program\bbb.txt>file2
</select>
now i want the generated XML file to have either 'file1' or 'file2' depending on what option the user selects. then the JSP needs to extract data from either aaa.txt or bbb.txt
hope u understand what i need....
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case, you may need to have one more hidden field for uploading the file to the server before you send out the request.
This is because, even if a JSP get the value of "sel" as "C:\xxx\yyy.zzz", the JSP cannot read the file from your harddisk, cos JSP is resided in remote, not your machine!
You should have a html form with the attribute enctype="multipart/form-data", such that the file will be uploaded.
I dont know how JSP solely can perform the upload, as I use Struts to perform the upload. So, when Struts get the form, it knows where the file is located at the server, and use the info to get back the file.
Nick.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic