Java Tutorial/JSTL/XML Path
Содержание
Output XML with Select Statement
<source lang="java">
<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.ru/jstl/xml" prefix="x" %> <html>
<head> <title>xml actions</title> </head> <body> This example embeds stuff... <p/> <c:set var="someXML"> <person> <name>Ruth</name> <age>29</age> </person> </c:set> <x:parse var="parsedDocument" xml="${someXML}" /> <x:out select="$parsedDocument/*" /> </body>
</html></source>
Parse XML and Path Select
<source lang="java">
<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.ru/jstl/xml" prefix="x" %> <html>
<head> <title>Parse Examples</title> </head> <body>Please enter an XML file:
<form method="post"> <textarea rows="10" cols="50" name="xml"> <students> <student id="1"> <name> <first>A</first> <last>B</last> <middle>T</middle> </name> <grade> <points>88</points> <letter>B</letter> </grade> </student> <student id="2"> <name> <first>C</first> <last>D</last> <middle>K</middle> </name> <grade> <points>92</points> <letter>A</letter> </grade> </student> </students> </textarea>
<input type="submit" /> </form> <c:if test="${pageContext.request.method=="POST"}"> <x:parse var="doc" xml="${param.xml}" />
$doc/students/student/name/first |
<x:out select="$doc/students/student/name/first" /> |
$doc/students/student[@id=2]/name/first |
<x:out select="$doc/students/student[@id=2]/name/first" /> |
$doc/students/student[@id=1]/name/first |
<x:out select="$doc/students/student[@id=12]/name/first" /> |
</c:if> </body>
</html></source>
Parse XML With XPath Expression
<source lang="java">
<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.ru/jstl/xml" prefix="x" %> <html>
<head> <title>xml actions</title> </head> <body> This example parses XML and uses an XPath expression... <p/> <c:set var="someXML"> <people> <person> <name>Mark</name> <age>30</age> </person> <person> <name>Ruth</name> <age>29</age> </person> </people> </c:set> <x:parse var="parsedDocument" xml="${someXML}" /> Here is a list of people:
-
<x:forEach select="$parsedDocument/people/person">
- <x:out select="name" />
</x:forEach>
</body>
</html></source>
Set Variable by Selecting XML Path
<source lang="java">
<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.ru/jstl/xml" prefix="x" %> <html>
<head> <title>Set Examples</title> </head> <body> <c:import var="students" url="students.xml" /> <x:parse var="doc" xml="${students}" /> <x:set var="a" select="$doc/students/student/name/first" /> <x:set var="b" select="$doc/students/student[@id=2]/name/first" /> <x:set var="c" select="$doc/students/student[@id=1]/name/first" />
$doc/students/student/name/first |
<x:out select="$a" /> |
$doc/students/student[@id=2]/name/first |
<x:out select="$b" /> |
$doc/students/student[@id=1]/name/first |
<x:out select="$c" /> |
</body>
</html></source>
Use ForEach Loop to Select XML Path
<source lang="java">
<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %><%@ taglib uri="http://java.sun.ru/jstl/xml" prefix="x" %> <html>
<head> <title>For Each Examples</title> </head> <body> <c:import var="students" url="students.xml" /> <x:parse var="doc" xml="${students}" />
First | Last | Points | Letter |
---|---|---|---|
<x:out select="name/first" /> |
<x:out select="name/last" /> |
<x:out select="grade/points" /> |
<x:out select="grade/letter" /> |
</body>
</html></source>
Use JSTL to output and extract XML element with XPATH
<source lang="java">
<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.ru/jstl/xml" prefix="x" %> <html>
<head> <title>xml actions</title> </head> <body> This example uses XPath and the x:out action to extract the user"s name from a piece of XML. <p/> <c:set var="someXML"> <person> <name>Ruth</name> <age>29</age> </person> </c:set> <x:parse var="parsedDocument" xml="${someXML}" /> The person"s name is <x:out select="$parsedDocument//name" /> </body>
</html></source>