Java Tutorial/JSTL/XML Path

Материал из Java эксперт
Версия от 15:23, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Output XML with Select Statement

<%@ 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>





Parse XML and Path Select

<%@ 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:
  <br />
  <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>
    <br />
    <input type="submit" />
  </form>
  <c:if test="${pageContext.request.method=="POST"}">
    <x:parse var="doc" xml="${param.xml}"  />
    <table border="1">
      <tr>
        <td>$doc/students/student/name/first</td>
        <td>
          <x:out select="$doc/students/student/name/first" />
        </td>
      </tr>
      <tr>
        <td>$doc/students/student[@id=2]/name/first</td>
        <td>
          <x:out
          select="$doc/students/student[@id=2]/name/first" />
        </td>
      </tr>
      <tr>
        <td>$doc/students/student[@id=1]/name/first</td>
        <td>
          <x:out
          select="$doc/students/student[@id=12]/name/first" />
        </td>
      </tr>
    </table>
  </c:if>
  </body>
</html>





Parse XML With XPath Expression

<%@ 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:
    <ul>
      <x:forEach select="$parsedDocument/people/person">
        <li> <x:out select="name" /> </li>
      </x:forEach>
    </ul>
  </body>
</html>





Set Variable by Selecting XML Path

<%@ 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" />
    <table border="1">
      <tr>
        <td>$doc/students/student/name/first</td>
        <td>
          <x:out select="$a" />
        </td>
      </tr>
      <tr>
        <td>$doc/students/student[@id=2]/name/first</td>
        <td>
          <x:out select="$b" />
        </td>
      </tr>
      <tr>
        <td>$doc/students/student[@id=1]/name/first</td>
        <td>
          <x:out select="$c" />
        </td>
      </tr>
    </table>
  </body>
</html>





Use ForEach Loop to Select XML Path

<%@ 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}" />
    <table border="1">
      <tr>
        <TH>First</th>
        <TH>Last</th>
        <TH>Points</th>
        <TH>Letter</th>
      </tr>
      <x:forEach var="student" select="$doc/students/student">
        <tr>
          <td>
            <x:out select="name/first" />
          </td>
          <td>
            <x:out select="name/last" />
          </td>
          <td>
            <x:out select="grade/points" />
          </td>
          <td>
            <x:out select="grade/letter" />
          </td>
        </tr>
      </x:forEach>
    </table>
  </body>
</html>





Use JSTL to output and extract XML element with XPATH

<%@ 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>