Except where otherwise noted, the contents of this presentation are © Copyright 2007 Marty Stepp and are licensed under the Creative Commons Attribution 2.5 License.
<?xml version="1.0" encoding="UTF-8"?>
document tag
<element attributes> text or tags </element>
name="value"
<!-- comment -->
<?xml version="1.0" encoding="UTF-8"?> <note> <to>Tove</to> <from>Jani</from> <subject>Reminder</subject> <message> Don't forget me this weekend! </message> </note>
<?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>
XMLHttpRequest
objectXMLHttpRequest
template for XMLvar ajax = new XMLHttpRequest(); ajax.onreadystatechange = function() { if (ajax.readyState == 4) { do something with ajax.responseXML; } }; ajax.open("GET", url, true); ajax.send(null);
responseXML
returns response as an XML document treeresponseXML
treefirstChild
, lastChild
, childNodes
, nextSibling
, previousSibling
, parentNode
nodeName
, nodeType
, nodeValue
, attributes
appendChild
, insertBefore
, removeChild
, replaceChild
getElementsByTagName
, getAttribute
, hasAttributes
, hasChildNodes
nodeType
: what kind of node it is
Kind of node | nodeType value |
---|---|
element | 1 |
attribute | 2 |
text | 3 |
comment | 8 |
document | 9 |
nodeName
: uppercase version of tag such as "DIV"
or "ARTICLE"
"#text"
"#document"
nodeValue
: text inside a text node, or value of an attribute nodeelement.getElementsByTagName("tag")
"p"
, "div"
, etc.)document
or on a specific nodeelement.getAttribute("attributeName")
<?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> ... </bookstore>
var xmlDoc = ajax.responseXML; var books = xmlDoc.getElementsByTagName("book"); for (var i = 0; i < books.length; i++) { var category = books[i].getAttribute("category"); if (category == "computers") { var title = books[i].getElementsByTagName("title")[0].firstChild.nodeValue; var author = books[i].getElementsByTagName("author")[0].firstChild.nodeValue; var p = document.createElement("p"); p.innerHTML = title + ", by " + author; document.body.appendChild(p); } }
<book>
element about computers<node id="id"> <question>question</question> <yes id="id" /> <no id="id" /> </node>
<node id="id"> <answer>answer</answer> </node>
function ajaxHelper(url, fn) { // calls fn when data arrives var ajax = new XMLHttpRequest(); ajax.onreadystatechange = function() { if (ajax.readyState == 4) { if (ajax.status == 200) { fn(ajax); } else { alert("Error making Ajax request to URL:\n" + url + "\n\nServer status:\n" + ajax.status + " " + ajax.statusText + "\n\n" + "Server response text:\n" + ajax.responseText); } } }; ajax.open("GET", url, true); ajax.send(null); }
ajaxHelper("url", functionName); ... function functionName(ajax) { do something with ajax.responseText or ajax.responseXML; }
ajaxHelper("http://www.example.com/foo.html", myFunction);
...
function myFunction(ajax) {
alert(ajax.responseText);
}
XMLHttpRequest
restrictions