Reputation: 37
I have a .jsp which determines what specific things a user has access to. It creates an XML file which is read into a stream by the previous jsp. How would I populate a table with the XML data which is read into a char array stream?
Upvotes: 0
Views: 2712
Reputation: 1108802
There are a lot of ways to get a HTML table out of the XML file. One of the most clean ways is to parse that XML into a collection of reuseable javabeans which you pass to the JSP, so that you can use JSTL <c:forEach>
to iterate over it while rendering a HTML table. This way every layer keeps its own clear responsibility. The Java SE provided JAXB is very helpful in this.
Imagine that your XML look like this:
<data>
<entry>
<key>foo1</key>
<value>bar1</value>
</entry>
<entry>
<key>foo2</key>
<value>bar2</value>
</entry>
<entry>
<key>foo3</key>
<value>bar3</value>
</entry>
</data>
Then you can create a JAXB javabean as follows:
@XmlRootElement
public class Data {
@XmlElement(name="entry")
private List<Entry> entries;
public List<Entry> getEntries() {
return entries;
}
public static class Entry {
@XmlElement
private String key;
@XmlElement
private String value;
public String getKey() {
return key;
}
public String getValue() {
return value;
}
}
}
Then you can transform it into a List<Entry>
as follows:
List<Entry> entries = JAXBContext.newInstance(Data.class).createUnmarshaller().unmarshal(inputStream).getEntries();
Then you can let your servlet (or JSP? :/ ) store it in the request scope before forwarding the request to the JSP:
request.setAttribute("entries", entries);
Finally you can in JSP iterate over it and present it as a HTML table:
<table>
<c:forEach items="${entries}" var="entry">
<tr>
<td><c:out value="${entry.key}" /></td>
<td><c:out value="${entry.value}" /></td>
</tr>
</c:forEach>
</table>
Upvotes: 3
Reputation: 11130
So in your JSP you've got nothing other than the InputStream that is the raw XML document? It seems to me that you have little choice but to learn the Java libraries for processing XML.
Google search "XML Processing in Java" and you'll see the myriad of options available to you. Even the "XSLT to transform your XML to something you can deal with in the HTML of the page" recommendation above is going to require you to parse this stream. The JAXB API (http://jaxb.java.net/tutorial/section_1_2-Overview.html#Overview) makes this a little less painful, allowing you to map the XML document into POJOs.
Anyway you look at it, processing this at the JSP level is a bit of a code smell. You should process the XML stream somewhere else and send your JSP the POJO results for presentation.
My 2 cents worth.
Upvotes: 0
Reputation: 2635
You can use XSLT to style the XML as it is or use XPath to navigate over the XML and build an table that fits your needs.
But not sure if embedding that logic is clean, I would rather that was processed in a bean and you could get the final list from a bean that the jsp can use to produce the table.
http://oreilly.com/catalog/javaxslt/chapter/ch05.html
Upvotes: 2