开发者

I can't use JSTL tag for read rss

I want to read rss from another web. but I can't read rss . I use Netbeans and I add JSTL Library already. I can use forEach tag but every time when I use xml tag it show error. This is my code for read rss. I don't know how to read rss with JSTL tag please help me.


<!-- Filename: ShowStudents.jsp -->
<%@taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<HTML>
<BODY>
<c:import var="xmlDoc" url="http://www.manager.co.th/RSS/Home/Breakingnews.xml"/>
<x:parse var="parsedDocument" xml="${xmlDoc}"/>
<table>
<tr>
<th>Title : </th>
<th>Link : </th>
</tr>

<x:forEach select="$parsedDocument/channel/item"> // This code I ever been change to $parsedDocument/rss/channel/ and $parsedDocument/rss/channel/title ,but it show same error. 
<tr>
<td> <x:out select="title" /> </td>
<td> <x:out select="link" /> </td>
</tr>
</x:forEach>
</table>
</BODY>
</HTML>

when I run this code it show error like this. how to read rss with JSTL tag please help me.

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspException: org.xml.sax.SAXParseException: Content is not allowed in prolog. org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:527) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:401) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) Filter.ThaiRequest.doFilter(ThaiRequest.java:38) org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

root cause

javax.servlet.ServletException: javax.servlet.jsp.JspException: org.xml.sax.SAXParseException: Content is not allowed in prolog. org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:858) org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:791) org.apache.jsp.test_jsp._jspService(test_jsp.java:101) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) Filter.ThaiRequest.doFilter(ThaiRequest.java:38) org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

root cause

org.xml.sax.SAXParseException: Content is not allowed in prolog. com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:249) com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284) org.apache.taglibs.standard.tag.common.xml.ParseSupport.parseInputSource(ParseSupport.java:227) org.apache.taglibs.standard.tag.common.xml.ParseSupport.parseInputSourceWithFilter(ParseSupport.java:193) org.apache.taglibs.standard.tag.common.xml.ParseSupport.parseReaderWithFilter(ParseSupport.java:199) org.apache.taglibs.standard.tag.common.xml.ParseSupport.parseStringWithFilter(ParseSupport.java:206) org.apache.taglibs.standard.tag.common.xml.ParseSupport.doEndTag(ParseSupport.java:138) org.apache.jsp.test_jsp._jspx_meth_x_005fparse_005f0(test_jsp.java:150) org.apache.jsp.test_jsp._jspService(test_jsp.java:80) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) Filter.ThaiRequest.doFilter(ThaiRequest.java:38) org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

Update:

Now I can read xml with JDOM in servlet but I can't display in jsp file. I can display all data from xml in servlet. I don't know how to display data from title and link tag only in jsp file. please help me.

try {

     ReadXMLFile rxml = new ReadXMLFile();
     NodeList nodes = (NodeList) rxml.readXML();
     for (int i = 0; i < nodes.getLength(); i++) {

       /* I can display all data from xml. I want to display data from title and link tag only in JSP file. */
     System.开发者_运维问答out.println(nodes.item(i).getNodeValue());

        }


The document at http://www.manager.co.th/RSS/Home/Breakingnews.xml looks like it starts with a Byte-Order-Mark.

In Java, this is only processed correctly when the XML parser reads directly from the input stream. In your case, though, you're reading the document into a String, and then passing that String to the <x:parse> tag, and because it's reading from a String, it doesn't handle the BOM, it thinks it's junk, and gives you the "Content is not allowed in prolog" error.

I don't think this is easily fixable in JSP/JSTL. I think you'll need to either write a custom tag to parse the document direct from the input stream, or use an MVC structure to do the parsing in a servlet/controller, and forward to the JSP for display of the data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜