Java/JSTL/XML

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

JSTL: parse XML document

<%@ 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>Joe</name>
          <age>30</age>
        </person>
        <person>
          <name>Rosy</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>





JSTL XML Foreach

<?xml version="1.0" encoding="ISO-8859-1"?>
<students>
   <student id="1">
      <name>
         <first>Joe</first>
         <last>Y</last>
         <middle>T</middle>
      </name>
      <grade>
         <points>99</points>
         <letter>A</letter>
      </grade>
   </student>
   <student id="2">
      <name>
         <first>James</first>
         <last>Todd</last>
         <middle>K</middle>
      </name>
      <grade>
         <points>92</points>
         <letter>B</letter>
      </grade>
   </student>
   <student id="3">
      <name>
         <first>Kate</first>
         <last>Wang</last>
         <middle>A</middle>
      </name>
      <grade>
         <points>72</points>
         <letter>C</letter>
      </grade>
   </student>
   
</students>

/////////////////////////////////////////////////////////////////////////////////

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





JSTL XML Map Value

<?xml version="1.0" encoding="ISO-8859-1"?>
<students>
   <student id="1">
      <name>
         <first>Joe</first>
         <last>Y</last>
         <middle>T</middle>
      </name>
      <grade>
         <points>99</points>
         <letter>A</letter>
      </grade>
   </student>
   <student id="2">
      <name>
         <first>James</first>
         <last>Todd</last>
         <middle>K</middle>
      </name>
      <grade>
         <points>92</points>
         <letter>B</letter>
      </grade>
   </student>
   <student id="3">
      <name>
         <first>Kate</first>
         <last>Wang</last>
         <middle>A</middle>
      </name>
      <grade>
         <points>72</points>
         <letter>C</letter>
      </grade>
   </student>
   
</students>

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





JSTL XML Output

<?xml version="1.0" encoding="ISO-8859-1"?>
<students>
   <student id="1">
      <name>
         <first>Joe</first>
         <last>Y</last>
         <middle>T</middle>
      </name>
      <grade>
         <points>99</points>
         <letter>A</letter>
      </grade>
   </student>
   <student id="2">
      <name>
         <first>James</first>
         <last>Todd</last>
         <middle>K</middle>
      </name>
      <grade>
         <points>92</points>
         <letter>B</letter>
      </grade>
   </student>
   <student id="3">
      <name>
         <first>Kate</first>
         <last>Wang</last>
         <middle>A</middle>
      </name>
      <grade>
         <points>72</points>
         <letter>C</letter>
      </grade>
   </student>
   
</students>
////////////////////////////////////////////////////////////////////////
<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %><%@ taglib uri="http://java.sun.ru/jstl/xml" prefix="x" %>
<html>
  <head>
    <title>Out Examples</title>
  </head>
  <body>
    <c:import var="students" url="students.xml" />
    <x:parse var="doc" xml="${students}" />
    <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=1]/name/first</td>
        <td>
          <x:out
          select="$doc/students/student[@id=1]/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>
    </table>
  </body>
</html>





JSTL XML Parse

<?xml version="1.0" encoding="ISO-8859-1"?>
<students>
   <student id="1">
      <name>
         <first>Joe</first>
         <last>Y</last>
         <middle>T</middle>
      </name>
      <grade>
         <points>99</points>
         <letter>A</letter>
      </grade>
   </student>
   <student id="2">
      <name>
         <first>James</first>
         <last>Todd</last>
         <middle>K</middle>
      </name>
      <grade>
         <points>92</points>
         <letter>B</letter>
      </grade>
   </student>
   <student id="3">
      <name>
         <first>Kate</first>
         <last>Wang</last>
         <middle>A</middle>
      </name>
      <grade>
         <points>72</points>
         <letter>C</letter>
      </grade>
   </student>
   
</students>

///////////////////////////////////////////////////////////////////////////////
<%@ 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>John</first>
            <last>Smith</last>
            <middle>T</middle>
          </name>
          <grade>
            <points>88</points>
            <letter>B</letter>
          </grade>
        </student>
        <student id="2">
          <name>
            <first>James</first>
            <last>Smith</last>
            <middle>K</middle>
          </name>
          <grade>
            <points>92</points>
            <letter>A</letter>
          </grade>
        </student>
        <student id="3">
          <name>
            <first>Kelly</first>
            <last>Lane</last>
            <middle>A</middle>
          </name>
          <grade>
            <points>72</points>
            <letter>C</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>





JSTL XML XPath

<?xml version="1.0" encoding="ISO-8859-1"?>
<students>
   <student id="1">
      <name>
         <first>Joe</first>
         <last>Y</last>
         <middle>T</middle>
      </name>
      <grade>
         <points>99</points>
         <letter>A</letter>
      </grade>
   </student>
   <student id="2">
      <name>
         <first>James</first>
         <last>Todd</last>
         <middle>K</middle>
      </name>
      <grade>
         <points>92</points>
         <letter>B</letter>
      </grade>
   </student>
   <student id="3">
      <name>
         <first>Kate</first>
         <last>Wang</last>
         <middle>A</middle>
      </name>
      <grade>
         <points>72</points>
         <letter>C</letter>
      </grade>
   </student>
   
</students>
////////////////////////////////////////////////////////////////////////////////
<%@ 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>John</first>
            <last>Smith</last>
            <middle>T</middle>
          </name>
          <grade>
            <points>88</points>
            <letter>B</letter>
          </grade>
        </student>
        <student id="2">
          <name>
            <first>James</first>
            <last>Smith</last>
            <middle>K</middle>
          </name>
          <grade>
            <points>92</points>
            <letter>A</letter>
          </grade>
        </student>
        <student id="3">
          <name>
            <first>Kelly</first>
            <last>Lane</last>
            <middle>A</middle>
          </name>
          <grade>
            <points>72</points>
            <letter>C</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>





JSTL XML XPath Choose

<?xml version="1.0" encoding="ISO-8859-1"?>
<students>
   <student id="1">
      <name>
         <first>Joe</first>
         <last>Y</last>
         <middle>T</middle>
      </name>
      <grade>
         <points>99</points>
         <letter>A</letter>
      </grade>
   </student>
   <student id="2">
      <name>
         <first>James</first>
         <last>Todd</last>
         <middle>K</middle>
      </name>
      <grade>
         <points>92</points>
         <letter>B</letter>
      </grade>
   </student>
   <student id="3">
      <name>
         <first>Kate</first>
         <last>Wang</last>
         <middle>A</middle>
      </name>
      <grade>
         <points>72</points>
         <letter>C</letter>
      </grade>
   </student>
   
</students>

/////////////////////////////////////////////////////////////////////////////////////////
<%@ 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>
        <th>Note</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>
          <td>
            <x:choose>
              <x:when select="grade/points>90">You did
              great!</x:when>
              <x:when select="grade/points>80">You did
              good!</x:when>
              <x:when select="grade/points>75">You did
              ok.</x:when>
              <x:when select="grade/points>70">Well, you
              passed.</x:when>
              <x:otherwise>You failed</x:otherwise>
            </x:choose>
          </td>
        </tr>
      </x:forEach>
    </table>
  </body>
</html>





JSTL XML XPath 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>Reading RSS</title>
 </head>
 <body>
   <c:import var="news"
   url="http://www.wired.ru/news_drop/netcenter/netcenter.rdf" />
   <x:parse var="doc" xml="${news}" />
<x:forEach var="story" select="$doc/rss/channel/item">
         
           
             <x:out select="$doc/rss/channel/title" />
           
         
         
<x:out select="$doc/rss/channel/pubDate" />
         <x:out select="$doc/rss/channel/description" />



JSTL XML XPath: Wired New

   <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>Reading RSS</title>
 </head>
 <body>
   <c:import var="news"
   url="http://www.wired.ru/news_drop/netcenter/netcenter.rdf" />
   <x:parse var="doc" xml="${news}" />
<x:forEach var="story" select="$doc/rss/channel/item">
         
           
             <x:out select="$doc/rss/channel/title" />
           
         
         
<x:out select="$doc/rss/channel/pubDate" />
         <x:out select="$doc/rss/channel/description" />