XML and AJAX Lecture 28

Similar documents
XML Attributes. Lecture 33. Robb T. Koether. Hampden-Sydney College. Wed, Apr 25, 2018

XPath. Lecture 36. Robb T. Koether. Wed, Apr 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, / 28

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

AJAX. Lecture 26. Robb T. Koether. Fri, Mar 21, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) AJAX Fri, Mar 21, / 16

The Critical-Path Algorithm

Minimal Spanning Trees

DTDs and XML Attributes

PHP Queries and HTML Forms Lecture 23

The Decreasing-Time Algorithm

Binary Tree Applications

Form Validation. Lecture 25. Robb T. Koether. Hampden-Sydney College. Wed, Mar 23, 2018

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Building the Abstract Syntax Trees

Sampling Distribution Examples Sections 15.4, 15.5

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

Introduction to Databases

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

Webpage Navigation. Lecture 27. Robb T. Koether. Hampden-Sydney College. Mon, Apr 2, 2018

Basic PHP. Lecture 19. Robb T. Koether. Hampden-Sydney College. Mon, Feb 26, 2108

Function Definition Syntax Tree

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Rotations and Translations

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

Basic HTML. Lecture 14. Robb T. Koether. Hampden-Sydney College. Wed, Feb 20, 2013

Implementing Linked Lists

Solving Recursive Sequences by Iteration

Recursive Descent Parsers

The Traveling Salesman Problem Nearest-Neighbor Algorithm

The Graphics Pipeline

Scheduling and Digraphs

Density Curves Sections

jquery Lecture 34 Robb T. Koether Wed, Apr 10, 2013 Hampden-Sydney College Robb T. Koether (Hampden-Sydney College) jquery Wed, Apr 10, / 29

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

The Pairwise-Comparison Method

Basic HTML. Lecture 14. Robb T. Koether. Hampden-Sydney College. Wed, Feb 20, 2013

XML. Lecture 29. Robb T. Koether. Fri, Mar 28, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XML Fri, Mar 28, / 23

Insertions, Deletions, and Updates

The Traveling Salesman Problem Brute Force Method

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

Introduction to Databases

Street-Routing Problems

XQuery FLOWR Expressions Lecture 35

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

Regular Expressions. Lecture 10 Sections Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2016

PHP Querying. Lecture 21. Robb T. Koether. Hampden-Sydney College. Fri, Mar 2, 2018

LR Parsing - Conflicts

Basic CSS Lecture 17

Scope and Parameter Passing

The Coefficient of Determination

Displaying Distributions - Quantitative Variables

Recursive Linked Lists

The Plurality-with-Elimination Method

Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

The Class Construct Part 1

Javascript Lecture 23

XSLT. Lecture 38. Robb T. Koether. Mon, Apr 21, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XSLT Mon, Apr 21, / 26

Basic HTML Lecture 14

Boxplots. Lecture 17 Section Robb T. Koether. Hampden-Sydney College. Wed, Feb 10, 2010

Basic PHP Lecture 17

Magnification and Minification

The Graphics Pipeline

Scope and Parameter Passing

Relational Databases

Abstract Data Types. Lecture 23 Section 7.1. Robb T. Koether. Hampden-Sydney College. Wed, Oct 24, 2012

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

The x86 Instruction Set

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

Views. Lecture 15 Section 5.3. Robb T. Koether. Hampden-Sydney College. Mon, Feb 18, 2013

Inheritance: The Fundamental Functions

XQuery Constructors and Joins Lecture 36

Views. Lecture 15. Robb T. Koether. Fri, Feb 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, / 28

Selections. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 22, 2014

The Projection Matrix

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

Binary Tree Implementation

Recognition of Tokens

The CYK Parsing Algorithm

List Iterator Implementation

The Normal Distribution

Binary Tree Implementation

Friends and Unary Operators

Views. Lecture 15 Section 5.3. Robb T. Koether. Hampden-Sydney College. Mon, Feb 18, 2013

The Constructors. Lecture 7 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 1, 2017

The Traveling Salesman Problem Cheapest-Link Algorithm

Introduction to Compiler Design

Total Orders. Lecture 41 Section 8.5. Robb T. Koether. Hampden-Sydney College. Mon, Apr 8, 2013

Programming Languages

Programming Languages

Mipmaps. Lecture 35. Robb T. Koether. Hampden-Sydney College. Wed, Nov 18, 2015

Nondeterministic Programming in C++

PHP Arrays. Lecture 18. Robb T. Koether. Hampden-Sydney College. Mon, Mar 4, 2013

Stacks and their Applications

Specular Reflection. Lecture 19. Robb T. Koether. Hampden-Sydney College. Wed, Oct 4, 2017

Triggers. Lecture 14. Robb T. Koether. Hampden-Sydney College. Wed, Feb 14, 2018

List Iterators. Lecture 27 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

Relational Databases Lecture 2

AJAX: The Basics CISC 282 March 25, 2014

Integer Overflow. Lecture 8 Section 2.5. Robb T. Koether. Hampden-Sydney College. Mon, Jan 27, 2014

Recursion. Lecture 26 Sections , Robb T. Koether. Hampden-Sydney College. Mon, Apr 6, 2015

Transcription:

XML and AJAX Lecture 28 Robb T. Koether Hampden-Sydney College Wed, Mar 28, 2012 Robb T. Koether (Hampden-Sydney College) XML and AJAXLecture 28 Wed, Mar 28, 2012 1 / 29

1 Navigating the XML Tree 2 The getelementsbytagname() Method 3 Creating an HTML Page 4 A Photo Album Robb T. Koether (Hampden-Sydney College) XML and AJAXLecture 28 Wed, Mar 28, 2012 2 / 29

Outline 1 Navigating the XML Tree 2 The getelementsbytagname() Method 3 Creating an HTML Page 4 A Photo Album Robb T. Koether (Hampden-Sydney College) XML and AJAXLecture 28 Wed, Mar 28, 2012 3 / 29

The XML Tree Structure Recall from AJAX that an XMLHttpRequest object has a data member responsexml. This data member represents the contents of the XML file, stored as a tree structure. Using various member functions and data members, we are able to navigate that tree. Robb T. Koether (Hampden-Sydney College) XML and AJAXLecture 28 Wed, Mar 28, 2012 4 / 29

The XML Document The XML Document xml_doc = xmlhttp.responsexml; alert(xml_doc); We begin with the XML document itself. Robb T. Koether (Hampden-Sydney College) XML and AJAXLecture 28 Wed, Mar 28, 2012 5 / 29

The XML Document The XML Document root_elem = xml_doc.documentelement. alert(root_elem); alert(root_elem.nodename); Then we navigate to the document (root) element. It is a library node. Robb T. Koether (Hampden-Sydney College) XML and AJAXLecture 28 Wed, Mar 28, 2012 6 / 29

The XML Document Child Nodes root_child = root_elem.childnodes; alert(root_child); The root element has several children. root_child is a list of nodes. Robb T. Koether (Hampden-Sydney College) XML and AJAXLecture 28 Wed, Mar 28, 2012 7 / 29

The XML Document Child Nodes alert(root_child[0]); alert(root_child[1]); alert(root_child[2]); alert(root_child[3]); The node list is indexed. Each list element is a child of the node. XML allows that there may be text between any two elements. This is unfortunate. Robb T. Koether (Hampden-Sydney College) XML and AJAXLecture 28 Wed, Mar 28, 2012 8 / 29

The XML Document Navigating the XML Tree books_elem = root_child[1]; alert(books_elem); alert(books_elem.nodename); Let us navigate the books element. Robb T. Koether (Hampden-Sydney College) XML and AJAXLecture 28 Wed, Mar 28, 2012 9 / 29

The XML Document Navigating the XML Tree books_child = books_elem.childnodes; alert(books_child); alert(books_child[0]); alert(books_child[1]); alert(books_child[2]); alert(books_child[3]); The books element has several children. Again, we will ignore the text (null strings) in between the XML elements. Robb T. Koether (Hampden-Sydney College) XML and AJAXLecture 28 Wed, Mar 28, 2012 10 / 29

The XML Document Navigating the XML Tree book_elem = books_elem[1]; alert(book_elem); alert(book_elem.nodename); Let us navigate the book element. Robb T. Koether (Hampden-Sydney College) XML and AJAXLecture 28 Wed, Mar 28, 2012 11 / 29

The XML Document Navigating the XML Tree title_elem = book_elem.childnodes[1]; title = title_elem.childnodes[0]; alert("title = " + title.nodevalue); By now we get the pattern. Each element has children. Odd-numbered are the child elements. Even-numbered children are the text (if any) between the elements. Robb T. Koether (Hampden-Sydney College) XML and AJAXLecture 28 Wed, Mar 28, 2012 12 / 29

Outline 1 Navigating the XML Tree 2 The getelementsbytagname() Method 3 Creating an HTML Page 4 A Photo Album Robb T. Koether (Hampden-Sydney College) XML and AJAXLecture 28 Wed, Mar 28, 2012 13 / 29

The getelementsbytagname() Method The getelementsbytagname() Method getelementsbytagname(tag_name); getelementsbytagname() - Returns an indexed list of subtrees, the roots of which are the elements of the specified tag name. Robb T. Koether (Hampden-Sydney College) XML and AJAXLecture 28 Wed, Mar 28, 2012 14 / 29

The getelementsbytagname() Method Find the Book Titles var xml_doc = xmlhttp.responsexml; var title_list = xml_doc.getelementsbytagname("title"); for (var i = 1; i < title_list.length; i++) alert("title = " + title_list.childnodes[0].nodevalue); For example, consider an XML file shown above. Robb T. Koether (Hampden-Sydney College) XML and AJAXLecture 28 Wed, Mar 28, 2012 15 / 29

The getelementsbytagname() Member Function Accessing the Tree xmldoc = xmlhttp.responsexml; author_list = xmlhttp.getelementsbytagname("authors"); The code above will return the entire tree structure It is structured as a single (root) node <authors> with various children. Robb T. Koether (Hampden-Sydney College) XML and AJAXLecture 28 Wed, Mar 28, 2012 16 / 29

Photo Album Example We may use an XML file and Javascript to create an HTML photo album where we can navigate forwards and backwards through the photos. Information about the photos will be stored in an XML file. The Javascript will use AJAX to read that information and then create the HTML needed to present the photo. Robb T. Koether (Hampden-Sydney College) XML and AJAXLecture 28 Wed, Mar 28, 2012 17 / 29

Outline 1 Navigating the XML Tree 2 The getelementsbytagname() Method 3 Creating an HTML Page 4 A Photo Album Robb T. Koether (Hampden-Sydney College) XML and AJAXLecture 28 Wed, Mar 28, 2012 18 / 29

Creating an HTML Page We may read the data from the library XML file and create an HTML table of titles and authors. Robb T. Koether (Hampden-Sydney College) XML and AJAXLecture 28 Wed, Mar 28, 2012 19 / 29

Creating an HTML Page The table Element var table = "<table border= 1 cellpadding= 5 >" + "<tr><th>title</th><th>author(s)</th></tr>"; Create a (string) variable table to store the HTML code. Initialize it to the HTML code that begins the table. Robb T. Koether (Hampden-Sydney College) XML and AJAXLecture 28 Wed, Mar 28, 2012 20 / 29

Creating an HTML Page The List of Books var xml_doc = xmlhttp.responsexml; var book_list = xml_doc.getelementsbytagname("book"); Get the XML structure. Select the book elements. Robb T. Koether (Hampden-Sydney College) XML and AJAXLecture 28 Wed, Mar 28, 2012 21 / 29

Creating an HTML Page The Book Titles for (var i = 0; i < book_list.length; i++) { var title = book_list[i].getelementsbytagname("title")[0].childnodes[0].nodevalue; table += "<tr><td>" + title + "</td>"; : } Process each book element. Get the book s title and add it as a table data (td) element. Robb T. Koether (Hampden-Sydney College) XML and AJAXLecture 28 Wed, Mar 28, 2012 22 / 29

Creating an HTML Page The List of Authors var author_list = book_list[i].getelementsbytagname("author"); var name = "<td>"; for (var j = 0; j < author_list.length; j++) { : } table += name + "</td></tr>"; Get the list of author elements for the i-th book. Add a table data element for each author. Create a (string) variable name to hold the author(s) name(s). Robb T. Koether (Hampden-Sydney College) XML and AJAXLecture 28 Wed, Mar 28, 2012 23 / 29

Creating an HTML Page The Author s First Name var fname_elem = author_list[j].getelementsbytagname("fname")[0]; if (fname_elem!= null) { fname = fname_elem.childnodes[0].nodevalue; name += fname + " "; } Get the author s first name, if he has one, and add it to name. Robb T. Koether (Hampden-Sydney College) XML and AJAXLecture 28 Wed, Mar 28, 2012 24 / 29

Creating an HTML Page The Author s Middle and Last Names var mname_elem = author_list[j].getelementsbytagname("mname")[0]; if (mname_elem!= null) { mname = mname_elem.childnodes[0].nodevalue; name += mname + " "; } var lname_elem = author_list[j].getelementsbytagname("lname")[0]; lname = lname_elem.childnodes[0].nodevalue; name += lname + "<br/>"; In the same way, add the middle name, if there is one, and add the last name. Finish with a line break in case there are more authors. Robb T. Koether (Hampden-Sydney College) XML and AJAXLecture 28 Wed, Mar 28, 2012 25 / 29

Creating an HTML Page Insert the Table into the Web Page table += "</table>"; $("table").innerhtml = table; Complete the table element. Insert it into the web page. Robb T. Koether (Hampden-Sydney College) XML and AJAXLecture 28 Wed, Mar 28, 2012 26 / 29

Outline 1 Navigating the XML Tree 2 The getelementsbytagname() Method 3 Creating an HTML Page 4 A Photo Album Robb T. Koether (Hampden-Sydney College) XML and AJAXLecture 28 Wed, Mar 28, 2012 27 / 29

Photo Album Example The Structure of album.xml <album> <title>title</title> <bgcolor>background_color</bgcolor> <textcolor>text_color</textcolor> <folder>path_to_photos</folder> <photos> <photo> <file>file_name</file> <width>width</width> <height>height</height> <caption>caption</caption> </photo>. </photos> </album> Robb T. Koether (Hampden-Sydney College) XML and AJAXLecture 28 Wed, Mar 28, 2012 28 / 29

Photo Album Example Use HTML, Javascript, and AJAX to create a web page of the photos. Robb T. Koether (Hampden-Sydney College) XML and AJAXLecture 28 Wed, Mar 28, 2012 29 / 29