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 JSP and the fly likes JSP and XML 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 » JSP
Bookmark "JSP and XML" Watch "JSP and XML" New topic


Yell Srik
Ranch Hand

Joined: Mar 10, 2004
Posts: 61
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.
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
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?

SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
Forget to say, you need to import the following packages:
After creating the XML DOM object, you can then forward it to the Servlets, or JSPs for further processing.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
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

Joined: Mar 10, 2004
Posts: 61
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
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

Joined: Nov 07, 2003
Posts: 4982
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.
It is sorta covered in the JavaRanch Style Guide.
subject: JSP and XML
Similar Threads
how to process jsp as xml
is this possible in XML?
using servlets
Editable XML-PDF