JSTL XML Parse : XML « JSTL « Java






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.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/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-Parse.zip( 6,553 k)

Related examples in the same category

1.JSTL XML Foreach
2.JSTL XML Map Value
3.JSTL XML Output
4.JSTL XML XPath
5.JSTL XML XPath Choose
6.JSTL XML XPath Select
7.JSTL XML XPath: Wired New
8.JSTL: parse XML document