Top Banner
CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak
33

CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak.

Dec 24, 2015

Download

Documents

Diana Thornton
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak.

CS 174: Web ProgrammingApril 16 Class Meeting

Department of Computer ScienceSan Jose State University

Spring 2015Instructor: Ron Mak

www.cs.sjsu.edu/~mak

Page 2: CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak.

Computer Science Dept.Spring 2015: April 16

CS 174: Web Programming© R. Mak

2

Assignment #6

Add AJAX to your web application.

Modify a portion of your web page (or dynamically create a menu or table) without a full page refresh.

Turn in the usual zip file containing source files, database dump, and screen shots.

Due Friday, April 24.

Page 3: CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak.

Computer Science Dept.Spring 2015: April 16

CS 174: Web Programming© R. Mak

3

XML

The Extensible Markup Language (XML) is an industry standard to:

Store information.

Describe the structure of that information.

Exchange the information among different applications in a programming language-independent way.

Not all data comes from relational databases!

Page 4: CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak.

Computer Science Dept.Spring 2015: April 16

CS 174: Web Programming© R. Mak

4

XML Components

An XML element has an opening and a closing tag:

The closing tag is mandatory.

An XML element may be nested in another element (child elements):

XML documents that obey all

the syntax rulesare “well formed”.

<book> ... </book>

<book> <title> ... </title> <author> ... </author></book>

Page 5: CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak.

Computer Science Dept.Spring 2015: April 16

CS 174: Web Programming© R. Mak

5

XML Components, cont’d

An XML element may have content:

An element can have both content and child elements.

An empty element has no content and no child elements.

An empty element can be “self closed”.

<title>Macbeth</title><author>William Shakespeare</author>

<ebook></ebook><printed pages="256" />

Page 6: CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak.

Computer Science Dept.Spring 2015: April 16

CS 174: Web Programming© R. Mak

6

XML Components, cont’d

An XML element may have attributes Attribute values must be quoted. Attribute names must be unique within an element.

<title language="English"> Macbeth</title><author nationality="British" gender="male"> William Shakespeare</author>

Page 7: CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak.

Computer Science Dept.Spring 2015: April 16

CS 174: Web Programming© R. Mak

7

XML Components, cont’d

Comments:

Begin every XML document with the processing instruction:

<!--comment text-->

<?xml version="1.0"?>

Page 8: CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak.

Computer Science Dept.Spring 2015: April 16

CS 174: Web Programming© R. Mak

8

XML Components, cont’d

Every XML document must have a single root element:

<library> <book> ... </book> <book> ... </book> <journal> ... </journal> ...</library>

Page 9: CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak.

Computer Science Dept.Spring 2015: April 16

CS 174: Web Programming© R. Mak

9

Most Common XML Tools

Xpath

“Path expressions” to locate a specific node (element, attribute, or content) or node sets within an XML document.

Functions to compare, count, do arithmetic, extract substrings, etc.

XSLT

Extensible Style Language for Transformation. Transform XML from one form to another

(such as to HTML).

Page 10: CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak.

Computer Science Dept.Spring 2015: April 16

CS 174: Web Programming© R. Mak

10

Most Common XML Tools, cont’d

DTD

Document Type Definition. Specify the schema of XML documents.

The DTD is itself not an XML document. Validate an XML document against its schema.

Page 11: CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak.

Computer Science Dept.Spring 2015: April 16

CS 174: Web Programming© R. Mak

11

Most Common XML Tools, cont’d

XML Schema

XML Schema Definition (XSD). An alternate way to specify the schema of XML

documents. An XML Schema is itself an XML document.

A valid XML document is an instance of its schema.

XML schema : XML document Java class : Java object

Page 12: CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak.

Computer Science Dept.Spring 2015: April 16

CS 174: Web Programming© R. Mak

12

Most Common XML Tools, cont’d

XML parsers Parse an XML document to obtain its information.

Object-XML mapping Perform object bindings.

Xquery A query language for data stored as XML.

Web services A way to transport XML data between applications.

Page 13: CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak.

Computer Science Dept.Spring 2015: April 16

CS 174: Web Programming© R. Mak

13

XML Data on the Server

A data source for the web server can be XML.

The server must parse the XML data in order to understand its structure and extract its information.

Example: Parse XML data and convert it to HTML

for download to a web browser.

Page 14: CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak.

Computer Science Dept.Spring 2015: April 16

CS 174: Web Programming© R. Mak

14

Expat: An “Event-Driven” XML Parser

The Expat parser is an XML parser for PHP.

As it parses XML data from start to end, “events” are fired each time it reads

a start element tag an end element tag element contents

Callback functions process each event.

Page 15: CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak.

Computer Science Dept.Spring 2015: April 16

CS 174: Web Programming© R. Mak

15

Example XML Data: Courses

<?xml version="1.0"?><courses> <course> <title>CS 149 Operating Systems</title> <description> Fundamentals: Contiguous and non-contiguous memory management; processor scheduling and interrupts; concurrent, mutually exclusive, synchronized and deadlocked processes; files. Substantial programming project required. </description> <prequisites> CS 146 or SE 146 (with a grade of "C-" or better). </prequisites> </course>

courses.xml

Page 16: CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak.

Computer Science Dept.Spring 2015: April 16

CS 174: Web Programming© R. Mak

16

Example XML Data: Courses, cont’d

<course> <title>CS 153 Compiler Design</title> <description> Theoretical aspects of compiler design, including parsing context free languages, lexical analysis, translation specification and machine-independent code generation. Programming projects to demonstrate design topics. </description> <prequisites> CS 47 or CMPE 102, CS 146, and CS 154 (with a grade of "C-" or better in each) or instructor consent. </prequisites> </course>

courses.xml

Page 17: CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak.

Computer Science Dept.Spring 2015: April 16

CS 174: Web Programming© R. Mak

17

Example XML Data: Courses, cont’d

<course> <title>CS 174 Web Programming</title> <description> Development and deployment of multi-tier web-based applications. Introduction to HTML, XML, enterprise design patterns, web services and database access. </description> <prequisites> CS 46B (with a grade of "C-" or better). </prequisites> </course>

courses.xml

Page 18: CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak.

Computer Science Dept.Spring 2015: April 16

CS 174: Web Programming© R. Mak

18

Example XML Data: Courses, cont’d

<course> <title>CS 235 User Interface Design</title> <description> We will study the principles of designing, developing, and evaluating a compelling and effective user interface (UI) and experience (UX) for desktop, web, and mobile applications. </description> <topics> <topic>User requirements and use cases</topic> <topic>UI and UX design patterns</topic> <topic>Usability testing</topic> </topics> <prequisites> CS 46B (with a grade of "C-" or better). </prequisites> </course></courses>

courses.xml

Page 19: CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak.

Computer Science Dept.Spring 2015: April 16

CS 174: Web Programming© R. Mak

19

Expat Parsing for Structure<html><head> <meta charset="UTF-8"> <script type="text/javascript” src="js/jquery.js"> </script> <script type="text/javascript"> $(init); function init() { $("#output").load("structure.php"); } </script> <title>Structure</title></head>

<body> <h1>Structure</h1> <pre id='output'></pre></body></html>

structure.html

Page 20: CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak.

Computer Science Dept.Spring 2015: April 16

CS 174: Web Programming© R. Mak

20

Expat Parsing for Structure, cont’d

$file = "courses.xml";$depth = array();

function startElement($parser, $name, $attrs){ global $depth;

if (!isset($depth[$parser])) { $depth[$parser] = 0; }

for ($i = 0; $i < $depth[$parser]; $i++) { echo " "; } echo "$name\n"; $depth[$parser]++;}

structure.php

Page 21: CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak.

Computer Science Dept.Spring 2015: April 16

CS 174: Web Programming© R. Mak

21

Expat Parsing for Structure, cont’d

function endElement($parser, $name){ global $depth; $depth[$parser]--;}

$xml_parser = xml_parser_create();xml_set_element_handler($xml_parser, "startElement", "endElement");

if (!($fp = fopen($file, "r"))) { die("Could not open XML input.");}

structure.php

Page 22: CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak.

Computer Science Dept.Spring 2015: April 16

CS 174: Web Programming© R. Mak

22

Expat Parsing for Structure, cont’d

while ($data = fread($fp, 4096)) { if (!xml_parse($xml_parser, $data, feof($fp))) { die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); }}

xml_parser_free($xml_parser);?>

Demo

structure.php

Page 23: CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak.

Computer Science Dept.Spring 2015: April 16

CS 174: Web Programming© R. Mak

23

Expat Parsing: XML to HTML <html><head> <meta charset="UTF-8"> <script type="text/javascript" src="js/jquery.js"> </script> <script type="text/javascript"> $(init); function init() { $("#output").load("courses1.php"); } </script> <title>Expat Parser</title></head>

<body> <h1>Courses by Expat Parser</h1> <div id='output'></div></body></html>

courses1.html

Page 24: CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak.

Computer Science Dept.Spring 2015: April 16

CS 174: Web Programming© R. Mak

24

Expat Parsing: XML to HTML, cont’d

function openElement($p, $element, $attributes) { switch ($element) { case "COURSE": { echo "<div>"; break; } case "TITLE": { echo "<h2>"; break; } case "DESCRIPTION": { echo "<p>"; break; } courses1.php

Page 25: CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak.

Computer Science Dept.Spring 2015: April 16

CS 174: Web Programming© R. Mak

25

Expat Parsing: XML to HTML, cont’d

case "PREQUISITES": { echo "<dl><dt>Prerequisites</dt><dd>"; break; } case "TOPICS": { echo "<ul>"; break; } case "TOPIC": { echo "<li>"; break; } }} courses1.php

Page 26: CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak.

Computer Science Dept.Spring 2015: April 16

CS 174: Web Programming© R. Mak

26

Expat Parsing: XML to HTML, cont’d

function closeElement($p, $element) { switch ($element) { case "COURSE": { echo "</div><hr />"; break; } case "TITLE": { echo "</h2>"; break; } case "DESCRIPTION": { echo "</p>"; break; } courses1.php

Page 27: CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak.

Computer Science Dept.Spring 2015: April 16

CS 174: Web Programming© R. Mak

27

Expat Parsing: XML to HTML, cont’d

case "PREQUISITES": { echo "</dd><dl>"; break; } case "TOPICS": { echo "</ul>"; break; } case "TOPIC": { echo "</li>"; break; } }}

courses1.php

Page 28: CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak.

Computer Science Dept.Spring 2015: April 16

CS 174: Web Programming© R. Mak

28

Expat Parsing: XML to HTML, cont’d

function characterData($p, $cdata) { echo $cdata;}

$parser = xml_parser_create();xml_set_element_handler($parser, "openElement", "closeElement");xml_set_character_data_handler($parser, "characterData");

courses1.php

Page 29: CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak.

Computer Science Dept.Spring 2015: April 16

CS 174: Web Programming© R. Mak

29

Expat Parsing: XML to HTML, cont’d

$file = "courses.xml";$fp = @fopen($file, "r") or die("<p>Could not open a file called '$file'." . "</p></body></html>");

while ($data = fread($fp, 4096)) { xml_parse($parser, $data, feof($fp));}

xml_parser_free($parser);?>

courses1.php

Demo

Page 30: CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak.

Computer Science Dept.Spring 2015: April 16

CS 174: Web Programming© R. Mak

30

Expat Advantages and Disadvantages

Advantages

Very fast One pass Can handle arbitrarily large XML data.

Disadvantages

Inflexible Must process the stream of “events” as they occur. The parser may become large.

Page 31: CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak.

Computer Science Dept.Spring 2015: April 16

CS 174: Web Programming© R. Mak

31

“Simple”: A DOM-Based Parser

“Simple” is a DOM-based XML parser for PHP. As it parses XML data, it builds a DOM tree. Walk the tree in order to obtain its information.

Page 32: CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak.

Computer Science Dept.Spring 2015: April 16

CS 174: Web Programming© R. Mak

32

“Simple”: A DOM-Based Parser, cont’d<?php$xml = simplexml_load_file("courses.xml");

foreach ($xml->course as $course) { echo "<div><h2>$course->title</h2>"; echo "<p>$course->description</p>";

if (isset($course->topics)) { echo "<ul>"; $topics = $course->topics; foreach ($topics->topic as $topic) {

echo "<li>$topic</li>"; } echo "</ul>"; } echo "<dl><dt>Prerequisites</dt><dd>$course->prequisites</dd></dl>"; echo "</div><hr />";}?>

courses2.php

Demo

Page 33: CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak mak.

Computer Science Dept.Spring 2015: April 16

CS 174: Web Programming© R. Mak

33

“Simple” Advantages and Disadvantages

Advantages

More straightforward, structure-aware code. The parser can be small.

Disadvantages

Must understand the structure of the XML datain order to walk the DOM tree properly.

Building the DOM tree in memory limits the size of the XML data that can be parsed.