<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>xml标签库测试</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <!-- 这次操作的xml的文件的结构 <?xml version="1.0" encoding="UTF-8"?> <score> <anser qid="1" type="choice" order="1">B</anser> <anser qid="2" type="choice" order="2">B</anser> <anser qid="4" type="choice" order="3">D</anser> <anser qid="5" type="choice" order="4">C</anser> <anser qid="6" type="choice" order="5">B</anser> <anser qid="8" type="choice" order="6">C</anser> <anser qid="9" type="choice" order="7">A</anser> <anser qid="12" type="choice" order="8">D</anser> <anser qid="13" type="choice" order="9">A</anser> <anser qid="16" type="choice" order="10">D</anser> </score> --> <body> <center><h3>xml测试</h3></center> <c:import url="style.xml" var="xmldoc" /> <!-- 将xml文件包含进来--> <x:parse doc="${xmldoc}" var="xpath" /> <!-- 对xml文件进行解析 --> score: <x:out select="$xpath//score" /><br /> <!-- 读取到所有的score 结果:$xpath//score: B B D C B C A D A D --> anser第9个: <x:out select="$xpath//score/anser[9]"/><br /><!-- anser第9个: A --> anser最后一个: <x:out select="$xpath//score/anser[last()]"/><br /><!-- anser最后一个: D --> anser最后一个的id属性: <x:out select="$xpath//score/anser[last()]/@qid"/><br /><!-- anser最后一个的id属性: 16 --> anserid为8的type值: <x:out select="$xpath//score/anser[@qid=8]/@type"/><br /><!--anserid为8的type值: choice --> <br/><br/><br/><br/><br/><br/> <!-- x:if流程控制 --> <x:if select="$xpath//score/anser[@qid=8]/@type"> qid为8的答案的题目 类型是选择题 </x:if> <!-- 运行结果 qid为8的答案的题目 类型是选择题 --> <br/><br/><br/><br/><br/><br/> <!-- choose when otherWise标签的使用: --> <x:choose> <x:when select="$xpath//score/anser[@type='choice']"> 所有选择题的答案:<x:out select="$xpath//score/anser" /> </x:when> <x:otherwise> <c:out value="这不是你要查找的选择题"></c:out> </x:otherwise> </x:choose> <!-- 运行结果 所有选择题的答案:B--> <br/><br/><br/><br/><br/><br/> 所有成绩: <x:forEach select="$xpath//score" var="score"> <x:out select="$score"/><br/> </x:forEach> <!-- 运行结果 所有成绩: B B D C B C A D A D --> </body> </html>