JSP and XML

 

adk46.xml

 

<?xml version = '1.0' encoding = 'UTF-8'?>

<HighPeaks>

      <Peak id="A1" start="Heart Lake">

            <Name>Marcy</Name>

            <Height>5344</Height>

      </Peak>

      <Peak id="A2" start="Heart Lake" with="A8 A16">

            <Name>Algonquin</Name>

            <Height>5114</Height>

            <Climbed>9/28/2002</Climbed>

            <Climbed>9/7/2004</Climbed>

      </Peak>

      <Peak id="A3" start="Garden" with="A9 A17">

            <Name>Haystack</Name>

            <Height>4960</Height>

            <Climbed>7/31/2003</Climbed>

      </Peak>

      <Peak id="A4" start="Heart Lake" with="A7">

            <Name>Skylight</Name>

            <Height>4926</Height>

            <Climbed>8/25/2004</Climbed>

            <HerdPath/>

      </Peak>

      <Peak id="A5" start="Observation Station" with="A28">

            <Name>Whiteface</Name>

            <Height>4867</Height>

            <Climbed>10/5/2003</Climbed>

      </Peak>

      <Peak id="A6" start="Elm Lake" with="A21 A23 A37 A42">

            <Name>Dix</Name>

            <Height>4857</Height>

            <Climbed>7/3/2004</Climbed>

   <Climbed recent="yes">12/4/2005</Climbed>

   <Climbed recent="yes">12/5/2005</Climbed>

   </Peak>

      <Peak id="A7" start="Heart Lake" with="A4">

            <Name>Gray</Name>

            <Height>4840</Height>

            <Climbed>8/25/2004</Climbed>

            <HerdPath/>

      </Peak>

      <Peak id="A8" start="Heart Lake" with="A2">

            <Name>Iroquois</Name>

            <Height>4840</Height>

            <Climbed>9/28/2002</Climbed>

      </Peak>

      <Peak id="A9" start="Garden" with="A3 A17">

            <Name>Basin</Name>

            <Height>4827</Height>

            <Climbed>7/31/2003</Climbed>

      </Peak>

      <Peak id="A10" start="Gatehouse" with="A22 A29 A35">

            <Name>Gothics</Name>

            <Height>4736</Height>

            <Climbed>10/13/2003</Climbed>

      </Peak>

      <Peak id="A11" start="Heart Lake">

            <Name>Colden</Name>

            <Height>4714</Height>

            <Climbed>6/13/2004</Climbed>

      </Peak>

      <Peak id="A12" start="Rt 73">

            <Name>Giant Mt.</Name>

            <Height>4627</Height>

            <Climbed>9/19/2004</Climbed>

      </Peak>

      <Peak id="A13" start="Heart Lake" with="A41">

            <Name>Nippletop</Name>

            <Height>4620</Height>

            <Climbed>6/15/2004</Climbed>

      </Peak>

      <Peak id="A14" start="Upper Works" with="A18 A46">

            <Name>Santanoni</Name>

            <Height>4607</Height>

      </Peak>

      <Peak id="A15" start="Upper Works">

            <Name>Redfield</Name>

            <Height>4606</Height>

            <Climbed>8/17/2004</Climbed>

      </Peak>

      <Peak id="A16" start="Heart Lake">

            <Name>Wright</Name>

            <Height>4580</Height>

            <Climbed>9/28/2002</Climbed>

            <Climbed>9/7/2004</Climbed>

      </Peak>

      <Peak id="A17" start="Gatehouse" with="A3 A9">

            <Name>Saddleback</Name>

            <Height>4515</Height>

            <Climbed>7/31/2003</Climbed>

      </Peak>

      <Peak id="A18" start="Upper Works" with="A14 A46">

            <Name>Panther</Name>

            <Height>4442</Height>

      </Peak>

      <Peak id="A19" start="Heart Lake">

            <Name>Table Top</Name>

            <Height>4427</Height>

            <Climbed>7/14/2004</Climbed>

      </Peak>

      <Peak id="A20" start="Rt 73">

            <Name>Rocky Peak Ridge</Name>

            <Height>4420</Height>

            <Climbed>5/3/2005</Climbed>

      </Peak>

      <Peak id="A21" start="Elm Lake" with="A6 A23 A37 A42">

            <Name>Macomb</Name>

            <Height>4405</Height>

            <Climbed>7/3/2004</Climbed>

      </Peak>

      <Peak id="A22" start="Gatehouse" with="A10 A29 A35">

            <Name>Armstrong</Name>

            <Height>4400</Height>

            <Climbed>10/13/2003</Climbed>

      </Peak>

      <Peak id="A23" start="Elm Lake" with="A6 A21 A37 A42">

            <Name>Hough</Name>

            <Height>4400</Height>

            <Climbed>7/3/2004</Climbed>

      </Peak>

      <Peak id="A24" start="Coreys" with="A33 A40">

            <Name>Seward</Name>

            <Height>4361</Height>

            <Climbed>9/2/2004</Climbed>

      </Peak>

      <Peak id="A25" start="Upper Works">

            <Name>Marshall</Name>

            <Height>4360</Height>

      </Peak>

      <Peak id="A26" start="Upper Works">

            <Name>Allen</Name>

            <Height>4340</Height>

            <Climbed>8/4/2004</Climbed>

      </Peak>

      <Peak id="A27" start="Garden">

            <Name>Big Slide</Name>

            <Height>4240</Height>

            <Climbed>9/7/2002</Climbed>

            <Climbed>9/10/2003</Climbed>

      </Peak>

      <Peak id="A28" start="Observation Station" with="A5">

            <Name>Esther</Name>

            <Height>4240</Height>

            <Climbed>10/5/2003</Climbed>

      </Peak>

      <Peak id="A29" start="Gatehouse" with="A10 A22 A35">

            <Name>Upper Wolf Jaw</Name>

            <Height>4185</Height>

            <Climbed>10/13/2003</Climbed>

      </Peak>

      <Peak id="A30" start="Gatehouse">

            <Name>Lower Wolf Jaw</Name>

            <Height>4175</Height>

            <Climbed>10/14/2002</Climbed>

      </Peak>

      <Peak id="A31" start="Heart Lake" with="A45">

            <Name>Street</Name>

            <Height>4166</Height>

            <Climbed>6/1/2003</Climbed>

      </Peak>

      <Peak id="A32" start="Heart Lake">

            <Name>Phelps</Name>

            <Height>4161</Height>

            <Climbed>8/6/2002</Climbed>

      </Peak>

      <Peak id="A33" start="Coreys" with="A24 A40">

            <Name>Donaldson</Name>

            <Height>4140</Height>

            <Climbed>9/2/2004</Climbed>

      </Peak>

      <Peak id="A34" start="Coreys">

            <Name>Seymour</Name>

            <Height>4120</Height>

      </Peak>

      <Peak id="A35" start="Gatehouse" with="A10 A22 A29">

            <Name>Sawteeth</Name>

            <Height>4100</Height>

            <Climbed>10/13/2003</Climbed>

      </Peak>

      <Peak id="A36" start="Rt 73" with="A38">

            <Name>Cascade</Name>

            <Height>4098</Height>

            <Climbed>7/30/2002</Climbed>

            <Climbed>9/3/2003</Climbed>

      </Peak>

      <Peak id="A37" start="Elm Lake" with="A6 A21 A23 A42">

            <Name>South Dix</Name>

            <Height>4060</Height>

            <Climbed>7/3/2004</Climbed>

      </Peak>

      <Peak id="A38" start="Rt 73" with="A36">

            <Name>Porter</Name>

            <Height>4059</Height>

            <Climbed>7/30/2002</Climbed>

            <Climbed>9/3/2003</Climbed>

      </Peak>

      <Peak id="A39" start="Gatehouse" with="A43">

            <Name>Colvin</Name>

            <Height>4057</Height>

            <Climbed>9/14/2003</Climbed>

      </Peak>

      <Peak id="A40" start="Coreys" with="A24 A33">

            <Name>Emmons</Name>

            <Height>4040</Height>

            <Climbed>9/2/2004</Climbed>

      </Peak>

      <Peak id="A41" start="Gatehouse" with="A13">

            <Name>Dial</Name>

            <Height>4020</Height>

            <Climbed>6/15/2003</Climbed>

      </Peak>

      <Peak id="A42" start="Elm Lake" with="A6 A21 A23 A37">

            <Name>East Dix</Name>

            <Height>4012</Height>

            <Climbed>7/3/2004</Climbed>

      </Peak>

      <Peak id="A43" start="Gatehouse" with="A39">

            <Name>Blake</Name>

            <Height>3960</Height>

            <Climbed>9/14/2003</Climbed>

      </Peak>

      <Peak id="A44" start="Upper Works">

            <Name>Cliff</Name>

            <Height>3960</Height>

      </Peak>


      <Peak id="A45" start="Heart Lake" with="A31">

            <Name>Nye</Name>

            <Height>3895</Height>

            <Climbed>6/1/2003</Climbed>

      </Peak>

      <Peak id="A46" start="Upper Works" with="A14 A18">

            <Name>Couchsachraga</Name>

            <Height>3820</Height>

      </Peak>

</HighPeaks>

 

 

 

adk46.dtd

 

<!ELEMENT HighPeaks (Peak+)>

<!ELEMENT Peak (Name, Height, Climbed*, HerdPath?)>

<!ELEMENT Name (#PCDATA)>

<!ELEMENT Height (#PCDATA)>

<!ELEMENT Climbed (#PCDATA)>

<!ELEMENT HerdPath EMPTY>

<!ATTLIST Peak

      id ID #REQUIRED

      start CDATA #IMPLIED

      with IDREFS #IMPLIED

>

<!ATTLIST Climbed

      recent CDATA #IMPLIED

>

 


ex1.jsp

 

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>

<%@ taglib uri="http://java.sun.com/jstl/xml" prefix="x"%>

<html>

  <head>

    <title>ex1</title>

  </head>

  <body>

    <h1>Information about the High Peaks</h1>

    <c:import var="xmlFile" url="ADK46.xml" />

    <x:parse var="myDoc" xml="${xmlFile}" />

    <table border="2">

    <tr><th>Name</th><th>Height</th></tr>

    <x:forEach select="$myDoc/HighPeaks/Peak"

               var="mountain">

      <tr>

        <td><x:out select="$mountain/Name" /></td>

        <td><x:out select="$mountain/Height" /></td>

      </tr>

    </x:forEach>

  </body>

</html>

 


ex2.jsp

 

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>

<%@ taglib uri="http://java.sun.com/jstl/xml" prefix="x"%>

<html>

  <head>

    <title>ex1</title>

  </head>

  <body>

    <h1>Information about the High Peaks</h1>

    <c:import var="xmlFile" url="ADK46.xml" />

    <x:parse var="myDoc" xml="${xmlFile}" />

    <table border="2">

    <tr><th>Name</th><th>Height</th>

        <th>Climbed</th><th>Herd Path</th></tr>

    <x:forEach select="$myDoc/HighPeaks/Peak"

               var="mountain">

      <tr>

        <td><x:out select="$mountain/Name" /></td>

        <td><x:out select="$mountain/Height" /></td>

        <td>&nbsp;<ul>

            <x:forEach select="$mountain/Climbed"

                       var="climbdate">

              <li><x:out select="$climbdate" /></li>

            </x:forEach>

            </ul>

        </td>

        <td>

          <x:choose>

            <x:when select="$mountain/HerdPath">

             Yes

            </x:when>

            <x:otherwise>

              No

            </x:otherwise>

          </x:choose>

        </td>

      </tr>

    </x:forEach>

  </body>

</html>

 


ex3.jsp

 

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>

<%@ taglib uri="http://java.sun.com/jstl/xml" prefix="x"%>

<html>

  <head>

    <title>ex1</title>

  </head>

  <body>

    <h1>Information about the High Peaks</h1>

    <c:import var="xmlFile" url="ADK46.xml" />

    <x:parse var="myDoc" xml="${xmlFile}" />

    <table border="2">

    <tr><th>Name</th><th>Height</th><th>Start</th>

        <th>Climbed</th><th>Herd Path</th>

        <th>Along With</th></tr>

    <x:forEach select="$myDoc/HighPeaks/Peak"

               var="mountain">

      <tr>

        <td><x:out select="$mountain/Name" /></td>

        <td><x:out select="$mountain/Height" /></td>

        <td><x:out select="$mountain/@start" /></td>       

        <td>&nbsp;<ul>

            <x:forEach select="$mountain/Climbed"

                       var="climbdate">

              <li><x:out select="$climbdate" /></li>

            </x:forEach>

            </ul>

        </td>

        <td>

          <x:choose>

            <x:when select="$mountain/HerdPath">

             Yes

            </x:when>

            <x:otherwise>

              No

            </x:otherwise>

          </x:choose>

        </td>

        <td>&nbsp;

            <x:set select="$mountain/@with"

                       var="with" />

            <c:set var="myWiths">

              <x:out select="$with" />

            </c:set>

             <c:forTokens items="${myWiths}"

               delims=" "

               var="current">

              <br><c:out value="${current}" />

            </c:forTokens>

        </td>

      </tr>

    </x:forEach>

  </body>

</html>

 


ex4.jsp

 

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>

<%@ taglib uri="http://java.sun.com/jstl/xml" prefix="x"%>

<html>

  <head>

    <title>ex1</title>

  </head>

  <body>

    <h1>Information about the High Peaks</h1>

    <c:import var="xmlFile" url="ADK46.xml" />

    <x:parse var="myDoc" xml="${xmlFile}" />

    <table border="2">

    <tr><th>Name</th><th>Height</th><th>Start</th>

        <th>Climbed</th><th>Herd Path</th>

        <th>Along With</th></tr>

    <x:forEach select="$myDoc/HighPeaks/Peak" var="mountain">

      <tr>

        <td><x:out select="$mountain/Name" /></td>

        <td><x:out select="$mountain/Height" /></td>

        <td><x:out select="$mountain/@start" /></td>       

        <td>&nbsp;<ul>

            <x:forEach select="$mountain/Climbed"

                       var="climbdate">

              <li><x:out select="$climbdate" /></li>

            </x:forEach>

            </ul>

        </td>

        <td>

          <x:choose>

            <x:when select="$mountain/HerdPath">

             Yes

            </x:when>

            <x:otherwise>

              No

            </x:otherwise>

          </x:choose>

        </td>

        <td>&nbsp;

            <x:set select="$mountain/@with"

                       var="with" />

            <c:set var="myWiths">

              <x:out select="$with" />

            </c:set>

            <ul>

              <c:forTokens items="${myWiths}"

                delims=" "

                var="current">

                <li>

                  <x:out select="$myDoc//Peak[@id=$current]/Name" />

                </li>

              </c:forTokens>

            </ul>

        </td>

      </tr>

    </x:forEach>

  </body>

</html>

ex5.htm

 

<HTML>

<HEAD>

<TITLE>Information about a mountain</TITLE>

</HEAD>

<BODY>

<FORM

  ACTION="ex5.jsp"

  METHOD="POST">

  Enter the mountain:  <INPUT TYPE="text" NAME="mountain">

  <BR><INPUT TYPE="submit" VALUE="Enter">

</FORM>

</HTML>

 

 


ex5.jsp

 

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>

<%@ taglib uri="http://java.sun.com/jstl/xml" prefix="x"%>

<html>

  <head>

    <title>ex5</title>

  </head>

  <body>

    <h1>Information about

        <c:out value="${param.mountain}" />

    </h1>

    <c:import var="xmlFile" url="ADK46.xml" />

    <x:parse var="myDoc" xml="${xmlFile}" />

    <x:choose>

    <x:when select="$myDoc/HighPeaks/Peak[Name=$param:mountain]">

    <x:set select="$myDoc/HighPeaks/Peak[Name=$param:mountain]"

               var="mountain" />

    <P>Height: <x:out select="$mountain/Height" />

    <P>Start: <x:out select="$mountain/@start" />       

    <P>Dates climbed:

    <x:forEach select="$mountain/Climbed" var="climbdate">

      <br>&nbsp;&nbsp;<x:out select="$climbdate" />

    </x:forEach>

    <P>Herdpath:

    <x:choose>

      <x:when select="$mountain/HerdPath">

        Yes

      </x:when>

      <x:otherwise>

        No

      </x:otherwise>

     </x:choose>

     <P>Along with:

     <x:set select="$mountain/@with" var="with" />

     <c:set var="myWiths">

       <x:out select="$with" />

     </c:set>

     <c:forTokens items="${myWiths}" delims=" " var="current">

        <br>&nbsp;&nbsp;

            <x:out select="$myDoc//Peak[@id=$current]/Name" />

     </c:forTokens>

     </x:when>

     <x:otherwise>

       Not Found

     </x:otherwise>

     </x:choose>

  </body>

</html>

 


ex6.jsp

 

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>

<%@ taglib uri="http://java.sun.com/jstl/xml" prefix="x"%>

<html>

  <head>

    <title>ex6</title>

  </head>

  <body>

    <h1>Enter Information about a Climb</h1>

    <c:import var="xmlFile" url="ADK46.xml" />

    <x:parse var="myDoc" xml="${xmlFile}" />

    <form action="ex7.jsp" method="POST">

      Mountain:

      <select name="mountain">

        <x:forEach select="$myDoc/HighPeaks/Peak"

               var="mountain">

          <c:set var="this_mountain">

            <x:out select="$mountain/Name" />

          </c:set>

          <option value="<c:out value="${this_mountain}" />">

            <c:out value="${this_mountain}" />

          </option>

        </x:forEach>

      </select>

      <p>Date Climbed

        <input type="text" name="date">

      <p>

        <input type="submit" value="Enter">

    </form>

  </body>

</html>

 

 

ex7.jsp

 

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>

<%@ taglib uri="http://java.sun.com/jstl/xml" prefix="x"%>

<html>

  <head>

    <title>ex7</title>

  </head>

  <body>

    <jsp:useBean id="xmlBean" class="package12.Bean7">

      <jsp:setProperty name="xmlBean" property="*"/>

    </jsp:useBean>

    <jsp:setProperty name="xmlBean" property="save" value=""/>

    <h1>Information recorded!</h1>   

  </body>

</html>

 

 


Bean7.java

 

package package12;

 

import java.io.*;

import javax.swing.JFileChooser;

import javax.xml.parsers.*;

import javax.xml.transform.*;

import javax.xml.transform.dom.*;

import javax.xml.transform.stream.*;

import org.w3c.dom.*;

import org.xml.sax.*;

 

 

public class Bean7

{

  Document document;

  String mountain = "";

  String date = "";

  File xmlFile;

 

  public Bean7()

  {

    xmlFile = new File("C:\\temp\\ADK46.xml");

    try

    {

      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

      DocumentBuilder builder = factory.newDocumentBuilder();

      builder.setErrorHandler(new MyErrorHandler());

      document = builder.parse( xmlFile );

    }

    catch (Exception e)

    {   

    }

  }

 

  public void setMountain(String mountain)

  {

    this.mountain = mountain;

  }

 

  public void setDate(String date)

  {

    this.date = date;

  }

 


  public void setSave(String temp)

  {

    try

    {

      Node root = document.getDocumentElement();

      Element elementRoot = (Element) root;

      NodeList peaks = elementRoot.getElementsByTagName("Peak");

      for (int i = 0; i < peaks.getLength(); i++)

      {

         Element peak = (Element) peaks.item(i);

         NodeList names = peak.getElementsByTagName("Name");

         if (names.item(0).getFirstChild().getNodeValue().equals(mountain))

         {

           Element climbedNode = document.createElement("Climbed");

           Attr recentAttribute = document.createAttribute("recent");

           recentAttribute.setValue("yes");

           climbedNode.setAttributeNode(recentAttribute);

           climbedNode.appendChild( document.createTextNode(date));

           peak.appendChild(climbedNode);

         }

      }

      Source xmlSource = new DOMSource(document);

      Result storedResult = new StreamResult(xmlFile);

      Result storedResult1 = new StreamResult(System.out);

      TransformerFactory transformerFactory =

        TransformerFactory.newInstance();

      Transformer transformer = transformerFactory.newTransformer();

      transformer.setOutputProperty("indent", "yes");

      transformer.transform(xmlSource, storedResult1); 

      transformer.transform(xmlSource, storedResult);    

    } // try

    catch (Exception e)

    {

        e.printStackTrace();

    }

  }

 

  private class MyErrorHandler implements ErrorHandler

  {

    // throw SAXException for fatal errors

    public void fatalError(SAXParseException exception) throws SAXException

    {

      throw exception;

    }

 

    public void error(SAXParseException exception) throws SAXParseException

    {

      throw exception;

    }

 

    // print warnings

    public void warning(SAXParseException exception) throws SAXParseException

    {

      System.err.println("Warning: " + exception.getMessage());

    }

  }

}