Except where otherwise noted, the contents of this presentation are Copyright 2010 Marty Stepp and Jessica Miller.
My note: BEGIN TO: Tove FROM: Jani SUBJECT: Reminder MESSAGE (english): Hey there, Don't forget to call me this weekend! END
<element attribute="value">content</element>
h1
, div
, img
, etc.id
/class
, src
, href
, etc.<?xml version="1.0" encoding="UTF-8"?> <!-- XML prolog --> <note> <!-- root element --> <to>Tove</to> <from>Jani</from> <!-- element ("tag") --> <subject>Reminder</subject> <!-- content of element --> <message language="english"> <!-- attribute and its value --> Don't forget me this weekend! </message> </note>
<?xml ... ?>
header tag ("prolog")note
)to
, from
, subject
book
, title
, author
<?xml version="1.0" encoding="UTF-8"?> <categories> <category>children</category> <category>computers</category> ... </categories>
The DOM properties and methods* we already know can be used on XML nodes:
firstChild
, lastChild
, childNodes
, nextSibling
, previousSibling
, parentNode
nodeName
, nodeType
, nodeValue
, attributes
appendChild
, insertBefore
, removeChild
, replaceChild
getElementsByTagName
, getAttribute
, hasAttributes
, hasChildNodes
innerHTML
in the XML DOM!
* (though not Prototype's, such as up
, down
, ancestors
, childElements
, or siblings
)
id
s or class
es to use to get specific nodes (no $
or $$
). Instead:
// returns all child tags inside node that use the given element
var elms = node.getElementsByTagName("tagName");
innerHTML
to get the text inside a node. Instead:
var text = node.firstChild.nodeValue;
.attributeName
to get an attribute's value from a node. Instead:
var attrValue = node.getAttribute("attrName");
XMLnode.getElementsByTagName("tag")
XMLelement.getAttribute("name")
, XMLelement.firstChild.nodeValue
, etc.document.createElement("tag")
, HTMLelement.innerHTML
HTMLelement.appendChild(element)
new Ajax.Request("url", { method: "get", onSuccess: functionName } ); ... function functionName(ajax) { do something with ajax.responseXML; }
ajax.responseText
contains the XML data in plain textajax.responseXML
is a pre-parsed XML DOM object<?xml version="1.0" encoding="UTF-8"?> <employees> <lawyer money="5"/> <janitor name="Sue"><vacuumcleaner/></janitor> <janitor name="Bill">too poor</janitor> </employees>
We can use DOM properties and methods on ajax.responseXML
:
// zeroth element of array of length 1 var employeesTag = ajax.responseXML.getElementsByTagName("employees")[0]; // how much money does the lawyer make? var lawyerTag = employeesTag.getElementsByTagName("lawyer")[0]; var salary = lawyerTag.getAttribute("money"); // "5" // array of 2 janitors var janitorTags = employeesTag.getElementsByTagName("janitor"); var excuse = janitorTags[1].firstChild.nodeValue; // " too poor "
<?xml version="1.0" encoding="UTF-8"?> <employees> <lawyer money="5"/> <janitor name="Bill"><vacuumcleaner/></janitor> <janitor name="Sue">too poor</janitor> </employees>
What are the results of the following expressions?
// zeroth element of array of length 1
var employeesTag = ajax.responseXML.getElementsByTagName("employees")[0];
employeesTag.firstChild
ajax.responseXML.getElementsByTagName("lawyer")
employeesTag.getElementsByTagName("janitor").length
employeesTag.getElementsByTagName("janitor")[0].firstChild
employeesTag.getElementsByTagName("janitor")[1].firstChild
employeesTag.getElementsByTagName("janitor")[0].nextSibling
<?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>
// make a paragraph for each book about computers var books = ajax.responseXML.getElementsByTagName("book"); for (var i = 0; i < books.length; i++) { var category = books[i].getAttribute("category"); if (category == "computers") { // extract data from XML var title = books[i].getElementsByTagName("title")[0].firstChild.nodeValue; var author = books[i].getElementsByTagName("author")[0].firstChild.nodeValue; // make an XHTML <p> tag containing data from XML var p = document.createElement("p"); p.innerHTML = title + ", by " + author; document.body.appendChild(p); } }
http://webster.cs.washington.edu/cse190m/hw/hw.php
assignment=hwN
animalgame.php
.<node nodeid="id"> <question>question</question> <yes nodeid="id" /> <no nodeid="id" /> </node>
<node nodeid="id"> <answer>answer</answer> </node>
animalgame.php?nodeid=id
nodeid
of 1
to get the first questionQuestions we should ask ourselves:
responseXML
in Firebug