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

Similar documents
Web Programming Step by Step

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

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

CSE 154 LECTURE 23: XML

CSE 154 LECTURE 12: XML

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

11. EXTENSIBLE MARKUP LANGUAGE (XML)

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

XML (Extensible Markup Language)

DOM Interface subset 1/ 2

Web Programming Step by Step

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

AJAX: The Basics CISC 282 March 25, 2014

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

AJAX: The Basics CISC 282 November 22, 2017

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

Bioinforma)cs Resources XML / Web Access

Asynchronous JavaScript + XML (Ajax)

Web Programming Paper Solution (Chapter wise)

XML extensible Markup Language

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

Ajax. Ronald J. Glotzbach

Web Programming Step by Step

EXtensible Markup Language XML

INLEDANDE WEBBPROGRAMMERING MED JAVASCRIPT INTRODUCTION TO WEB PROGRAMING USING JAVASCRIPT

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

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

Document Object Model (DOM)

INDEX SYMBOLS See also

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

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

INTERNET PROGRAMMING XML

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

Chapter 11 Objectives

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

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

Introducing the JavaScript DOM

CSE 154 LECTURE 17: WEB SERVICES

XML. Jonathan Geisler. April 18, 2008

Recall: Document Object Model (DOM)

COMP9321 Web Application Engineering. Extensible Markup Language (XML)

COMS W3101: SCRIPTING LANGUAGES: JAVASCRIPT (FALL 2018)

The components of a basic XML system.

Javascript. Many examples from Kyle Simpson: Scope and Closures

CS Final Exam Review Suggestions - Spring 2018

XML: Tools and Extensions

Using JavaScript for Client-Side Behavior

AJAX: Introduction CISC 282 November 27, 2018

CSC System Development with Java Working with XML

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

XML. Marie Dubremetz Uppsala, April 2014

XML: Tools and Extensions

Create web pages in HTML with a text editor, following the rules of XHTML syntax and using appropriate HTML tags Create a web page that includes

University of Washington, CSE 190 M Homework Assignment 4: NerdLuv

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

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

Comm 244 Week 3. Navigation. Navigation. Websites need a formalized system of links to allow users to navigate the site

Client Side JavaScript and AJAX

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

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

JavaScript and the DOM MIS Konstantin Bauman. Department of MIS Fox School of Business Temple University

User Interaction: jquery

Application Note AN Copyright InduSoft Systems LLC 2006

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

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

CSE 154 LECTURE 10: THE DOM TREE

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

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

Navigation. Websites need a formalized system of links to allow users to navigate the site

This course is designed for web developers that want to learn HTML5, CSS3, JavaScript and jquery.

Basic Web Pages with XHTML (and a bit of CSS) CSE 190 M (Web Programming), Spring 2008 University of Washington Reading: Chapter 1, sections

CSC Web Programming. JavaScript Browser Objects

XML Extensible Markup Language

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

Financial IT Examination Syllabus(2018)

Ingegneria del Software T. XML Programming

XML: some structural principles

AJAX Workshop. Karen A. Coombs University of Houston Libraries Jason A. Clark Montana State University Libraries

XML. Presented by : Guerreiro João Thanh Truong Cong

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

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

JQuery WHY DIDN T WE LEARN THIS EARLIER??!

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

Markup Languages SGML, HTML, XML, XHTML. CS 431 February 13, 2006 Carl Lagoze Cornell University

Web Programming Step by Step

Programming the World Wide Web by Robert W. Sebesta

Document Object Model (DOM) A brief introduction. Overview of DOM. .. DATA 301 Introduction to Data Science Alexander Dekhtyar..

c122jan2714.notebook January 27, 2014

Web scraping and crawling, open data, markup languages and data shaping. Paolo Boldi Dipartimento di Informatica Università degli Studi di Milano

footer, header, nav, section. search. ! Better Accessibility.! Cleaner Code. ! Smarter Storage.! Better Interactions.

XML Technologies. Doc. RNDr. Irena Holubova, Ph.D. Web pages:

AJAX. Introduction. AJAX: Asynchronous JavaScript and XML

Unobtrusive JavaScript. Lecture Outline. CSE 190 M (Web Programming), Spring 2008 University of Washington

Web Programming Pre-01A Web Programming Technologies. Aryo Pinandito, ST, M.MT

XML: Extensible Markup Language

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

XML JavaScript Object Notation JSON Cookies Miscellaneous What Javascript can t do. OOP Concepts of JS

Web Services Part I. XML Web Services. Instructor: Dr. Wei Ding Fall 2009

Ajax. David Matuszek's presentation,

Transcription:

Extensible Markup Language (XML) CSE 190 M (Web Programming), Spring 2008 University of Washington Except where otherwise noted, the contents of this presentation are Copyright 2008 Marty Stepp and Jessica Miller and are licensed under the Creative Commons Attribution 2.5 License. What is XML? a specification for creating languages to store data; used to share data between systems a basic syntax of tags & attributes languages written in XML specify tag names, attribute names, and rules of use Example: XHTML is a "flavor" of XML an adaptation of old HTML to fit XML's syntax requirements XML specifies tag syntax: <......="..."></...> HTML contributes tag names (e.g. h1, img) and attributes (id/class on all elements, src/alt on img tag) An example XML file <?xml version="1.0" encoding="utf-8"?> <note> <to>tove</to> <from>jani</from> <subject>reminder</subject> <message language="english"> Don't forget me this weekend! </message> </note> XML syntax: begins with an xml header tag, then a single document tag (in this case, note) tag, attribute, and comment syntax is identical to XHTML's

What tags are legal in XML? any tag you want; the person storing the data can make up their own tag structure example: a person storing data about email messages may want tags named to, from, subject example: a person storing data about books may want tags named book, title, author "Garden State" XML: if you're feeling unoriginal, make up some XML nobody's ever done before <bloop bleep="flibbetygibbet">quirkleblat</bloop> Schemas schema: an optional set of rules specifying which tags and attributes are valid, and how they can be used together used to validate XML files to make sure they follow the rules of that "flavor" XHTML has a schema; W3C validator uses it to validate doctype at top of XHTML file specifies schema two ways to define a schema: Document Type Definition (DTD) W3C XML Schema (we won't use schemas in this course) Uses of XML 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 XML languages are used for music, math, vector graphics popular use: RSS for news feeds & podcasts Pros and cons of XML pro: con: easy to read (for humans and computers) standard format makes automation easy don't have to "reinvent the wheel" for storing new types of data international, platform-independent, open/free standard can represent almost any general kind of data (record, list, tree) bulky syntax/structure makes files large; can decrease performance example: quadratic formula in MathML can be hard to "shoehorn" data into an intuitive XML format won't need to know how for this class

Fetching XML using AJAX (template) new Ajax.Request( "url", { method: "get", onsuccess: functionname } );... function functionname(ajax) { do something with ajax.responsexml; } ajax.responsetext still contains XML code, but in plain text ajax.responsexml is a pre-parsed DOM object representing the XML file (more useful) Using XML data in a web page custom flavor of XML needs to be converted to XHTML, then injected into page we will transform using Javascript XML DOM basic technique: 1. fetch XML data using Ajax 2. examine the responsexml object, using DOM methods and properties 3. extract data from XML elements and wrap them in HTML elements 4. inject HTML elements into web page other ways to transform XML (not covered): CSS, XSLT Recall: Javascript XML (XHTML) DOM All of the DOM properties and methods we already know can be used on XML nodes: properties: firstchild, lastchild, childnodes, nextsibling, previoussibling, parentnode nodename, nodetype, nodevalue, attributes methods: appendchild, insertbefore, removechild, replacechild getelementsbytagname, getattribute, hasattributes, haschildnodes Prototype methods: ancestors, childelements, descendants, firstdescendant, descendantof, next, previous, siblings, previoussiblings, nextsiblings, adjacent

XML DOM tree structure the XML tags have a tree structure DOM nodes have parents, children, and siblings Analyzing a fetched XML file using DOM Assume the following XML file is returned via an Ajax request: <?xml version="1.0" encoding="utf-8"?> <foo bloop="bleep"> <bar/> <baz><quux/></baz> <baz><xyzzy/></baz> </foo> We can use DOM properties and methods on ajax.responsexml: // zeroth element of array of length 1 var foo = ajax.responsexml.getelementsbytagname("foo")[0]; // same var bar = foo.getelementsbytagname("bar")[0]; // array of length 2 var all_bazzes = foo.getelementsbytagname("baz"); // string "bleep" var bloop = foo.getattribute("bloop");

Recall: Pitfalls of the DOM Using the same file: <?xml version="1.0" encoding="utf-8"?> <foo bloop="bleep"> <bar/> <baz><quux/></baz> <baz><xyzzy/></baz> </foo> We are reminded of some pitfalls of the DOM: // works - XML prolog is removed from document tree var foo = ajax.responsexml.firstchild; // WRONG - just a text node with whitespace! var bar = foo.firstchild; // works var first_baz = foo.getelementsbytagname("baz")[0]; // WRONG - just a text node with whitespace! var second_baz = first_baz.nextsibling; // works - why? var xyzzy = second_baz.firstchild;

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> <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> Navigating the node tree don't have ids or classes to use to get specific nodes firstchild/nextsibling properties are unreliable best way to walk the tree is using getelementsbytagname: node.getelementsbytagname("tagname") get an array of all node's children that are of the given tag ("book", "subject", etc.) can be called on the overall XML document or on a specific node node.getattribute("attributename") gets an attribute from a node (e.g., category, lang) Prototype methods also useful: childelements, siblings, next/previous, etc.

Navigating node tree example // make a paragraph for each book about computers var books = ajax.responsexml.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].textcontent; var author = books[i].getelementsbytagname("author")[0].textcontent; } // make an XHTML <p> tag based on the book's XML data var p = document.createelement("p"); p.textcontent = title + ", by " + author; document.body.appendchild(p); } A historical interlude: why XHTML? in XML, different "flavors" can be combined in single document theoretical benefit of including other XML data in XHTML nobody does this most embedded data are in non-xml formats (e.g., Flash) non-xml data must be embedded another way (we'll talk about this later on) requires browser/plugin support for other "flavor" of XML development slow to nonexistent most XML flavors are specialized uses Why XML in AJAX? most data you want are provided in XML the de facto universal format the browser can already parse XML (i.e., XHTML) into DOM objects DOM only defined for XML-based formats, may not map directly to another format would have to manually parse a different format simple formats can be parsed manually from ajax.responsetext most data are easier to manipulate as DOM objects than to parse manually

Practice problem: Animal game Write a program that guesses which animal the user is thinking of. The program will arrive at a guess based on the user's responses to yes or no questions. The questions come from a web app named animalgame.php. Practice problem: Animal game (cont'd) The data comes in the following format: <node nodeid="id"> <question>question</question> <yes nodeid="id" /> <no nodeid="id" /> </node> <node nodeid="id"> <answer>answer</answer> </node> to get a node with a given id: animalgame.php?nodeid=id start by requesting the node with nodeid of 1 to get the first question

Attacking the problem Questions we should ask ourselves: 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? Debugging responsexml in Firebug can examine the entire XML document, its node/tree structure