JSP and XML
adk46.xml
<?xml
version = '1.0' encoding = 'UTF-8'?>
<HighPeaks>
<Peak id="A1" start="
<Name>Marcy</Name>
<Height>5344</Height>
</Peak>
<Peak id="A2" start="
<Name>Algonquin</Name>
<Height>5114</Height>
<Climbed>
<Climbed>
</Peak>
<Peak id="A3"
start="Garden" with="A9 A17">
<Name>Haystack</Name>
<Height>4960</Height>
<Climbed>
</Peak>
<Peak id="A4" start="
<Name>Skylight</Name>
<Height>4926</Height>
<Climbed>
<HerdPath/>
</Peak>
<Peak id="A5"
start="Observation Station" with="A28">
<Name>Whiteface</Name>
<Height>4867</Height>
<Climbed>
</Peak>
<Peak id="A6" start="
<Name>Dix</Name>
<Height>4857</Height>
<Climbed>
<Climbed recent="yes">
<Climbed recent="yes">
</Peak>
<Peak id="A7" start="
<Name>Gray</Name>
<Height>4840</Height>
<Climbed>
<HerdPath/>
</Peak>
<Peak id="A8" start="
<Name>Iroquois</Name>
<Height>4840</Height>
<Climbed>
</Peak>
<Peak id="A9"
start="Garden" with="A3 A17">
<Name>Basin</Name>
<Height>4827</Height>
<Climbed>
</Peak>
<Peak id="A10"
start="Gatehouse" with="A22 A29 A35">
<Name>Gothics</Name>
<Height>4736</Height>
<Climbed>
</Peak>
<Peak id="A11" start="
<Name>Colden</Name>
<Height>4714</Height>
<Climbed>
</Peak>
<Peak id="A12" start="Rt
73">
<Name>
<Height>4627</Height>
<Climbed>
</Peak>
<Peak id="A13" start="
<Name>Nippletop</Name>
<Height>4620</Height>
<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>
</Peak>
<Peak id="A16" start="
<Name>Wright</Name>
<Height>4580</Height>
<Climbed>
<Climbed>
</Peak>
<Peak id="A17"
start="Gatehouse" with="A3 A9">
<Name>Saddleback</Name>
<Height>4515</Height>
<Climbed>
</Peak>
<Peak id="A18"
start="Upper Works" with="A14 A46">
<Name>Panther</Name>
<Height>4442</Height>
</Peak>
<Peak id="A19" start="
<Name>Table Top</Name>
<Height>4427</Height>
<Climbed>
</Peak>
<Peak id="A20" start="Rt
73">
<Name>Rocky Peak
Ridge</Name>
<Height>4420</Height>
<Climbed>
</Peak>
<Peak id="A21" start="
<Name>
<Height>4405</Height>
<Climbed>
</Peak>
<Peak id="A22"
start="Gatehouse" with="A10 A29 A35">
<Name>Armstrong</Name>
<Height>4400</Height>
<Climbed>
</Peak>
<Peak id="A23" start="
<Name>Hough</Name>
<Height>4400</Height>
<Climbed>
</Peak>
<Peak id="A24"
start="Coreys" with="A33 A40">
<Name>Seward</Name>
<Height>4361</Height>
<Climbed>
</Peak>
<Peak id="A25"
start="Upper Works">
<Name>
<Height>4360</Height>
</Peak>
<Peak id="A26"
start="Upper Works">
<Name>Allen</Name>
<Height>4340</Height>
<Climbed>
</Peak>
<Peak id="A27"
start="Garden">
<Name>Big Slide</Name>
<Height>4240</Height>
<Climbed>
<Climbed>
</Peak>
<Peak id="A28"
start="Observation Station" with="A5">
<Name>Esther</Name>
<Height>4240</Height>
<Climbed>
</Peak>
<Peak id="A29"
start="Gatehouse" with="A10 A22 A35">
<Name>Upper Wolf
Jaw</Name>
<Height>4185</Height>
<Climbed>
</Peak>
<Peak id="A30"
start="Gatehouse">
<Name>Lower Wolf
Jaw</Name>
<Height>4175</Height>
<Climbed>
</Peak>
<Peak id="A31" start="
<Name>Street</Name>
<Height>4166</Height>
<Climbed>
</Peak>
<Peak id="A32" start="
<Name>Phelps</Name>
<Height>4161</Height>
<Climbed>
</Peak>
<Peak id="A33"
start="Coreys" with="A24 A40">
<Name>Donaldson</Name>
<Height>4140</Height>
<Climbed>
</Peak>
<Peak id="A34"
start="Coreys">
<Name>
<Height>4120</Height>
</Peak>
<Peak id="A35"
start="Gatehouse" with="A10 A22 A29">
<Name>Sawteeth</Name>
<Height>4100</Height>
<Climbed>
</Peak>
<Peak id="A36" start="Rt
73" with="A38">
<Name>Cascade</Name>
<Height>4098</Height>
<Climbed>
<Climbed>
</Peak>
<Peak id="A37" start="
<Name>South Dix</Name>
<Height>4060</Height>
<Climbed>
</Peak>
<Peak id="A38" start="Rt
73" with="A36">
<Name>Porter</Name>
<Height>4059</Height>
<Climbed>
<Climbed>
</Peak>
<Peak id="A39"
start="Gatehouse" with="A43">
<Name>Colvin</Name>
<Height>4057</Height>
<Climbed>
</Peak>
<Peak id="A40" start="Coreys"
with="A24 A33">
<Name>Emmons</Name>
<Height>4040</Height>
<Climbed>
</Peak>
<Peak id="A41"
start="Gatehouse" with="A13">
<Name>Dial</Name>
<Height>4020</Height>
<Climbed>
</Peak>
<Peak id="A42" start="
<Name>East Dix</Name>
<Height>4012</Height>
<Climbed>
</Peak>
<Peak id="A43"
start="Gatehouse" with="A39">
<Name>Blake</Name>
<Height>3960</Height>
<Climbed>
</Peak>
<Peak id="A44"
start="Upper Works">
<Name>Cliff</Name>
<Height>3960</Height>
</Peak>
<Peak id="A45" start="
<Name>Nye</Name>
<Height>3895</Height>
<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
Name (#PCDATA)>
<!ELEMENT
Height (#PCDATA)>
<!ELEMENT
Climbed (#PCDATA)>
<!ELEMENT
HerdPath EMPTY>
<!
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
<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
<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> <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
<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> <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>
<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
<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> <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>
<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> <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>
<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());
}
}
}