Extensible Markup Language (XML) What is XML? Structure of an XML document. CSE 190 M (Web Programming), Spring 2007 University of Washington

Similar documents
Extensible Markup Language (XML) What is XML? An example XML file. CSE 190 M (Web Programming), Spring 2008 University of Washington

XML: a "skeleton" for creating markup languages you already know it! <element attribute="value">content</element> languages written in XML specify:

XML CSC 443: Web Programming

Web Programming Step by Step

CSE 154 LECTURE 23: XML

INTERNET & WEB APPLICATION DEVELOPMENT SWE 444. Fall Semester (081) Module 4 (VII): XML DOM

CSE 154 LECTURE 12: XML

Asynchronous JavaScript + XML (Ajax)

11. EXTENSIBLE MARKUP LANGUAGE (XML)

AJAX: The Basics CISC 282 November 22, 2017

AJAX: The Basics CISC 282 March 25, 2014

Web Programming Step by Step

DOM Interface subset 1/ 2

Data formats. { "firstname": "John", "lastname" : "Smith", "age" : 25, "address" : { "streetaddress": "21 2nd Street",

XML (Extensible Markup Language)

E-Applications. XML and DOM in Javascript. Michail Lampis

Ajax. Ronald J. Glotzbach

XML extensible Markup Language

Software Engineering Methods, XML extensible Markup Language. Tutorial Outline. An Example File: Note.xml XML 1

Bioinforma)cs Resources XML / Web Access

INDEX SYMBOLS See also

What is XML? XML is designed to transport and store data.

INTERNET PROGRAMMING XML

INLEDANDE WEBBPROGRAMMERING MED JAVASCRIPT INTRODUCTION TO WEB PROGRAMING USING JAVASCRIPT

Web Programming Paper Solution (Chapter wise)

Extensible Markup Language (XML) Hamid Zarrabi-Zadeh Web Programming Fall 2013

The Document Object Model (DOM) is a W3C standard. It defines a standard for accessing documents like HTML and XML.

Document Object Model (DOM)

The attributes property of an element node returns a list of attribute nodes. It s called a named node map

XML. Jonathan Geisler. April 18, 2008

SYBEX Web Appendix. DOM Appendix: The Document Object Model, Level 1

EXtensible Markup Language XML

COMP9321 Web Application Engineering. Extensible Markup Language (XML)

Recall: Document Object Model (DOM)

AJAX: Introduction CISC 282 November 27, 2018

AJAX. Introduction. AJAX: Asynchronous JavaScript and XML

8/1/2016. XSL stands for EXtensible Stylesheet Language. CSS = Style Sheets for HTML XSL = Style Sheets for XML. XSL consists of four parts:

Introducing the JavaScript DOM

XML APIs. Web Data Management and Distribution. Serge Abiteboul Philippe Rigaux Marie-Christine Rousset Pierre Senellart

Javascript. Many examples from Kyle Simpson: Scope and Closures

CSC 337. JavaScript Object Notation (JSON) Rick Mercer

markup language carry data define your own tags self-descriptive W3C Recommendation

Chapter 11 Objectives

COMS W3101: SCRIPTING LANGUAGES: JAVASCRIPT (FALL 2018)

AJAX and JSON. Day 8

Using JavaScript for Client-Side Behavior

XML and AJAX Lecture 28

COMP9321 Web Application Engineering

Client Side JavaScript and AJAX

CSE 154 LECTURE 10: THE DOM TREE

Financial IT Examination Syllabus(2018)

University of Washington, CSE 190 M Homework Assignment 8: Baby Names

Programming the World Wide Web by Robert W. Sebesta

CSI 3140 WWW Structures, Techniques and Standards. Representing Web Data: XML

Web Programming Step by Step

The course also includes an overview of some of the most popular frameworks that you will most likely encounter in your real work environments.

CSC Web Technologies, Spring Web Data Exchange Formats

Web services CSCI 470: Web Science Keith Vertanen Copyright 2011

The components of a basic XML system.

XML: Tools and Extensions

Walking the DOM Tree. Lecture Outline. An example XHTML page. CSE 190 M (Web Programming), Spring 2008 University of Washington

XML: Tools and Extensions

Course Details. Skills Gained. Who Can Benefit. Prerequisites. View Online URL:

CSC System Development with Java Working with XML

COMP9321 Web Application Engineering

University of Washington, CSE 154 Homework Assignment 8: Baby Names

Ingegneria del Software T. XML Programming

Contents. 1. Using Cherry 1.1 Getting started 1.2 Logging in

COURSE 20480B: PROGRAMMING IN HTML5 WITH JAVASCRIPT AND CSS3

Programming in HTML5 with JavaScript and CSS3

Lesson 12: JavaScript and AJAX

EAE-2037 Loading transactions into your EAE/ABSuite system. Unite 2012 Mike Bardell

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

IIUG Conference Announcements

Financial IT Examination Syllabus(2017)

XML and Databases. Outline

Application Note AN Copyright InduSoft Systems LLC 2006

DOM. Contents. Sergio Luján Mora. What is DOM? DOM Levels DOM Level 0 DOM Level 1. Departamento de Lenguajes y Sistemas Informáticos

University of Washington, CSE 190 M Homework Assignment 9: Remember the Cow (To-Do List)

AJAX ASYNCHRONOUS JAVASCRIPT AND XML. Laura Farinetti - DAUIN

XML Extensible Markup Language

Introduction to AJAX Bringing Interactivity & Intuitiveness Into Web Applications. By : Bhanwar Gupta SD-Team-Member Jsoft Solutions

Introduction to XML. M2 MIA, Grenoble Université. François Faure

Module 5 JavaScript, AJAX, and jquery. Module 5. Module 5 Contains an Individual and Group component

XML: Extensible Markup Language

2/6/2012. Rich Internet Applications. What is Ajax? Defining AJAX. Asynchronous JavaScript and XML Term coined in 2005 by Jesse James Garrett

CSCC43H: Introduction to Databases. Lecture 10

Course 20480: Programming in HTML5 with JavaScript and CSS3

CS Final Exam Review Suggestions - Spring 2018

c122jan2714.notebook January 27, 2014

TIME SCHEDULE MODULE TOPICS PERIODS. HTML Document Object Model (DOM) and javascript Object Notation (JSON)

CSE 154 LECTURE 17: WEB SERVICES

Overview

Shankersinh Vaghela Bapu Institue of Technology

Module 5 JavaScript, AJAX, and jquery. Module 5. Module 5 Contains 2 components

Delivery Options: Attend face-to-face in the classroom or via remote-live attendance.

XML Metadata Standards and Topic Maps

XML. Objectives. Duration. Audience. Pre-Requisites

Understanding this structure is pretty straightforward, but nonetheless crucial to working with HTML, CSS, and JavaScript.

CSC Web Programming. JavaScript Browser Objects

Transcription:

Page 1 Extensible Markup Language (XML) CSE 190 M (Web Programming), Spring 2007 University of Washington Reading: Sebesta Ch. 8 sections 8.1-8.3, 8.7-8.8, 8.10.3 What is XML? a specification for creating markup languages to store hierarchical data used to facilitate sharing data between different systems XHTML is a subset of XML a language created using XML specifications an adaptation of old HTML to fit XML's syntax requirements Structure of an XML document a header, then a single document tag that can contain other tags <?xml version="1.0" encoding="utf-8"?> document tag tag syntax: <element attributes> text or tags </element> or a tag with no inner tags/content can end with /> attribute syntax: name="value" comments: <!-- comment -->

Page 2 An example XML file <?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> other examples: music, math, vector graphics, web feeds Larger XML file example <?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 category="computers"> <title lang="en">xquery Kick Start</title> <author>james McGovern</author> <year>2003</year><price>49.99</price> <book category="children"> <title lang="en">harry Potter</title> <author>j K. Rowling</author> <year>2005</year><price>29.99</price> <book category="computers"> <title lang="en">learning XML</title> <author>erik T. Ray</author> <year>2003</year><price>39.95</price> </bookstore>

Page 3 Resulting tree structure (partial) the XML tags have a tree structure nodes have parents, children, and siblings (we'll process these nodes using the Javascript XML DOM) What tags are legal in XML? any tag you want; you can make up your own structure schema: description of which tags are legal with your data a schema can be formally defined in different languages: Document Type Definition (DTD) W3C XML Schema if you define a formal schema for your XML files, you can use tools to validate XML files to make sure they match your schema XHTML is an XML language that has a schema allows W3C validator to check HTML files to see if they follow it

Page 4 Facts about XML data comes from many sources on the web: web servers store data as XML files databases sometimes return query results as XML web services use XML to communicate RSS news feeds use an XML format pros and cons of XML: pro: human-readable, self-documenting format strict syntax allows standardized tools international, platform-independent can represent almost any general kind of data (record, list, tree) con: bulky syntax/structure makes files large; can decrease performance Displaying XML data on a web page XML is pure data; doesn't specify how it should be displayed can transform XML into HTML using Javascript XML DOM basic outline: fetch XML data using an Ajax XMLHttpRequest object retrieve the resulting data as an XML document tree examine the tree, using DOM properties we've already seen turn XML data into HTML tags as desired other ways to transform XML (not covered): CSS, XSLT Ajax XMLHttpRequest template for XML var 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 tree can use all DOM properties and methods on responsexml tree

Page 5 DOM node properties/methods properties: firstchild, lastchild, childnodes, nextsibling, previoussibling, parentnode nodename, nodetype, nodevalue, attributes methods: appendchild, insertbefore, removechild, replacechild getelementsbytagname, getattribute, hasattributes, haschildnodes Details about XML node properties 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" an attribute node's name is the attribute's name all text nodes have name "#text" document node has name "#document" nodevalue : text inside a text node, or value of an attribute node Navigating the node tree element.getelementsbytagname("tag") get an array of all children of the given type ("p", "div", etc.) can be called on the overall document or on a specific node element.getattribute("attributename") get an attribute from an element node

Page 6 Recall: XML file <?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 category="computers"> <title lang="en">xquery Kick Start</title> <author>james McGovern</author> <year>2003</year><price>49.99</price>... </bookstore> let's write code to process the computer books in this file Navigating node tree example 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); makes a paragraph for each <book> element about computers (e.g. "XQuery Kick Start, by James McGovern")

Page 7 Practice problem: Animal game Write a program where the user thinks of an animal, and the page repeatedly asks yes/no questions to find it. The data comes from a web application (game.php) that serves XML data in the following format: <node id="id"> <question>question</question> <yes id="id" /> <no id="id" /> </node> <node id="id"> <answer>answer</answer> </node> to get a node with a given id: grades.php?id=id Attacking the problem questions we should ask ourselves: what should be the initial state of the program? how do I retrieve data from the web app? (what URL, etc.) once I retrieve a piece of data, what should I do with it? when the user clicks "Yes", what should I do? when the user clicks "No", what should I do? how do I know when the game is over? what should I do in this case? We'll fetch the data from the web app using Ajax. Let's look at a robust final template for Ajax code that we can copy into our program. Final Ajax template 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); accepts a function as a parameter; calls it when data arrives

Page 8 Using the Ajax template 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); using this Ajax template, you can handle text or XML data easily you may want to comment out the error message alert code in the template, depending on how you want to handle various errors Recall: XMLHttpRequest restrictions Ajax code can only be run on a web page stored on a web server can only fetch files from the same site that the page is on to test/debug your Ajax code, must upload it to UW web space

Page 9 Debugging Ajax code in Firebug Net tab shows each request, its parameters, response, any errors Debugging responsexml in Firebug can examine the entire XML document, its node/tree structure