Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS 174: Web Programming November 4 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak."— Presentation transcript:

1 CS 174: Web Programming November 4 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak

2 Computer Science Dept. Spring 2015: November 4 CS 174: Web Programming © R. Mak Assignment #7  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, Nov. 13. 2

3 Computer Science Dept. Spring 2015: November 4 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!

4 Computer Science Dept. Spring 2015: November 4 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 rules are “well formed”..........

5 Computer Science Dept. Spring 2015: November 4 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 XML element may have attributes. Attribute values must be quoted: Attribute names must be unique within an element. Macbeth William Shakespeare

6 Computer Science Dept. Spring 2015: November 4 CS 174: Web Programming © R. Mak 6 XML Components, cont’d  An empty element has no content and no child elements. An empty element can be “self closed”.  Comments:

7 Computer Science Dept. Spring 2015: November 4 CS 174: Web Programming © R. Mak 7 XML Components, cont’d  Begin every XML document with the processing instruction:  Every XML document must have a single root element:............

8 Computer Science Dept. Spring 2015: November 4 CS 174: Web Programming © R. Mak 8 XML Namespaces  Prevent element name clashes. An element name can be in the scope of a namespace.  A namespace name must be unique.  Use a URI (uniform resource identifier) as the name. Start with your unique domain name.  A URL is a common form of URI. The URL doesn’t have to point to an actual file.

9 Computer Science Dept. Spring 2015: November 4 CS 174: Web Programming © R. Mak 9 XML Namespaces, cont’d  Declare a namespace in an element tag.  The scope of the namespace is that element and its children.  Example: A namespace declared in the root element has the entire XML document in its scope.

10 Computer Science Dept. Spring 2015: November 4 CS 174: Web Programming © R. Mak 10 XML Namespaces, cont’d  Declare the default namespace. Example:  All elements in its scope are in the default namespace.  Elements not in any namespace scope are “in no namespace”....

11 Computer Science Dept. Spring 2015: November 4 CS 174: Web Programming © R. Mak 11 XML Namespaces, cont’d  Declare a non-default namespace with a prefix. Example:  Prefix element names that are in the namespace scope (e.g., au ). The element containing the declaration is itself in the scope. The prefix is considered part of the element name....

12 Computer Science Dept. Spring 2015: November 4 CS 174: Web Programming © R. Mak 12 XML Namespaces, cont’d  Nested namespaces:  Why both the book and author namespaces? Prevent the book title and the author title name clash. <library xmlns="http://www.cs.sjsu.edu/cs157b/library" xmlns:bk="http://www.cs.sjsu.edu/cs157b/book" xmlns:au="http://www.cs.sjsu.edu/cs157b/author"> Java Programming Dr....

13 Computer Science Dept. Spring 2015: November 4 CS 174: Web Programming © R. Mak 13 XML Namespaces, cont’d  Alternate: Java Programming Dr....

14 Computer Science Dept. Spring 2015: November 4 CS 174: Web Programming © R. Mak 14 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).

15 Computer Science Dept. Spring 2015: November 4 CS 174: Web Programming © R. Mak 15 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.

16 Computer Science Dept. Spring 2015: November 4 CS 174: Web Programming © R. Mak 16 Common XML Tools, cont’d  XML Schema XML Schema Definition (XSD). An 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  XQuery A query language for data stored as XML.

17 Computer Science Dept. Spring 2015: November 4 CS 174: Web Programming © R. Mak 17 Common XML Tools, cont’d  XML parsers Parse an XML document to obtain its information.  Object-XML mapping Perform object bindings.  Web services A way to transport XML data between applications.

18 Computer Science Dept. Spring 2015: November 4 CS 174: Web Programming © R. Mak 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. 18

19 Computer Science Dept. Spring 2015: November 4 CS 174: Web Programming © R. Mak 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. 19

20 Computer Science Dept. Spring 2015: November 4 CS 174: Web Programming © R. Mak Example XML Data: Courses 20 CS 149 Operating Systems Fundamentals: Contiguous and non-contiguous memory management; processor scheduling and interrupts; concurrent, mutually exclusive, synchronized and deadlocked processes; files. Substantial programming project required. CS 146 or SE 146 (with a grade of "C-" or better). courses.xml

21 Computer Science Dept. Spring 2015: November 4 CS 174: Web Programming © R. Mak Example XML Data: Courses, cont’d 21 CS 153 Compiler Design 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. CS 47 or CMPE 102, CS 146, and CS 154 (with a grade of "C-" or better in each) or instructor consent. courses.xml

22 Computer Science Dept. Spring 2015: November 4 CS 174: Web Programming © R. Mak Example XML Data: Courses, cont’d 22 CS 174 Web Programming Development and deployment of multi-tier web-based applications. Introduction to HTML, XML, enterprise design patterns, web services and database access. CS 46B (with a grade of "C-" or better). courses.xml

23 Computer Science Dept. Spring 2015: November 4 CS 174: Web Programming © R. Mak Example XML Data: Courses, cont’d 23 CS 235 User Interface Design 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. User requirements and use cases UI and UX design patterns Usability testing CS 46B (with a grade of "C-" or better). courses.xml

24 Computer Science Dept. Spring 2015: November 4 CS 174: Web Programming © R. Mak Expat Parsing for Structure 24 $(init); function init() { $("#output").load("structure.php"); } Structure Structure structure.html

25 Computer Science Dept. Spring 2015: November 4 CS 174: Web Programming © R. Mak Expat Parsing for Structure, cont’d 25 $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

26 Computer Science Dept. Spring 2015: November 4 CS 174: Web Programming © R. Mak Expat Parsing for Structure, cont’d 26 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

27 Computer Science Dept. Spring 2015: November 4 CS 174: Web Programming © R. Mak Expat Parsing for Structure, cont’d 27 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

28 Computer Science Dept. Spring 2015: November 4 CS 174: Web Programming © R. Mak Expat Parsing: XML to HTML 28 $(init); function init() { $("#output").load("courses1.php"); } Expat Parser Courses by Expat Parser courses1.html

29 Computer Science Dept. Spring 2015: November 4 CS 174: Web Programming © R. Mak Expat Parsing: XML to HTML, cont’d 29 function openElement($p, $element, $attributes) { switch ($element) { case "COURSE": { echo " "; break; } case "TITLE": { echo " "; break; } case "DESCRIPTION": { echo " "; break; } courses1.php

30 Computer Science Dept. Spring 2015: November 4 CS 174: Web Programming © R. Mak Expat Parsing: XML to HTML, cont’d 30 case "PREQUISITES": { echo " Prerequisites "; break; } case "TOPICS": { echo " "; break; } case "TOPIC": { echo " "; break; } courses1.php

31 Computer Science Dept. Spring 2015: November 4 CS 174: Web Programming © R. Mak Expat Parsing: XML to HTML, cont’d 31 function closeElement($p, $element) { switch ($element) { case "COURSE": { echo " "; break; } case "TITLE": { echo " "; break; } case "DESCRIPTION": { echo " "; break; } courses1.php

32 Computer Science Dept. Spring 2015: November 4 CS 174: Web Programming © R. Mak Expat Parsing: XML to HTML, cont’d 32 case "PREQUISITES": { echo " "; break; } case "TOPICS": { echo " "; break; } case "TOPIC": { echo " "; break; } courses1.php

33 Computer Science Dept. Spring 2015: November 4 CS 174: Web Programming © R. Mak Expat Parsing: XML to HTML, cont’d 33 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

34 Computer Science Dept. Spring 2015: November 4 CS 174: Web Programming © R. Mak Expat Parsing: XML to HTML, cont’d 34 $file = "courses.xml"; $fp = @fopen($file, "r") or die(" Could not open a file called '$file'.". " "); while ($data = fread($fp, 4096)) { xml_parse($parser, $data, feof($fp)); } xml_parser_free($parser); ?> courses1.php Demo

35 Computer Science Dept. Spring 2015: November 4 CS 174: Web Programming © R. Mak 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. 35

36 Computer Science Dept. Spring 2015: November 4 CS 174: Web Programming © R. Mak “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. 36

37 Computer Science Dept. Spring 2015: November 4 CS 174: Web Programming © R. Mak “Simple”: A DOM-Based Parser, cont’d 37 <?php $xml = simplexml_load_file("courses.xml"); foreach ($xml->course as $course) { echo " $course->title "; echo " $course->description "; if (isset($course->topics)) { echo " "; $topics = $course->topics; foreach ($topics->topic as $topic) { echo " $topic "; } echo " "; } echo " Prerequisites $course- >prequisites "; echo " "; } ?> courses2.php Demo

38 Computer Science Dept. Spring 2015: November 4 CS 174: Web Programming © R. Mak “Simple” Advantages and Disadvantages  Advantages More straightforward, structure-aware code. The parser can be small.  Disadvantages Must understand the structure of the XML data in order to walk the DOM tree properly. Building the DOM tree in memory limits the size of the XML data that can be parsed. 38


Download ppt "CS 174: Web Programming November 4 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak."

Similar presentations


Ads by Google