File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes xml in JSP page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "xml in JSP page" Watch "xml in JSP page" New topic

xml in JSP page

Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
First off, I'm a complete newbie so execuse my ignorance.
if I have an xml document that specified some text that appears in a jsp file:
lets say the xml file is like: form.xml

Now in the JSP file I want the <fn>, <ln>, <ph>, <adr> elements to be displayed in the right place and to be rendered on the server, not on the clients machine, so client side xsl is not an option.
What is the easiest way to do this? How to use this specific xml file? what code do you write to get the values of the elements and put them in the right place using server side code
Lets say we have the following html.

Thanks in advance

Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
prabhakar rao

Joined: May 17, 2003
Posts: 6
my suggestion is to learn JSTL, there are inbuilt tags are available to read xml elements and its values. So that u can use JSTL tags and get the output.
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
Thanks for the advice, but can you please tell me how would the code look like? Just a little snippet of code.
Geeta Ramasami
Ranch Hand

Joined: Mar 05, 2003
Posts: 72
For parsing and processing XML u have so many parsers available.U can go in for Apaches parser.Apaches SAX parser provides methods use to read XML file.
prabhakar rao

Joined: May 17, 2003
Posts: 6
hi friend,
copy c.tld and x.tld files under Web-inf folder
and observ carefully here, I've mentioned import JSTL tag to import your xml file for e.g., my xml file name is ForEachDoc.xml then
parse your xml file by using again JSTL tag and store it in a variable called as document. Here I'm using forEach tag for my own task. but that is not required for u. But observe carefully I've used x-path notations inside forEach JSTL tag i.e, $document//a
here $document means, your xml document stored in the document varaible under that I've mentioned
//a this means I'm reading element name is 'a'
in your case u give fn, ln etc., in place of 'a'
read the code, I try my level best to reach this code. In case u didn't get me pls send your personal mail id , I can send complete documentation of JSTL.
<%@ taglib prefix="c" uri="/WEB-INF/c.tld" %>
<%@ taglib prefix="x" uri="/WEB-INF/x.tld" %>
<title>JSTL: XML Support -- if</title>
<body bgcolor="#FFFFFF">
<c:import var="docString" url="ForEachDoc.xml"/>
<x arse var="document" xml="${docString}"/>
<x:forEach select="$document//a" var="att">
<x:if select="$att/d">
<x ut select="$att"/><br>
I agree. Here's the link:
subject: xml in JSP page
It's not a secret anymore!