使用JSTL读取XML文件


JSTL是JSP的标准标签库,里面包含了许多功能强大,使用简单的标签,其中XML模块的标签让JSP读取XML文件变的收放自如。

假定我们的XML文件为(将该文件存放到web应用文件夹下):

<books>

<book id=“1″>
<author>Dave Crane</author>
<bookname>Ajax In Action</bookname>
</book></books>
<book id=“2″>
<author>Ryan Asleson</author>
<bookname>Foundations of Ajax</bookname>
</book>

为了使用JSTL,则必须从jakarta.apache.org 下载JSTL,将里面的jstl.jar、standard.jar、xercesImpl.jar、xalan.jar这些包放到web应用的lib文件夹下,这些是读取XML文件必须的。

编写JSP页面,代码如下:

<%@ page contentType=“text/html;charset=GB2312″ %>

<%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>
<%@ taglib prefix=“x” uri=“http://java.sun.com/jsp/jstl/xml” %>

<c:import var=“books” url=“book.xml”>
<x:parse var=“doc” xml=“${books}”>
<x:out select=“$doc//*”></x:out></x:parse></c:import>

用浏览器浏览就可以看到结果,如何读取到你想要的结果,关键在XML的select表达式,一般来讲,是给关键性的标签加一个id来识别,比如为每一个book标签加一个id,那么在jsp页面中我们就可以使用如下方式来找到我们需要的那个book标签:

<x:out select=“$doc/books/book[@id='2']/bookname”></x:out>

版权信息:本文来自老杨个人网站,作者老杨原文地址,转载请注明出处。

如果喜欢本站,敬请订阅:老杨个人网站。谢谢惠顾!


分类: 信息技术
标签: , , ,

前2排已经被占了我要占座

  1. JSTL是stl的java版本吗?
    算法库?

    回复Ta

    @philcheng001, 不是一回事。呵呵,这是Java读取标签的一个标准

    回复Ta

:emotion58 :emotion57 :emotion563 :emotion562 :emotion561 :emotion560 :emotion56 :emotion559 :emotion558 :emotion557 :emotion556 :emotion554 :emotion552 :emotion551 :emotion550 :emotion55 :emotion547 :emotion546 :emotion544 :emotion543 :emotion542 :emotion541 :emotion540 :emotion54 :emotion539 :emotion538 :emotion537 :emotion536 :emotion535 :emotion534 :emotion533 :emotion532 :emotion531 :emotion530 :emotion53 :emotion529 :emotion526 :emotion525 :emotion524 :emotion523 :emotion522 :emotion521 :emotion520 :emotion52 :emotion519 :emotion518 :emotion517 :emotion516 :emotion515 :emotion514 :emotion513 :emotion512 :emotion511 :emotion510 :emotion51 :emotion119 :emotion113