xml标签库资料转帖
第一节 XML标签库(Core tag library)
分类 |
功能分类 |
标签名称 |
XML |
核心操作 |
out parse set |
流程控制 |
choose when otherwise forEach if |
|
文件交换 |
transform |
(1) 核心操作
<x:parse>
<x:parse>用来解析XML文件。
语法1:
语法2
属性
名称 |
说明 |
EL |
类型 |
必须 |
默认值 |
doc |
XML文件 |
Y |
String/ Reader |
否 |
无 |
systemId |
XML文件的URL |
Y |
String |
否 |
无 |
filter |
XMLFilter过滤器 |
Y |
org.xml.sax.XMLFilter |
否 |
无 |
varDom |
储存解析后的XML文件 (org.w3c.dom.Document) |
N |
String |
否 |
无 |
scopeDom |
varDom的范围 |
N |
String |
否 |
Page |
var |
储存解析后的XML文件 |
N |
String |
否 |
无 |
scope |
var变量的JSP范围 |
N |
String |
否 |
Page |
l <x:out>
<x:out>主要用来取出XML中的字符串。
语法
属性
名称 |
说明 |
EL |
类型 |
必须 |
默认值 |
select |
XPath语句 |
N |
String |
否 |
无 |
escapeXml |
是否转换特殊字符,例:<转换< |
Y |
boolean |
是 |
true |
l <x:set>
<x:set>将从XML文件取得的内容储存至属性范围中
语法:
属性
名称 |
说明 |
EL |
类型 |
必须 |
默认值 |
select |
XPath语句 |
N |
String |
是 |
无 |
var |
将从XML文件中取得的内容储存至varName中 |
N |
String |
是 |
无 |
scope |
var变量的JSP范围 |
N |
String |
否 |
page |
NewFile.xml
<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person>
<name>youdazhi</name>
<age>31</age>
</person>
</persons>
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>
<html>
<head>
</head>
<body>
<h2></h2>
<c:import var="sample" url="NewFile.xml"/>
<x:parse doc="${sample }" var="sampleXml1"/>
<x:out select="$sampleXml1/persons/person/name"/>
<x:parse var="sampleXml2">
<items>
<item>
<name>car</name>
<price>1000</price>
</item>
</items>
</x:parse>
<x:set var="item" select="$sampleXml2/items/item"/>
<x:out select="$item/price"/>
</body>
</html>
(1) 流程控制
流程控制功能主要分为两类。
1)条件判断,在JSP中当我们想要依据XML的内容做某些动作时,条件判断部分就会使我们处理起来方便许多。
2)循环功能,让整个XML文件被循环浏览一遍。
<x:if>和<x:choose>就是所谓的条件判断,而<x:forEach>就是循环功能。
<x:if>
<c:if>和<c:if>类似,只是条件判断的内容为XPath语句
语法1:
语法2
属性
名称 |
说明 |
EL |
类型 |
必须 |
默认值 |
select |
XPath语句,如果为true,则执行<x:if>的本体内容 |
N |
String |
是 |
无 |
var |
设定属性名称存放判断结果(true或false) |
N |
String |
否 |
无 |
scope |
var变量的JSP范围 |
N |
String |
否 |
page |
示例
NewFile2.xml
<?xml version="1.0" encoding="UTF-8"?>
<customers>
<cumtomer id="1">
<name>brower</name>
<phone>156-1324</phone>
<phone>201-9999</phone>
</cumtomer>
<cumtomer id="2">
<name>koji</name>
</cumtomer>
</customers>
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>
<html>
<head>
</head>
<body>
<h2></h2>
<c:import var="sample" url="NewFile2.xml"/>
<x:parse doc="${sample }" var="doc"/>
<x:if select="$doc/customers/customer[@id=1]/phone">
谢谢您留下您的个人电话
</x:if>
</body>
</html>