CSE 154 LECTURE 12: XML

Similar documents
CSE 154 LECTURE 23: XML

Web Programming Step by Step

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

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

AJAX: The Basics CISC 282 March 25, 2014

DOM Interface subset 1/ 2

AJAX: The Basics CISC 282 November 22, 2017

Ajax. Ronald J. Glotzbach

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

INDEX SYMBOLS See also

INLEDANDE WEBBPROGRAMMERING MED JAVASCRIPT INTRODUCTION TO WEB PROGRAMING USING JAVASCRIPT

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

AJAX and JSON. Day 8

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

AJAX: Introduction CISC 282 November 27, 2018

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

CS Final Exam Review Suggestions - Spring 2018

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

Web Programming Step by Step

CSE 154 LECTURE 10: THE DOM TREE

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

CSE 154 LECTURE 17: WEB SERVICES

INTERNET PROGRAMMING XML

XML Processing & Web Services. Husni Husni.trunojoyo.ac.id

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

Asynchronous JavaScript + XML (Ajax)

The components of a basic XML system.

Web Programming Paper Solution (Chapter wise)

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

COMP 1130 Programming Fundamentals (Javascript Rocks)

Financial IT Examination Syllabus(2018)

Chapter 11 Objectives

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

Document Object Model (DOM)

XML Extensible Markup Language

CSC Web Technologies, Spring Web Data Exchange Formats

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

Introduction to XML. When talking about XML, here are some terms that would be helpful:

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

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

Web Programming Step by Step

DOM. Ajax Technology in Web Programming. Sergio Luján Mora. DLSI - Universidad de Alicante 1. API for accessing and manipulating HTML documents

CSC Web Programming. JavaScript Browser Objects

So, if you receive data from a server, in JSON format, you can use it like any other JavaScript object.

Programming the World Wide Web by Robert W. Sebesta

Recall: Document Object Model (DOM)

CITS1231 Web Technologies. Ajax and Web 2.0 Turning clunky website into interactive mashups

XML. XML Syntax. An example of XML:

JavaScript Specialist v2.0 Exam 1D0-735

CSC Javascript

Introduction to Database Systems CSE 414

.. Cal Poly CPE/CSC 366: Database Modeling, Design and Implementation Alexander Dekhtyar..

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

EXtensible Markup Language XML

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

XML: Introduction. !important Declaration... 9:11 #FIXED... 7:5 #IMPLIED... 7:5 #REQUIRED... Directive... 9:11

XML. Jonathan Geisler. April 18, 2008

Financial IT Examination Syllabus(2017)

AJAX: From the Client-side with JavaScript, Back to the Server

Client Side JavaScript and AJAX

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

User Interaction: jquery

Javascript. Many examples from Kyle Simpson: Scope and Closures

XMLHttpRequest. CS144: Web Applications

IN Development in Platform Ecosystems Lecture 2: HTML, CSS, JavaScript

XML: Tools and Extensions

Working with Database. Client-server sides AJAX JSON Data formats Working with JSON data Request Response Bytes Database

Application Note AN Copyright InduSoft Systems LLC 2006

XML: Tools and Extensions

Ajax. David Matuszek's presentation,

CSC 337. JavaScript Object Notation (JSON) Rick Mercer

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

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

JavaScript 3. Working with the Document Object Model (DOM) and DHTML

Introducing the JavaScript DOM

Informatics 1: Data & Analysis

React. HTML code is made up of tags. In the example below, <head> is an opening tag and </head> is the matching closing tag.

5/19/2015. Objectives. JavaScript, Sixth Edition. Understanding the Browser Object Model and the Document Object Model. Objectives (cont'd.

Ajax- XMLHttpResponse. Returns a value such as ArrayBuffer, Blob, Document, JavaScript object, or a DOMString, based on the value of

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

COMS W3101: SCRIPTING LANGUAGES: JAVASCRIPT (FALL 2018)

Intro to XML. Borrowed, with author s permission, from:

Lecture : 3. Practical : 2. Course Credit. Tutorial : 0. Total : 5. Course Learning Outcomes

Introduction to XML. XML: basic elements

Using JavaScript for Client-Side Behavior

Chapter 13 XML: Extensible Markup Language

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

Ingegneria del Software T. XML Programming

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

COMP9321 Web Application Engineering. Extensible Markup Language (XML)

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

Welcome to CS50 section! This is Week 10 :(

Web Engineering (CC 552)

COMP9321 Web Application Engineering

11. EXTENSIBLE MARKUP LANGUAGE (XML)

Chapter 1 Introduction to Computers and the Internet

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

JavaScript Introduction

Transcription:

CSE 154 LECTURE 12: XML

Storing structured data in arbitrary text formats (bad) My note: BEGIN FROM: Alice Smith (alice@example.com) TO: Robert Jones (roberto@example.com) SUBJECT: Tomorrow's "Birthday Bash" event! MESSAGE (english): Hey Bob, Don't forget to call me this weekend! PRIVATE: true END XML Many apps make up their own custom text format for storing structured data. We could also send a file like this from the server to browser with Ajax. What's wrong with this approach?

XML: A better way of storing data <?xml version="1.0" encoding="utf-8"?> <note private="true"> <from>alice Smith (alice@example.com)</from> <to>robert Jones (roberto@example.com)</to> <subject>tomorrow's "Birthday Bash" event!</subject> <message language="english"> Hey Bob, Don't forget to call me this weekend! </message> </note> XML extensible Markup Language (XML) is a format for storing nested data with tags and attributes essentially, it's HTML, but you can make up any tags and attributes you want lots of existing data on the web is stored in XML format

Anatomy of an XML file <?xml version="1.0" encoding="utf-8"?> <!-- XML prolog --> <note private="true"> <!-- root element --> <from>alice Smith (alice@example.com)</from> <to>robert Jones (roberto@example.com)</to> <subject>tomorrow's "Birthday Bash" event!</subject> <message language="english"> Hey Bob, Don't forget to call me this weekend! </message> </note> XML begins with an <?xml...?> header tag (prolog) has a single root element (in this case, note) tag, attribute, and comment syntax is just like HTML

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 is the de facto universal format for exchange of data XML languages are used for music, math, vector graphics popular use: RSS for news feeds & podcasts

What tags are legal in XML? any tags you want! examples: a library might use tags book, title, author a song might use tags key, pitch, note when designing XML data, you choose how to best represent the data large or complex pieces of data become tags smaller details and metadata with simple types (integer, string, boolean) become attributes <measure number="1"> <attributes> <divisions>1</divisions> <key><fifths>0</fifths></key> <time><beats>4</beats></time> <clef> <sign>g</sign><line>2</line> </clef> </attributes> <note> <pitch> <step>c</step> <octave>4</octave> </pitch> <duration>4</duration> <type>whole</type> </note> </measure> XML

XML and Ajax web browsers can display XML files, but often you instead want to fetch one and analyze its data the XML data is fetched, processed, and displayed using Ajax (XML is the "X" in "Ajax") It would be very clunky to examine a complex XML structure as just a giant string! luckily, the browser can break apart (parse) XML data into a set of objects there is an XML DOM, similar to the HTML DOM

Fetching XML using Ajax (template) var ajax = new XMLHttpRequest(); ajax.onload = functionname; ajax.open("get", url, true); ajax.send();... function functionname() { do something with this.responsexml; } XML this.responsetext contains the data in plain text (a string) this.responsexml is a parsed XML DOM tree object it has methods very similar to HTML DOM objects

Interacting with XML DOM nodes To get an array of nodes: var elms = node.getelementsbytagname("tag"); var elms = node.queryselectorall("selector"); // all elements var elm = node.queryselector("selector"); // first element XML To get the text inside of a node: var text = node.textcontent; // or, var text = node.firstchild.nodevalue; XML To get the value of a given attribute on a node: var attrvalue = node.getattribute("name"); XML

Differences from HTML DOM Don't usually use getelementbyid because XML nodes don't have IDs or classes. var div = document.getelementbyid("main"); JS Can't get/set the text inside of a node using innerhtml: var text = div.innerhtml; JS Can't get an attribute's value using.attributename: var imageurl = document.getelementbyid("myimage").src; JS

Ajax XML DOM example <?xml version="1.0" encoding="utf-8"?> <employees> <lawyer money="99999.00" /> <janitor name="ed"> <vacuum model="hoover" /> </janitor> <janitor name="bill">no vacuum, too poor</janitor> </employees> // how much money does the lawyer make? var lawyer = this.responsexml.queryselector("lawyer"); var salary = parsefloat(lawyer.getattribute("money")); // 99999.0 // array of 2 janitors var janitors = this.responsexml.queryselectorall("janitor"); var vacmodel = janitors[0].queryselector("vacuum").getattribute("model"); var excuse = janitors[1].textcontent; // "no vacuum, too poor" How would we find out the first janitor's name? (use the Console) How would we find out how many janitors there are? How would we find out how many janitors have vs. don't have vacuums? XML

Exercise: 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 text</question> <yes nodeid="id" /> <no nodeid="id" /> </node> <node nodeid="id"> <answer>answer text</answer> </node> XML XML 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

Full list of XML DOM properties properties: nodename, nodetype, nodevalue, attributes firstchild, lastchild, childnodes, nextsibling, previoussibling, parentnode methods: getelementbyid, getelementsbytagname, queryselector, queryselec torall, getattribute, hasattribute,haschildnodes appendchild, insertbefore, removechild, replacechild full reference

Schemas and Doctypes "rule books" describing which tags/attributes you want to allow in your data used to validate XML files to make sure they follow the rules of that "flavor" the W3C HTML validator uses an HTML schema to validate your HTML (related to <!DOCTYPE html> tag) these are optional; if you don't have one, there are no rules beyond having wellformed XML syntax for more info: W3C XML Schema Document Type Definition (DTD) ("doctype")

Exercise: Late day distribution Write a program that shows how many students turn homework in late for each assignment. Data service here: http://webster.cs.washington.edu/cse154/hw/hw.php parameter: assignment=hwn