Web Programming Step by Step: What Is XML?
Web Programming Step by Step: What Is XML?
Lecture 20
XML
Reading: 10.3 - 10.4
Except where otherwise noted, the contents of this presentation are Copyright 2009 Marty
Stepp and Jessica Miller.
What is XML?
XML: a "skeleton" for creating markup languages
you already know it!
syntax is identical to XHTML's:
<element attribute="value">content</element>
Uses of XML
XML data comes from many sources on the web:
web servers store data as XML files
databases sometimes return query results as XML
web services use XML to communicate
XML is the de facto universal format for exchange of data
XML languages are used for music, math, vector graphics
popular use: RSS for news feeds & podcasts
Pros and cons of XML
pro:
con:
properties:
firstChild, lastChild, childNodes, nextSibling,
previousSibling, parentNode
nodeName, nodeType, nodeValue, attributes
methods:
appendChild, insertBefore, removeChild, replaceChild
getElementsByTagName, getAttribute, hasAttributes,
hasChildNodes
caution: cannot use HTML-specific properties like innerHTML in the XML DOM!
function functionName(ajax) {
do something with ajax.responseXML;
}
// ditto
var bar = foo.getElementsByTagName("bar")[0];
// array of length 2
var all_bazzes = foo.getElementsByTagName("baz");
// string "bleep"
var bloop = foo.getAttribute("bloop");
Exercise: Late day distribution
Write a program that shows how many students turn homework in late for each
assignment.
Data service here: http://webster.cs.washington.edu/hw/
parameter: assignment=hwn
// works
var first_baz = foo.getElementsByTagName("baz")[0];
// works - why?
var xyzzy = second_baz.firstChild;
Larger XML file example
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year><price>30.00</price>
</book>
<book category="computers">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<year>2003</year><price>49.99</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year><price>29.99</price>
</book>
<book category="computers">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year><price>39.95</price>
</book>
</bookstore>
<node nodeid="id">
<answer>answer</answer>
</node>
How do I retrieve data from the web app? (what URL, etc.)
Once I retrieve a piece of data, what should I do with it?
When the user clicks "Yes", what should I do?
When the user clicks "No", what should I do?
How do I know when the game is over? What should I do in this case?
Debugging responseXML in Firebug