I am creating an mvc application, in controller (servlet), I am populating a request variable and then trying to read and show it in view (jsp) via foreach jstl tags. The problem is that I don't know how to read xml from session and then parse it and show attributes and elements in jsp page. Below is the code that I have.
In servlet, set request attribute:
request.setAttribute("XMLDocument", xmlResultStr);
This is a sample xml that can be stored in xmlResultStr.
<?xml version="1.0" encoding="UTF-8"?>
<SearchResults>
<result url="http://haacked.com/" totalcount="14">
<keyword count="9">start</keyword>
<keyword count="5">end</keyword>
</result>
<result url="http://feeds.haacked.com/haacked" totalcount="14">
<keyword count="9">start</keyword>
<keyword count="5">end</keyword>
</result>
</SearchResults>
In jsp page, I am using this code but completely without luck, I am searching for many hours and fully stuck on this:
<x:parse xml="${XMLDocument}" var="xml"/>
<c:out value="${xml}" />
<x:forEach select="$xml/SearchResults/result" var="item">
<c:set var="elementName" value="url"/>
<x:out select="$item/*[name()=$elementName]" />
<h3><a href="<x:out select="$item/*[name()=$elementName]" />" target="_blank"><x:out select="$item/*[name()=$elementName]" /></a></h3>
<p>
<%-- somehow display keywords list here, like shown below --%>
</p>
<hr />
</x:forEach>
What I want is this:
<h3><a href="http://haacked.com/" target="_blank">http://haacked.com/</a></h3>
<p>
(start - 9, end - 5)
</p>
<hr />