XML Javascript
XML Javascript
Lecture 10
7-10pm
What is JSP?
Separate computation from
presentation
Presentation: JSP
Computation: Servlet
http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JAXPIntro.html
SAX
Subclass
DefaultHandler
Implement Notification
Methods ("Callbacks")
startElement(...) //
handles open tag
<a_tag>
endElement(...) //
http://sax.sourceforge.net/
handles close tag
</a_tag>
characters(...) //
handles stuff in
mydots.xml
<dots>
<dot x="81" y="67" />
<dot x="175" y="122" />
<flip>
<dot x="175" y="122" />
<dot x="209" y="71" />
</flip>
<dot x="209" y="71" />
<flip>
<flip><dot x="133" y="877">My Favorite
Dot!</dot></flip>
<dot x="1" y="2"/>
</flip>
</dots>
SAX Example
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(
new BufferedInputStream(new FileInputStream(
new File(argv[0]))), new XMLDotReader());
XMLDotReader Class
• Maintains flip state in boolean
• extends DefaultHandler
• overrides startElement(...)
• Check if dot tag or flip tag
• Do stuff, accordingly
• overrides endElement(...)
• If flip tag... "unflip"
DOM
Get a DocumentBuilder
Read in an XML File,
get a Document object
Traverse the Document
object and do stuff
http://java.sun.com/j2ee/1.4/docs/tutorial/
doc/JAXPDOM.html#wp79996
DOM Example
// Step 1: create a DocumentBuilderFactory
// and setNamespaceAware
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
HTTP Request
HTTP Response
Client runs
JavaScript HTML file with
embedded JavaScript
JavaScript
All Client Side
Can
Adjust HTML
Open Windows, Resize Windows
Cannot
Access File System
Do Networking
JavaScript
Advantages
Better User Experience
(2X Latency)
Disadvantages
Thicker Client
Possible Abuse
JavaScript Basics
<script> section in HTML runs on
document load
No type declarations
undefined if not given a value
Global variables by default
var makes them local
Generate Dynamic HTML
...
<BODY>
...Regular HTML Here....
<SCRIPT TYPE="text/javascript">
<!--
BUILD HTML HERE!!!
//-->
</SCRIPT>
www.devguru.com
DOM
JavaScript is Object-Oriented
JavaScript Interacts with Document
Object Model of Browser
DOM Not Totally Standardized
Objects
http://www.devguru.com
Arrays or Objects?
The Same!
a.foo <=> a["foo"] <=> a[x]
a[2] cannot be accessed as a.2
Global Functions
escape(string)
unescape(string)
Safe Strings
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
1234567890
@*-_+./
Unsafe Strings => %20, %5c, etc...
Communicating with
User
Alert window.alert("Hello!");
Confirm window.confirm("Delete files?
=D")
Status Bar window.status = "Hi!";
<html>
<head>
<title>JS Demo</title>
<script language="JavaScript">
function hello(greeting) {
var str = greeting + "!!!";
alert(str);
}
count = 0;
function upCount() {
count++;
alert(count);
}
function noFear() {
var fear = document.affirm.fear.value;
if (!document.affirm.mockMode.checked) {
alert("No " + fear + " to be seen around here!");
}
else {
var mock = "Being afraid of " + fear + " is stupid!";
window.status = mock
document.affirm.mock.value = mock;
}
}
</script>
</head>
<body>
<p>
<button onClick="hello('hi there');" >Say Hello</button>
<br><button onClick="upCount();" >Count</button>
<br><a onClick="alert('holy ####!')">oops</a>
<p>
Thing you are afraid of...
<form name=affirm>
<input type=text name=fear>
<p><input type=button onClick="noFear();" value="No Fear">
Mock mode:<input type=checkbox name=mockMode>
<p><input type=text size=40 name=mock>
</form>
</body>
</html>
Fear Example
Quotes Example
Text Box
<form name=form1
onsubmit="setQuotes('form1'); return false;">
Search: <input type=text name=target >
<input type=submit value=Submit>
</form>
Text Box
<input type=text name=target
onKeyUp="setQuotes('form2');">
<script language="JavaScript">