Except where otherwise noted, the contents of this document are Copyright 2012 Marty Stepp, Jessica Miller, and Victoria Kirst. All rights reserved. Any redistribution, reproduction, transmission, or storage of part or all of the contents in any form is prohibited without the author's expressed written permission.
My note: BEGIN FROM: Alice Smith (alice@example.com) TO: Robert Jones (roberto@example.com) SUBJECT: Tomorrow's "Birthday Bash" event! MESSAGE (english): Hey Bob, Don't forget to call me this weekend! PRIVATE: true END
<?xml version="1.0" encoding="UTF-8"?> <note private="true"> <from>Alice Smith (alice@example.com)</from> <to>Robert Jones (roberto@example.com)</to> <subject>Tomorrow's "Birthday Bash" event!</subject> <message language="english"> Hey Bob, Don't forget to call me this weekend! </message> </note>
<element attribute="value">content</element>
h1
, div
, img
, etc.id
/class
, src
, href
, etc.<?xml version="1.0" encoding="UTF-8"?> <!-- XML prolog --> <note private="true"> <!-- root element --> <from>Alice Smith (alice@example.com)</from> <to>Robert Jones (roberto@example.com)</to> <subject>Tomorrow's "Birthday Bash" event!</subject> <message language="english"> Hey Bob, Don't forget to call me this weekend! </message> </note>
<?xml ... ?>
header tag (prolog)note
)<measure number="1"> <attributes> <divisions>1</divisions> <key><fifths>0</fifths></key> <time><beats>4</beats></time> <clef> <sign>G</sign><line>2</line> </clef> </attributes> <note> <pitch> <step>C</step> <octave>4</octave> </pitch> <duration>4</duration> <type>whole</type> </note> </measure>
book
, title
, author
key
, pitch
, note
var ajax = new XMLHttpRequest(); ajax.onload = functionName; ajax.open("GET", url, true); ajax.send(); ... function functionName() { do something with this.responseXML; }
this.responseText
contains the data in plain text (a string)this.responseXML
is a parsed XML DOM tree object<?xml version="1.0" encoding="UTF-8"?> <categories> <category>children</category> <category>computers</category> ... </categories>
<p> This is a paragraph of text with a <a href="/path/page.html">link in it</a>. </p>
every node's DOM object has the following properties:
name(s) | description |
---|---|
firstChild , lastChild
|
start/end of this node's list of children |
childNodes
|
array of all this node's children |
nextSibling , previousSibling
|
neighboring nodes with the same parent |
parentNode
|
the element that contains this node |
nodeName
, nodeType
, nodeValue
To get a list of all nodes that use a given element:
var elms = node.getElementsByTagName("tag");
To get the text inside of a node:
var text = node.firstChild.nodeValue;
To get an attribute's value from a node:
var attrValue = node.getAttribute("name");
Can't get a list of nodes by id or class using getElementById
or querySelectorAll
:
var elms =document.querySelectorAll("#main li");document.getElementById("id");
Can't get/set the text inside of a node using innerHTML
:
var text =document.getElementById("foo").innerHTML= "hi";
Can't get an attribute's value using .attributeName
:
var imageUrl =document.getElementById("myimage").src= "foo.jpg";
nodeName
, nodeType
, nodeValue
, attributes
firstChild
, lastChild
, childNodes
, nextSibling
, previousSibling
, parentNode
getElementsByTagName
, getAttribute
, hasAttribute
, hasChildNodes
appendChild
, insertBefore
, removeChild
, replaceChild
<?xml version="1.0" encoding="UTF-8"?> <employees> <lawyer money="99999.00" /> <janitor name="Ed"> <vacuum model="Hoover" /> </janitor> <janitor name="Bill">no vacuum, too poor</janitor> </employees>
// how much money does the lawyer make? var lawyer = this.responseXML.getElementsByTagName("lawyer")[0]; var salary = lawyer.getAttribute("money"); // "99999.00" // array of 2 janitors var janitors = this.responseXML.getElementsByTagName("janitor"); var vacModel = janitors[0].getElementsByTagName("vacuum")[0].getAttribute("model"); // "Hoover" var excuse = janitors[1].firstChild.nodeValue; // "no vacuum, too poor"
<?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 = this.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 HTML <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/cse154/hw/hw.php
assignment=hwN
animalgame.php
.<node nodeid="id"> <question>question text</question> <yes nodeid="id" /> <no nodeid="id" /> </node>
<node nodeid="id"> <answer>answer text</answer> </node>
animalgame.php?nodeid=id
nodeid
of 1
to get the first questionQuestions we should ask ourselves:
responseXML
in Firebug<!DOCTYPE html>
tag)