wood burning stoves 2.0*
The moose likes JSP and the fly likes Reading xml data in JSP 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 "Reading xml data in JSP" Watch "Reading xml data in JSP" New topic
Author

Reading xml data in JSP

lavi mendonca
Ranch Hand

Joined: Apr 24, 2002
Posts: 53
In my application, I have an xml document which contains all my data. A Java class within the application returns this xml document and it does not have any standard structure. It can contain different kinds of child elements and/ or attributes. Thus I cannot define a fixed schema from the onset. I need to display the data contained in this xml document on the browser using JSP technology.

What are the various options i might use to do this ?? Also, is there a standard mechanism by which i can convert this xml document into some on the fly Java Value Object (which contains fields, getters and setters), so that i can access this in my jsp page and then display the data on the browser ?? Note that i am not restricted to using 1 JSP file for all xml documents. For each type of xml document (functionality based), i can have a different JSP.

Any help would be greatly appreciated.

Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

Check out the XML tags of the JSTL.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
John Catron
Greenhorn

Joined: Oct 17, 2008
Posts: 3
Originally posted by lavi mendonca:
... Also, is there a standard mechanism by which i can convert this xml document into some on the fly Java Value Object (which contains fields, getters and setters), so that i can access this in my jsp page and then display the data on the browser ?? ...


http://xmlbeans.apache.org/
lavi mendonca
Ranch Hand

Joined: Apr 24, 2002
Posts: 53
Thank you - Bear Bibeault and and J, Catron for your help.

I will explore JSTL further. XMLBeans is not an option because it requires a mapping/ schema to convert the xml into Java objects. We would like to transform the xml document into Java objects directly (if any mechanism exists) without using any kind of mapping or schema in between as our xml structure is varied and building thousands of schema or mapping documents is not feasible.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

The simplest way to display the data contained in that XML document on the browser is to simply copy it to the browser unchanged.

But if you're talking about converting it into Java objects and then converting those Java objects into HTML via JSP, then presumably you want to interpret the data in some way before displaying it. Have you excluded the possibility of using XSLT to do that?

If you don't have a schema that describes the data, then the description of the data must be inherent in the code you use to convert the XML document into Java objects. I don't think that's likely to be the sort of thing that JSTL supports, so you should really consider not doing that code in the JSP.
lavi mendonca
Ranch Hand

Joined: Apr 24, 2002
Posts: 53
Hi Paul,

Thank you for your inputs. XSLT is not an option. This is a usual web application and instead of retreiving data from the database etc, the application needs to display it from the xml document which is a stream. And the technology on the presentation side has to be JSP (JSTL).

Bear - I have a few questions
1. The JSTL XML library has a tag to import the xml document. Is it necessary that this xml file should exist as a physical file in the file system ?? Can we configure it to read the xml document as a inputstream ?

2. Also, on googlin i found there is Sun's JSTL and Jakarta's Standard Taglibs. Therefore, which is the actual one that we should be using in our web application ?

Thanks again.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

Originally posted by lavi mendonca:
The JSTL XML library has a tag to import the xml document. Is it necessary that this xml file should exist as a physical file in the file system ?? Can we configure it to read the xml document as a inputstream ?

Consider the following example from the JSTL Specification:
Can you answer your own question given this info?
[ October 20, 2008: Message edited by: Bear Bibeault ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Originally posted by lavi mendonca:
XSLT is not an option. This is a usual web application and instead of retreiving data from the database etc, the application needs to display it from the xml document which is a stream. And the technology on the presentation side has to be JSP (JSTL).
Well, neither of those two things eliminates XSLT as an option. You can certainly transform XML from an InputStream. And just because you've chosen JSTL for the presentation side (a good choice I would add), that doesn't mean it's the only tool you can use. You could certainly use XSLT in the business logic layer before handing over the data to the presentation layer.

In fact even if you didn't use XSLT, I can still envision processing the XML document in the business logic layer first.
lavi mendonca
Ranch Hand

Joined: Apr 24, 2002
Posts: 53
Hi Bear Bibeault ,

I went through the JSTL specs, and I am a bit confused. I was able to read the content of the xml document which was present in the request scope (say xmlObj attribute, set by the business logic Java code).

<xut select="$xmlObj/mainElement/childElement/FirstName/text()"/>

But at the same time, I also need a way of reading data from the xml which is not a String or Reader. This xml file may exist physically on the filesystem/ webserver where the jsp/ jstl code also resides.

Can you please let me know if the current c:import, x:parse JSTL tags support this ? If so, can you please give me some examples ?

Thanks again for your inputs and help.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

If it can be addressed via a URL, <c:import> can access it.
lavi mendonca
Ranch Hand

Joined: Apr 24, 2002
Posts: 53
Hi Bear Bibeault,

I tried using c:import (please see below), but it didn't work. I also used relative url in the c:import, even then it did not work. Right now, i am deploying this on my local machine using Tomcat, but we would be hosting this on a proper web server. Where am i going wrong in the syntax ?

<c:import
url="C:/usr/local/tomcat/webapps/poc/WEB-INF/jsp/tiles/layouts/stocks.xml" var="xmldoc"/>

<x:parse xml="${xmldoc}" var="output"/>

<xut select="$output/mainEle/childEle/FirstName/text()"/>

Thanks again for your inputs and help.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

Use a valid context-relative URL. Hard-coding a path in the file system is never a good idea.
lavi mendonca
Ranch Hand

Joined: Apr 24, 2002
Posts: 53
Hi Bear,

I used hardcoding just to check if it finds the file, but i also tried the context relative URL like below, but it wasn't able to read it. On the browser, no content is displayed.

<c:import url="stocks.xml" var="xmldoc"/>
<x:parse xml="${xmldoc}" var="output"/>
<xut select="$output/mainEle/childEle/FirstName/text()"/>

where stocks.xml is currently present in the same location as the jsp containing the above text.

Also, if i do view the source from the browser (right click - view source), then it displays the jstl code along with html code as below. This shows that the jstl code is not intepreted.

<html>
<body>
<c:import url="stocks.xml" var="xmldoc"/>
<x:parse xml="" var="output"/>
<xut select="$output/mainEle/childEle/FirstName/text()"/>
</body>
</html>

I am not able to figure out what is wrong here. As i mentioned before, i am able to read data from the xml document stored as a Reader in the request scope.

Thanks again.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

That's not a context-relative URL, it's a page-relative URL.

Where is the file in relation to the web app root?
lavi mendonca
Ranch Hand

Joined: Apr 24, 2002
Posts: 53
The web app root - poc
This is under webapps folder of tomcat.

under poc folder, I have WEB-INF. Under WEB-INF, I have jsp, classes, lib folders. tiles-defs.xml (As tiles are also used) and web.xml are in WEB-INF. Under jsp folder, tiles\mainLayout contains the jsp for the actual layout. It defines the header, body and footer as below:

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jstl/xml" %>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<title> Tiles </title>
<body>
<div id="layoutWrapper">
<%--Header starts here --%>
<tiles:insertAttribute name="header"/>

<%--Rigth Content starts here --%>
<tiles:insertAttribute name="body"/>

<%--Footer starts here --%>
<tiles:insertAttribute name="footer"/>

</div>
</body>
</html>

Now, the jsps for the header, body and footer are in another folder - under the jsp folder i.e tiles\otherLayout. The footer jsp which contains the code for reading stock.xml is present here. This folder also contains stock.xml. Thus the code in footer.jsp is:

<div id="footerContentDiv">
<c:import url="stocks.xml" var="xmldoc"/>
<x:parse xml="${xmldoc}" var="output"/>

<p>
<table border="2" width="50%">
<tr>
<th>Stock Symbol</th>
<th>Company Name</th>
<th>Price</th>
</tr>


<x:forEach select="$output/portfolio/stock" var="item">
<tr>
<td><xut select="symbol"/></td>
<td><xut select="name"/></td>
<td><xut select="price"/></td>
</tr>
</x:forEach>
</table>

<br/>
</div>

Hope this information is helpful. Please let me know if you need any other info.

I tried another approach without using tiles. Pure JSP, JSTL and this time, the c:import and x:parse xml worked and it was able to read the xml file (jsp containing the jstl code and xml document were in the same folder).

Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

If it's in the root of the web app, the context-relative reference is /stocks.xml.
 
wood burning stoves
 
subject: Reading xml data in JSP
 
Similar Threads
General Document Style Validation Questions
Can I use java in XML file?
Q for Dmitry Kirsanov : XSLT's value
xml/xslt/jsp Usage patterns...?
how do i write xml in to file by using jsp