XML CSC 443: Web Programming

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

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

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)

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

XML (Extensible Markup Language)

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

DOM Interface subset 1/ 2

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

AJAX: The Basics CISC 282 March 25, 2014

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

INLEDANDE WEBBPROGRAMMERING MED JAVASCRIPT INTRODUCTION TO WEB PROGRAMING USING JAVASCRIPT

Web Programming Paper Solution (Chapter wise)

AJAX: The Basics CISC 282 November 22, 2017

INDEX SYMBOLS See also

EXtensible Markup Language XML

Document Object Model (DOM)

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

Ajax. Ronald J. Glotzbach

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

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

Web Programming Step by Step

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

Introducing the JavaScript DOM

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

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

Recall: Document Object Model (DOM)

Chapter 11 Objectives

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

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

Javascript. Many examples from Kyle Simpson: Scope and Closures

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

The components of a basic XML system.

Web Programming Step by Step

INTERNET PROGRAMMING XML

CSE 154 LECTURE 10: THE DOM TREE

XML. Jonathan Geisler. April 18, 2008

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

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

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

EECS1012. Net-centric Introduction to Computing. Lecture JavaScript DOM

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

The Document Object Model (DOM) The Browser Object Model (BOM) Client-side Javascript. Web Development 2 CS1116/CS5018

Ingegneria del Software T. XML Programming

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

CSC System Development with Java Working with XML

HTML CS 4640 Programming Languages for Web Applications

Application Note AN Copyright InduSoft Systems LLC 2006

Informatics 1: Data & Analysis

XML: Tools and Extensions

Using JavaScript for Client-Side Behavior

COMP9321 Web Application Engineering. Extensible Markup Language (XML)

XML: Tools and Extensions

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

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

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

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

User Interaction: jquery

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

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

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

CSC Javascript

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

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

Web services CSCI 470: Web Science Keith Vertanen Copyright 2011

User Interaction: XML and JSON

CSC Web Technologies, Spring Web Data Exchange Formats

Tutorial 1 Getting Started with HTML5. HTML, CSS, and Dynamic HTML 5 TH EDITION

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

(1) I (2) S (3) P allow subscribers to connect to the (4) often provide basic services such as (5) (6)

CSC Web Programming. JavaScript Browser Objects

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

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

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

Announcements. 1. Class webpage: Have you been reading the announcements? Lecture slides and coding examples will be posted

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

XML. extensible Markup Language. ... and its usefulness for linguists

HTML. Hypertext Markup Language. Code used to create web pages

AJAX. Introduction. AJAX: Asynchronous JavaScript and XML

CHAPTER 2 MARKUP LANGUAGES: XHTML 1.0

COMS W3101: SCRIPTING LANGUAGES: JAVASCRIPT (FALL 2018)

Programmazione Web a.a. 2017/2018 HTML5

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

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

CSE 154 LECTURE 17: WEB SERVICES

UNIT 3 SECTION 1 Answer the following questions Q.1: What is an editor? editor editor Q.2: What do you understand by a web browser?

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

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

Project 3 CIS 408 Internet Computing

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

Make a Website. A complex guide to building a website through continuing the fundamentals of HTML & CSS. Created by Michael Parekh 1

MODULE 2 HTML 5 FUNDAMENTALS. HyperText. > Douglas Engelbart ( )

HTML. Mohammed Alhessi M.Sc. Geomatics Engineering. Internet GIS Technologies كلية اآلداب - قسم الجغرافيا نظم المعلومات الجغرافية

CSC 443: Web Programming

Transcription:

1 XML CSC 443: Web Programming Haidar Harmanani Department of Computer Science and Mathematics Lebanese American University Byblos, 1401 2010 Lebanon What is XML? 2 XML: a "skeleton" for creating markup languages you already know it! syntax is identical to XHTML's: <element attribute="value">content</element> languages written in XML specify: names of tags in XHTML: h1, div, img, etc. names of attributes in XHTML: id/class, src, href, etc. rules about how they go together in XHTML: inline vs. block-level elements

Why do we need XML? 3 to present complex data in human-readable form "self-describing data" Anatomy of an XML file 4 <?xml version="1.0" encoding="utf-8"?> <!-- XML prolog --> <note> <!-- root element --> <to>tove</to> <from>jani</from> <!-- element ("tag") --> <subject>reminder</subject> <!-- content of element --> <message language="english"> <!-- attribute and its value --> Don't forget 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 XHTML

Uses of XML 5 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 Pros and cons of XML 6 pro: 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)

Pros and cons of XML 7 con: bulky syntax/structure makes files large; can decrease performance n example: quadratic formula in MathML can be hard to "shoehorn" data into a good XML format What tags are legal in XML? 8 any tags you want! examples: an email message might use tags called to, from, subject a library might use tags called book, title, author when designing an XML file, you choose the tags and attributes that best represent the data rule of thumb: data = tag, metadata = attribute

Doctypes and Schemas 9 "rule books" for individual flavors of XML list which tags and attributes are valid in that language, and how they can be used together used to validate XML files to make sure they follow the rules of that "flavor" the W3C HTML validator uses the XHTML doctype to validate your HTML for more info: Document Type Definition (DTD) ("doctype") W3C XML Schema XML and Ajax 10 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, very similar to the (X)HTML DOM

XML DOM tree structure 11 <?xml version="1.0" encoding="utf-8"?> <categories> <category>children</category> <category>computers</category>... </categories> XML the XML tags have a tree structure DOM nodes have parents, children, and siblings XML DOM tree structure 12

Recall: Javascript XML (XHTML) DOM 13 The DOM properties and methods we already know can be used on XML nodes: properties: n firstchild, lastchild, childnodes, nextsibling, n previoussibling, parentnode n nodename, nodetype, nodevalue, attributes methods: n appendchild, insertbefore, removechild, replacechild n getelementsbytagname, getattribute, hasattributes, haschildnodes caution: cannot use HTML-specific properties like innerhtml in the XML DOM! Navigating the node tree 14 caution: can only use standard DOM methods and properties in XML DOM HTML DOM has Prototype methods, but XML DOM does not! caution: can't use ids or classes to use to get specific nodes id and class are not necessarily defined as attributes in the flavor of XML being read

Navigating the node tree 15 caution: firstchild/nextsibling properties are unreliable annoying whitespace text nodes! the best way to walk the XML tree: var elms = node.getelementsbytagname("tagname") returns an array of all node's children of the given tag name node.getattribute("attributename") gets an attribute of an element Using XML data in a web page 16 Procedure: 1. use Ajax to fetch data 2. use DOM methods to examine XML: n XMLnode.getElementsByTagName() 3. extract the data we need from the XML: n XMLelement.getAttribute(), XMLelement.firstChild.nodeValue, etc. 4. create new HTML nodes and populate with extracted data: n document.createelement(), HTMLelement.innerHTML 5. inject newly-created HTML nodes into page n HTMLelement.appendChild()

Fetching XML using AJAX (template) 17 new Ajax.Request( "url", { method: "get", onsuccess: functionname } );... function functionname(ajax) { do something with ajax.responsexml; } JS ajax.responsetext contains the XML data in plain text ajax.responsexml is a pre-parsed XML DOM object 18 Analyzing a fetched XML file using DOM <?xml version="1.0" encoding="utf-8"?> <foo bloop="bleep"> <bar/> <baz><quux/></baz> <baz><xyzzy/></baz> </foo> XML We can use DOM properties and methods on ajax.responsexml: // zeroth element of array of length 1 var foo = ajax.responsexml.getelementsbytagname("foo")[0]; // ditto var bar = foo.getelementsbytagname("bar")[0]; // array of length 2 var all_bazzes = foo.getelementsbytagname("baz"); // string "bleep" var bloop = foo.getattribute("bloop"); JS

Larger XML file example 19 <?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> XML Navigating node tree example 20 // 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") { // extract data from XML var title = books[i].getelementsbytagname("title")[0].firstchild.node Value; var author = books[i].getelementsbytagname("author")[0].firstchild.nod evalue; // make an XHTML <p> tag containing data from XML var p = document.createelement("p"); p.innerhtml = title + ", by " + author; document.body.appendchild(p); } } JS

Resources 21 http://www.sitepoint.com/really-good-introductionxml/ http://www.w3.org/xml/schema.html