CSE 154 LECTURE 23: XML. Storing structured data in arbitrary text formats (bad) My note: BEGIN FROM: Alice Smith TO: Robert Jones.

Slides:



Advertisements
Similar presentations
XML CS What is XML?  XML: a "skeleton" for creating markup languages  you already know it!  syntax is identical to XHTML's: content  languages.
Advertisements

JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
XML XML What XML is and what it means to me as a Computer Scientist By: Derek Edwards CS 376 March 10, 2003.
1 Extensible Markup Language: XML HTML: portable, widely supported protocol for describing how to format data XML: portable, widely supported protocol.
1 Extensible Markup Language: XML HTML: portable, widely supported protocol for describing how to format data XML: portable, widely supported protocol.
XML(EXtensible Markup Language). XML XML stands for EXtensible Markup Language. XML is a markup language much like HTML. XML was designed to describe.
Introduction to XML: Yong Choi CSU Bakersfield.
The Document Object Model (DOM) & Asynchronous Javascript And XML (AJAX) : an introduction UFCEKG-20-2 : Data, Schemas and Applications.
CSE 154 LECTURE 24: XML AND JSON. Debugging responseXML in Firebug can examine the entire XML document, its node/tree structure.
Pemrograman Berbasis WEB XML part 2 -Aurelio Rahmadian- Sumber: w3cschools.com.
Creating a Basic Web Page
XML introduction to Ahmed I. Deeb Dr. Anwar Mousa  presenter  instructor University Of Palestine-2009.
HTML DOM.  The HTML DOM defines a standard way for accessing and manipulating HTML documents.  The DOM presents an HTML document as a tree- structure.
JS: Document Object Model (DOM)
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Pemrograman Berbasis WEB XML -Aurelio Rahmadian- Sumber: w3cschools.com.
Extensible Markup and Beyond
CISC 3140 (CIS 20.2) Design & Implementation of Software Application II Instructor : M. Meyer Address: Course Page:
JavaScript & DOM Client-side scripting. JavaScript JavaScript is a tool to automate client side (which is implemented using HTML so far) JavaSript syntax.
D2L Notes Be sure to submit your link in the dropbox provided on D2L You can just upload an empty text file if a file upload is required Do not use D2L.
Chapter 1 XHTML: Part I The Web Warrior Guide to Web Design Technologies.
Review IDIA 619 Spring 2013 Bridget M. Blodgett. HTML A basic HTML document looks like this: Sample page Sample page This is a simple sample. HTML user.
 XML is designed to describe data and to focus on what data is. HTML is designed to display data and to focus on how data looks.  XML is created to structure,
5.2 DOM (Document Object Model). 2 Motto: To write it, it took three months; to conceive it three minutes; to collect the data in it — all my life. —F.
How do I use HTML and XML to present information?.
WORKING WITH XML IN THE.NET FRAMEWORK. Accessing an XML File Basic activities: open it, read it.NET Framework provides structured and unstructured mechanisms.
1 Dr Alexiei Dingli XML Technologies XML. 2 XML stands for EXtensible Markup Language XML is a markup language much like HTML XML was designed to carry.
WEB APPLICATION DEVELOPMENT For More visit:
Consuming eXtensible Markup Language (XML) feeds.
An Introduction to XML Sandeep Bhattaram
XML Introduction. What is XML? XML stands for eXtensible Markup Language XML stands for eXtensible Markup Language XML is a markup language much like.
What it is and how it works
XML Introduction. Markup Language A markup language must specify What markup is allowed What markup is required How markup is to be distinguished from.
1 Dr Alexiei Dingli XML Technologies SAX and DOM.
XML Presented by Kushan Athukorala. 2 Agenda XML Overview Entity References Elements vs. Atributes XML Validation DTD XML Schema Linking XML and CSS XSLT.
CISC 3140 (CIS 20.2) Design & Implementation of Software Application II Instructor : M. Meyer Address: Course Page:
Markup basics. Markup languages add commentary to text files –so that the commentary can be ignored if not understood eg HyperText Markup Language –adds.
1 Javascript DOM Peter Atkinson. 2 Objectives Understand the nature and structure of the DOM Add and remove content from the page Access and change element.
Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa ME (Modular) CSE 2011 Batch.
When we create.rtf document apart from saving the actual info the tool saves additional info like start of a paragraph, bold, size of the font.. Etc. This.
 defined as Extensible Markup Language (XML) is a set of rules for encoding documents  Defines structure and data.
DTD ]>. Internal DTD Declaration ]> Faculty Director Reminder Please be regular in your classes.
IS444: Modern software development tools Dr. Azeddine Chikh.
DOM (Document Object Model) - Parsing and Reading HTML and XML -
AJAX. Ajax  $.get  $.post  $.getJSON  $.ajax  json and xml  Looping over data results, success and error callbacks.
Advanced DOM Builds on last presentation on DOM Allows you to dynamically create elements and position them on a page DOM methods/properties are W3C standard.
Dave Salinas. What is XML? XML stands for eXtensible Markup Language Markup language, like HTML HTML was designed to display data, whereas XML was designed.
Create Element, Remove Child. The Document Tree Document Element Root Element Element Element Element Element Text: HelloWorld Attribute “href”
JS: Document Object Model (DOM) DOM stands for Document Object Model, and allows programmers generic access to: DOM stands for Document Object Model, and.
SEMI-STRUCTURED DATA (XML) 1. SEMI-STRUCTURED DATA ER, Relational, ODL data models are all based on schema Structure of data is rigid and known is advance.
XML DOM Week 11 Web site:
XML Introduction to XML Extensible Markup Language.
XML Notes taken from w3schools. What is XML? XML stands for EXtensible Markup Language. XML was designed to store and transport data. XML was designed.
XML. What is XML? XML stands for EXtensible Markup Language XML is a markup language much like HTML XML was designed to carry data, not to display data.
XML CS What is XML?  XML: a "skeleton" for creating markup languages  you already know it!  syntax is identical to XHTML's: content  languages.
XML intro. What is XML? XML stands for EXtensible Markup Language XML is a markup language much like HTML XML was designed to carry data, not to display.
XML BASICS and more…. What is XML? In common:  XML is a standard, simple, self-describing way of encoding both text and data so that content can be processed.
CSE 154 LECTURE 13: XML AND JSON. Debugging responseXML in Firebug can examine the entire XML document, its node/tree structure.
CSE 154 Lecture 12: XML.
CSE 154 Lecture 13: XML and JSON.
CSE 154 Lecture 11: AJAx.
CSE 154 Lecture 12: XML.
CSE 154 Lecture 24: JSON.
Prepared for Md. Zakir Hossain Lecturer, CSE, DUET Prepared by Miton Chandra Datta
CSE 154 Lecture 11: AJAx.
DOM Document Object Model.
CSE 154 Lecture 22: AJAX.
What is XML?.
Lecture 12: The Fetch Api and AJAx
Presentation transcript:

CSE 154 LECTURE 23: XML

Storing structured data in arbitrary text formats (bad) My note: BEGIN FROM: Alice Smith TO: Robert Jones 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 Alice Smith Robert Jones Tomorrow's "Birthday Bash" event! Hey Bob, Don't forget to call me this weekend! 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 lots of existing data

What is XML? XML is a "skeleton" for creating markup languages you decide on an XML "language" of tags and attributes that you want to allow in your app XML syntax is mostly identical to HTML's: content the HTML/XML tag syntax is a nice general syntax for describing hierarchical (nested) data when you choose to store data in XML format (or access external XML data), you must decide: names of tags in HTML: h1, div, img, etc. names of attributes in HTML: id / class, src, href, etc. rules about how they go together in HTML: inline vs. block-level elements XML presents complex data in a human-readable, "self-describing" form

Anatomy of an XML file Alice Smith Robert Jones Tomorrow's "Birthday Bash" event! Hey Bob, Don't forget to call me this weekend! XML begins with an 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 graphicsmusicmathvector graphics popular use: RSS for news feeds & podcastsRSS

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 G 2 C 4 4 whole 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

XML DOM tree structure children computers... XML the XML tags have a tree structure DOM nodes have parents, children, and siblings each DOM node object has properties/methods for accessing nearby nodes

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 no vacuum, too poor XML // how much money does the lawyer make? var lawyer = this.responseXML.querySelector("lawyer"); var salary = parseFloat(lawyer.getAttribute("money")); // // 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?

Larger XML file example Everyday Italian Giada De Laurentiis XQuery Kick Start James McGovern Harry Potter J K. Rowling Learning XML Erik T. Ray

Navigating node tree example // make a paragraph for each book about computers var books = this.responseXML.getElementsByTagName("book"); for (var i = 0; i < books.length; i++) { var category = books[i].getAttribute("category"); if (category == "computers") { // extract data from XML var title = books[i].querySelector("title").textContent; var author = books[i].querySelector("author").textContent; // make an HTML tag containing data from XML var p = document.createElement("p"); p.innerHTML = title + ", by " + author; document.body.appendChild(p); } } JS

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. animalgame.php

Practice problem: Animal game (cont'd) The data comes in the following format: question text XML answer text 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 tag) these are optional; if you don't have one, there are no rules beyond having well- formed XML syntax for more info: W3C XML Schema Document Type Definition (DTD) ("doctype") Document Type Definition (DTD)

Exercise: Late day distribution Write a program that shows how many students turn homework in late for each assignment. Data service here: parameter: assignment=hw N