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

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

11. EXTENSIBLE MARKUP LANGUAGE (XML)

COMS W3101: SCRIPTING LANGUAGES: JAVASCRIPT (FALL 2018)

XML CSC 443: Web Programming

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

XML (Extensible Markup Language)

XML extensible Markup Language

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

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

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

Bioinforma)cs Resources XML / Web Access

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

Web Programming Paper Solution (Chapter wise)

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

CSE 154 LECTURE 23: XML

CSC System Development with Java Working with XML

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

User Interaction: XML and JSON

User Interaction: XML and JSON

CSC Web Technologies, Spring Web Data Exchange Formats

Javascript. UNIVERSITY OF MASSACHUSETTS AMHERST CMPSCI 120 Fall 2010

Informatics 1: Data & Analysis

EXtensible Markup Language XML

INTERNET PROGRAMMING XML

Interactor Tree. Edith Law & Mike Terry

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

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

AJAX: The Basics CISC 282 March 25, 2014

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

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

Produced by. App Development & Modeling. BSc in Applied Computing. Eamonn de Leastar

Lesson 12: JavaScript and AJAX

singly and doubly linked lists, one- and two-ended arrays, and circular arrays.

AJAX: The Basics CISC 282 November 22, 2017

COURSE OUTLINE MOC 20480: PROGRAMMING IN HTML5 WITH JAVASCRIPT AND CSS3

Overview. Event Handling. Introduction. Reviewing the load Event. Event mousemove and the event Object. Rollovers with mouseover and mouseout

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

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

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

Chapter 2 XML, XML Schema, XSLT, and XPath

Web Security, Summer Term 2012

Web Security, Summer Term 2012

User Interaction: XML and JSON

Course 20480: Programming in HTML5 with JavaScript and CSS3

CE212 Web Application Programming Part 2

COURSE 20480B: PROGRAMMING IN HTML5 WITH JAVASCRIPT AND CSS3

W3C XML XML Overview

Programming in HTML5 with JavaScript and CSS3

Introduction to XML. Asst. Prof. Dr. Kanda Runapongsa Saikaew Dept. of Computer Engineering Khon Kaen University

Web Development 20480: Programming in HTML5 with JavaScript and CSS3. Upcoming Dates. Course Description. Course Outline

JavaScript and XHTML. Prof. D. Krupesha, PESIT, Bangalore

1 Introduction. 2 Web Architecture

Data Visualization (DSC 530/CIS )

XML. Jonathan Geisler. April 18, 2008

Chapter 13 XML: Extensible Markup Language

CLASS DISCUSSION AND NOTES

Data Visualization (DSC 530/CIS )

Introduction to XML 3/14/12. Introduction to XML

Data Visualization (CIS 468)

Computer Science E-75 Building Dynamic Websites

Building Dynamic Forms with XML, XSLT

The Extensible Markup Language (XML) and Java technology are natural partners in helping developers exchange data and programs across the Internet.

Trees. Carlos Moreno uwaterloo.ca EIT

Overview. Introduction. Introduction XML XML. Lecture 16 Introduction to XML. Boriana Koleva Room: C54

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

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

Programming in HTML5 with JavaScript and CSS3

Programming in HTML5 with JavaScript and CSS3

One of the main selling points of a database engine is the ability to make declarative queries---like SQL---that specify what should be done while

User Interaction: XML and JSON

jquery Tutorial for Beginners: Nothing But the Goods

Microsoft Programming in HTML5 with JavaScript and CSS3

First Name Last Name CS-081 March 23, 2010 Midterm Exam

Project Title REPRESENTATION OF ELECTRICAL NETWORK USING GOOGLE MAP API. Submitted by: Submitted to: SEMANTA RAJ NEUPANE, Research Assistant,

AJAX. Introduction. AJAX: Asynchronous JavaScript and XML

Semistructured data, XML, DTDs

Some more XML applications and XML-related standards (XLink, XPointer, XForms)

COMP9321 Web Application Engineering. Extensible Markup Language (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:

Computer Science S-75 Building Dynamic Websites

Comp 426 Midterm Fall 2013

Todd toddreifsteck

20480B: Programming in HTML5 with JavaScript and CSS3

Data Visualization on the Web with D3

CSI 3140 WWW Structures, Techniques and Standards. Browsers and the DOM

Introduction to XML. An Example XML Document. The following is a very simple XML document.

Editing ReqIF-XHTML fragments with Writer

Web services CSCI 470: Web Science Keith Vertanen Copyright 2011

Binary Trees

Inductive Data Types

INLS 760 Web Databases Lecture 12 XML, XPATH, XSLT

The LuaXML library. Version 0.1a Introduction 2

extensible Markup Language (XML) Basic Concepts

Data Visualization (CIS/DSC 468)

NATIONAL DIPLOMA IN COMPUT TER TECHNOLOGY

The components of a basic XML system.

CS474 MULTIMEDIA TECHNOLOGY

HTML CSS JAVASCRIPT WEB PUBLISHING IN ONE HOUR A DAY SAMS TEACH YOURSELF COVERING HTML5 CSS3 AND JQUERY 7TH EDITION

Duncan Temple Lang. This is more of a ``how to'' than a ``why'', or ``how does it work'' document.

Transcription:

E-Applications XML and DOM in Javascript Michail Lampis michail.lampis@dauphine.fr

Acknowledgment Much of the material on these slides follows the tutorial given in: http://www.w3schools.com/dom/

XML XML (extensible Markup Language) is a general language format for storing structured text data XML is not (exactly) a language itself, but a general format which a data-storing language can follow

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="children"> <title lang="en">harry Potter</title> <author>j K. Rowling</author> <year>2005</year> <price>29.99</price> </book> </bookstore>

XML The general syntax follows the conventions of HTML Open and close tags: <tag-name> </tag-name> Attributes can be given inside the opening tag <tag-name attr1= val1 attr2= val2 >... </tag-name> Proper nesting must be followed: <a><b> </b></a>

XML Meaning (X)HTML is a form of XML A specific set of tags are allowed (e.g. <p>, <table>, <ol>,...) Their meaning is well-defined Which tags are we allowed to use in XML? What do they mean? ANYTHING!

XML languages XML allows us to write structured data This is useful in many applications: Data becomes easier to read/write/process than plain text Depending on the application we have in mind we can define an XML format to store our data, or use a pre-existing one.

XML languages Example: MathML is a standardized XML format for writing mathematical formulas It contains appropriate tags, such as: <mfrac>: denotes a fraction <msqrt>: denotes a square root...

XML languages Example: SVG (scalable vector graphics) is an XML-based format for storing (scalable) image data It contains tags such as: <rect>: rectangle <circle>: cycle...

XML languages Example: GraphML is an XML-based format for describing graphs. It contains tags such as: <node> <edge>...

Why do we care? XML is one of the most common information interchange formats on the web today. Furthermore, in this class we care a lot about AJAX Asynchronous Javascript and XML High-level idea: client-side programs which communicate with the server without refreshing the page (asynchronous) and send/receive data in XML.

Reading an XML document Javascript gives us facilities to request and read XML xhttp = new XMLHttpRequest(); xhttp.open( GET, books.xml,false); xhttp.send(); What this means: Send the server a GET request for the file books.xml The request is not asynchronous (3 rd parameter is false)...

XMLHttpRequest XMLHttpRequest is the general constructor we need to request XML files from the server Generally, for security reasons, we are only allowed to request XML files from the same domain We prepare a request, and send it The xhttp object then contains the response

XMLHttpRequest xhttp = new XMLHttpRequest(); xhttp.open( GET, books.xml,false); xhttp.send(); xhttp.responsexml contains the XML document in DOM format xhttp.responsetext contains the XML document in a string

Parsing XML The whole point of using XML is that we get the data structured in a nice object (DOM) We can still access the string XML If necessary, we can transform a string into a DOM object using the XML parser parser=new DOMParser(); xmldoc=parser.parsefromstring(text,"text/xml");

Document Object Model The DOM is the abstract model that describes the structure of an XML document We have already used such an object: The window.document object is a DOM representation of the current HTML document DOM works similarly (but not exactly the same) for HTML and XML

DOM Tree structure The DOM structure sees the XML document as a node-tree Each XML tag is a node...which may contain other nodes as children

Example (again) <?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="children"> <title lang="en">harry Potter</title> <author>j K. Rowling</author> <year>2005</year> <price>29.99</price> </book> </bookstore>

DOM Tree structure

DOM Tree Structure

DOM Tree structure Terminology: The initial node of the XML document is the root node Every node has a parent (except the root) A leaf is a node without children Two nodes with the same parent are siblings

Accessing nodes Basic node properties x.nodename - the name of x x.nodevalue - the value of x x.parentnode - the parent node of x x.childnodes - the child nodes of x (Array!) x.attributes - the attributes nodes of x

Accessing nodes Basic node methods x.getelementsbytagname(name) - get all elements with a specified tag name (Array!) x.appendchild(node) - insert a child node to x x.removechild(node) - remove a child node from x x.getelementbyid(id) - recall how this works in HTML

Some examples Let's do some examples with the HTML DOM This allows us to manipulate the web page the user is looking at, and practice our DOM skills! newitem = document.createelement("li"); This creates a new node. Its HTML tag is <li> Attention this does not (yet) appear anywhere in the document!

Some Examples newitem = document.createelement("li"); newitem.innerhtml = Some text ; This creates a new tag with type <li> which contains the text Some text Equivalent to writing <li>some text</li> in HTML This still does not appear anywhere...

Some examples Consider the following HTML <ul id= mylist > </ul> We can add elements to this list mylist = document.getelementbyid("mylist"); newitem = document.createelement("li"); newitem.innerhtml = Some text ; mylist.appendchild(newitem);

Some examples We can also set attributes of elements we add newitem = document.createelement("a"); newitem.innerhtml = Some link ; newitem.setattribute( href, http://www.google.c om );

Some examples The previous method can be used to add a (unique) id to an item newitem.setattribute( id, someid ); We can the use this to find (and remove?) the item myitem = document.getelementbyid( someid ); myitem.parentnode.removechild(myitem);

Event listeners Accessing DOM elements of the HTML page is also useful because it allows us to manipulate the event-handling procedures associated with them. addeventlistener()/removeeventlistener()

Example document.getelementbyid( canvas ).addeventlistener( m ousedown,myfunction); (or mouseup, mousemove,...) document.getelementbyid( canvas ).removeeventlistener ( mousedown,myfunction);