This document was uploaded by user and they confirmed that they have the permission to share
it. If you are author or own the copyright of this book, please report to us by using this DMCA
report form. Report DMCA
<parent> ......... <dots> <dot x="13" y="15"/> <dot x="1" y="3"/> ... <description>This is a super long description that should be put between tags, as opposed to in an attribute...
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); // Step 2: create a DocumentBuilder DocumentBuilder db = dbf.newDocumentBuilder();
// Step 3: parse the input file to get a Document object Document doc = db.parse(new File(filename)); Node n = (Node) doc; ... int type = n.getNodeType(); for (Node child = n.getFirstChild(); child!=null; child=child.getNextSibling()) { ... } ttp://java.sun.com/developer/earlyAccess/xml/examples/DOMEcho/DOMEcho.jav
SAX vs. DOM
SAX -- Event Driven, Serial Access, Element by Element
Preferred for Server Side Apps
DOM -- Read in whole XML structure, CPU & memory intensive
Ideal for interactive apps (Thick Clients); allows interactive editing of XML structure in memory
Other Java Parsers
There are other free parsers out there, that use either SAX or DOM strategies... “nanoxml” “JDOM”
XML Uses
Config Files
Data Files
Apple Property List SVG
Messages
SOAP
Strengths and Weaknesses
Strengths Just Text: Compatible with Web/Internet Protocols Human + Machine Readable Represents most CS datastructures well Strict Syntax → Parsing Fast and
Weaknesses Verbose/Redundan t Trouble modeling overlapping data structures (non hierarchical)
Five Minute Break
JavaScript
Invented by Netscape Communications Cross Platform, Object-based, Scripting Language ECMAScript (ECMA-262)
JavaScript HTTP Request
Client runs JavaScript
HTTP Response HTML file with embedded JavaScript
JavaScript
All Client Side Can Adjust HTML Open Windows, Resize Windows Animations, Play Sounds
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 ... ...Regular HTML Here.... <SCRIPT TYPE="text/javascript"> ...More Regular HTML....
JavaScript and Browser
document – HTML document document.name – named element in document document.images – array of images document.forms – array of forms Ways to access window, cookies, etc.
Alert window.alert("Hello!"); Confirm window.confirm("Delete files? =D") Status Bar window.status = "Hi!";
JS Demo <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 language="JavaScript"> // We allocate a global array and fill it with the quote data. lines = new Array(); lines.push("Everybody's always telling me one thing and out the other."); lines.push("Even a fish could stay out of trouble if it would just learn to keep its mouth shut."); lines.push("Beware the lollipop of mediocrity -- lick it once and you suck forever."); lines.push("We don't have time to stop for gas -- we're already late."); lines.push("By doing just a little each day, I can gradually let the task overwhelm me."); lines.push("Being powerful is like being a lady. If you have to tell people you are, you aren't."); lines.push("Never attribute to malice that which can satisfactorily be explained by incompetence.");
// Search for an element with the given id and set its innerHTML to // the given text. function setText(id, text) { var node = document.getElementById(id); if (node != null) { node.innerHTML = text; //node.childNodes[0].data = text; // alternative for some simple tags } } // Given the name of a form, access the target field within that // form and using its target text, generate the quote list and put // it into the result tag. function setQuotes(form_name) { // cute: use [] instead of . to access the form by name var target = document[form_name].target.value; var contents = ""; target = target.toLowerCase(); for (var i in lines) { if (lines[i].toLowerCase().indexOf(target)!=-1) { contents = contents + "