1 Dr Alexiei Dingli XML Technologies SAX and DOM.

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 8-1 Created by Cheryl M. Hughes, Harvard University Extension School Cambridge, MA The Web Wizards Guide.
Advertisements

XML IV. The Document Object Model The Document Object model is a hierarchical structure of an XML document. It provides a means for accessing, and manipulating.
The Document Object Model
DOM. Document Object Model • Orginalt DOM for JavaScript – Manipulere dokumenter dynamisk i nettleser • Språknøytral API – OMG IDL • Standard for å –
1 Extensible Markup Language: XML HTML: portable, widely supported protocol for describing how to format data XML: portable, widely supported protocol.
Document Object Model. Lecture 18 The Document Object Model (DOM) is not a programming language It is an object-oriented model of web documents Each.
Document Object Model (DOM): An Abstract Data Structure for XML data Alex Dekhtyar Department of Computer Science University of Kentucky.
Tutorial 16 Working with Dynamic Content and Styles.
1 Extensible Markup Language: XML HTML: portable, widely supported protocol for describing how to format data XML: portable, widely supported protocol.
XML DOM and SAX Parsers By Omar RABI. Introduction to parsers  The word parser comes from compilers  In a compiler, a parser is the module that reads.
1 Extensible Markup Language: XML HTML: widely supported protocol for formatting data XML: widely supported protocol for describing data XML is quickly.
1 XML Data Management 4. Domain Object Model Werner Nutt.
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)
1 Document Object Model (DOM) MV4920 – XML 24 September 2001 Simon R. Goerger MAJ, US Army
1 The Document Object Model SAMS Teach Yourself JavaScript in 24 Hours (Fourth Edition) Michael Moncur SAMS Publishing 2007.
5 Processing XML Parsing XML documents  Document Object Model (DOM)  Simple API for XML (SAX) Class generation Overview.
JavaScript & DOM Client-side scripting. JavaScript JavaScript is a tool to automate client side (which is implemented using HTML so far) JavaSript syntax.
CIS 375—Web App Dev II DOM. 2 Introduction to DOM The XML Document ________ Model (DOM) is a programming interface for XML documents. It defines the way.
Parsing with DOM using MSXML Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
Electronic Commerce COMP3210 Session 4: Designing, Building and Evaluating e-Commerce Initiatives – Part II Dr. Paul Walcott Department of Computer Science,
DOM Robin Burke ECT 360. Outline XHTML in Schema JavaScript DOM (MSXML) Loading/Parsing Transforming parameter passing DOM operations extracting data.
Fall 2006 Florida Atlantic University Department of Computer Science & Engineering COP 4814 – Web Services Dr. Roy Levow Part 4 - XML.
Working with the XML Document Object Model ©NIITeXtensible Markup Language/Lesson 7/Slide 1 of 44 Objectives In this lesson, you will learn to: *Identify.
The XML Document Object Model (DOM) Aug’10 – Dec ’10.
XML DOM Functionality in.NET DSK Chakravarthy
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
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.
1 Dr Alexiei Dingli XML Technologies XML Advanced.
Javascript II DOM & JSON. In an effort to create increasingly interactive experiences on the web, programmers wanted access to the functionality of browsers.
XML Document Object Model Anthony Borquez. The Document Object Model a programming interface for HTML and XML documents. It defines the way a document.
McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Scripting with the DOM Ellen Pearlman Eileen Mullin Programming the Web.
Sheet 1XML Technology in E-Commerce 2001Lecture 3 XML Technology in E-Commerce Lecture 3 DOM and SAX.
Introduction to XML This presentation covers introductory features of XML. What XML is and what it is not? What does it do? Put different related technologies.
XP Tutorial 16 New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with Dynamic Content and Styles Creating a Dynamic Table of Contents.
C# and Windows Programming XML Processing. 2 Contents Markup XML DTDs XML Parsers DOM.
XML Study-Session: Part III
Introduction to the Document Object Model Eugenia Fernandez IUPUI.
Markup basics. Markup languages add commentary to text files –so that the commentary can be ignored if not understood eg HyperText Markup Language –adds.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 4 1COMP9321, 15s2, Week.
Document Object Model. Back to the DOM… Javascript and the DOM  Originally, the Document Object Model (DOM) and Javascript were tightly bound  Each.
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.
XML DOM.
CO1552 – Web Application Development Further JavaScript: Part 1: The Document Object Model Part 2: Functions and Events.
DOM (Document Object Model) - Parsing and Reading HTML and XML -
Computer Information System Information System California State University Los Angeles Jongwook Woo CIS 461 Web Development I HTML DOM part I Jongwook.
Document Object Model.  The XML DOM (Document Object Model) defines a standard way for accessing and manipulating XML documents.  The DOM presents an.
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.
XML DOM  XML Document Object Model provides a robust international standard for XML Documents.  DOM Level 1 is a Dec 11, 1998 W3C recommendation.  XML.
XML DOM Week 11 Web site:
DOM Dr. Reda Salama. Back to the HTML DOM Once we get a response back from the server, we probably want to update our HTML page The HTML page itself is.
XP Tutorial 10 New Perspectives on JavaScript, Comprehensive 1 Working with Dynamic Content and Styles Creating a Dynamic Table of Contents.
THE DOM.
XML Parsers Overview Types of parsers Using XML parsers SAX DOM
In this session, you will learn to:
XML Parsers.
Scripting the DOM MIS 3502, Fall 2016 Jeremy Shafer Department of MIS
Applied Online Programming
Introduction to the Document Object Model
Week 11 Web site: XML DOM Week 11 Web site:
DOM Document Object Model.
XML Parsers Overview Types of parsers Using XML parsers SAX DOM
In this session, you will learn to:
More Sample XML By Sadia Anjum.
2017, Fall Pusan National University Ki-Joune Li
XML DOM and CSS Instructors: Geoffrey Fox and Bryan Carpenter
XML Programming in Java
XML and Web Services (II/2546)
Presentation transcript:

1 Dr Alexiei Dingli XML Technologies SAX and DOM

2 Simple API for XML Used to parse XML But does not create a default object It just fires events when it detects objects such as –open or close tags –PCDATA or CDATA –Comments –entities What is SAX?

3 Imagine the following document: Dingli Alexiei University of Malta Example

4 1.Creating a custom object model (like Person and AddressBook classes) 2.Creating a SAX parser 3.Creating a DocumentHandler (to turn your XML document into instances of your custom object model). SAX in 3 steps

5 Create both a person and an address book object Create its setters, getters and to xml methods Custom Object Model (1)

6 Custom Object Model (2)

7 Create a SAX parser

8 Actually 4 Interfaces... –The Document Handler –The Entity Resolver –The DTD Handler –The Error Handler Create a Document Handler (1)

9 Create a Document Handler (2)

10 parser.setDocumentHandler(... ) parser.setDTDHandler(... ) parser.setErrorHandler(... ) Setting the parser

11 Rather than implementing all the interfaces mentioned earlier Make use of org.xml.sax.helpers.DefaultHandler Which implements all the methods And you simply override what you want to use xml/sax/helpers/DefaultHandler.html Handler Class

12 SAX Handler Example Handler

13 W3C standard Standard way of accessing and manipulating documents Divided into 3 parts –Core DOM (access any structured document) –XML DOM –HTML DOM Presents element as a tree structure DOM

14 A standard object model for XML A standard programming interface for XML Platform- and language-independent A W3C standard The XML DOM is a standard for how to get, change, add, or delete XML elements XML DOM

15 Everything in XML is a node –The entire document is a document node –Every XML element is an element node –The text in the XML elements are text nodes –Every attribute is an attribute node –Comments are comment nodes XML DOM rulez

16 Learning XML 2008 Bookstore is the root node It contains one book node A book node contains a title node and a year node Title contains a text node “Learning XML” 2008 is not the value of the year node but a text node inside the year node Example

17 Any DOM object has a node tree where –In a node tree, the top node is called the root –Every node, except the root, has exactly one parent node –A node can have any number of children –A leaf is a node with no children –Siblings are nodes with the same parent The node tree

18 text=" " text=text+" "; text=text+" Everyday Italian "; text=text+" John Smith "; text=text+" 2008 "; text=text+" "; Creating the XML

19 try //Internet Explorer { xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async="false"; xmlDoc.loadXML(text); } catch(e) { try //Firefox, Mozilla, Opera, etc. { parser=new DOMParser(); xmlDoc=parser.parseFromString(text,"text/xml"); } catch(e) { alert(e.message) } document.write("xmlDoc is loaded, ready for use"); Parsing the XML

20 x.getElementsByTagName(name) - get all elements with a specified tag name x.appendChild(node) - insert a child node to x x.removeChild(node) - remove a child node from x XML DOM Methods

21 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 x.attributes - the attributes nodes of x XML DOM properties

22 document.write(xmlDoc.getElementsByTagName("title") [0].childNodes[0].nodeValue); document.write(" "); document.write(xmlDoc.getElementsByTagName("author") [0].childNodes[0].nodeValue); document.write(" "); document.write(xmlDoc.getElementsByTagName("year") [0].childNodes[0].nodeValue); Examples

23 1.By using the getElementsByTagName() method 2.By looping through (traversing) the nodes tree 3.By navigating the node tree, using the node relationships Accessing nodes

24 xmlDoc.getElementsByTagName("title") [0].childNodes[0].nodeValue; Example 1

25 x=xmlDoc.getElementsByTagName("title"); for ( i=0; i "); } Example 2

26 x=xmlDoc.getElementsByTagName("book")[0].childNodes; y=xmlDoc.getElementsByTagName("book")[0].firstChild; for (i=0;i<x.length;i++) { if (y.nodeType==1) {//Process only element_nodes (type 1) document.write(y.nodeName + " "); } y=y.nextSibling; } Example 3

27 nodeName nodeValue nodeType Node properties

28 nodeName is read-only nodeName of an element node is the same as the tag name nodeName of an attribute node is the attribute name nodeName of a text node is always #text nodeName of the document node is always #document nodeName property

29 nodeValue for element nodes is undefined nodeValue for text nodes is the text itself nodeValue for attribute nodes is the attribute value nodeValue property

30 Node typeNodeType Element1 Attribute2 Text3 Comment8 Document9 nodeType property

31 x=xmlDoc.getElementsByTagName("book")[0].attributes; document.write(x.getNamedItem("category").nodeValue); Acessing node attributes

32 // documentElement always represents the root node x=xmlDoc.documentElement.childNodes; for (i=0;i<x.length;i++) { document.write(x[i].nodeName); document.write(": "); document.write(x[i].childNodes[0].nodeValue); document.write(" "); } Traversing Example

33 parentNode childNodes firstChild lastChild nextSibling previousSibling Navigating Nodes (1)

34 Navigating Nodes (2)

35 x=xmlDoc.getElementsByTagName("title")[0]; y=x.childNodes[0]; txt=y.nodeValue; Result = the name of the book Title node > Text node Getting the node value

36 x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]; x.nodeValue="Easy Cooking"; Setting the node value

37 y=xmlDoc.getElementsByTagName("book")[0]; xmlDoc.documentElement.removeChild(y); Or y.parentNode.removeChild(y); Removing Nodes

38 newel=xmlDoc.createElement("edition"); x=xmlDoc.getElementsByTagName("book")[ 0]; x.appendChild(newel); Creating nodes

39 newel=xmlDoc.createElement("edition"); newtext=xmlDoc.createTextNode("first"); newel.appendChild(newtext); x=xmlDoc.getElementsByTagName("book")[ 0]; x.appendChild(newel); Creating text nodes

40 newCDATA=xmlDoc.createCDATASection(" Special Offer & Book Sale"); x=xmlDoc.getElementsByTagName("book")[ 0]; x.appendChild(newCDATA); Create CDATA nodes

41 newComment=xmlDoc.createComment("Re vised March 2008"); x=xmlDoc.getElementsByTagName("book")[ 0]; x.appendChild(newComment); Create Comment Node

42 x.appendChild(newNode) x.insertBefore(newNode,y) x.cloneNode(true) // add all attributes and children if true x.insertData(offset,"Easy "); // add text More additional methods

43 x=xmlDoc.getElementsByTagName("title")[0].getAttribu teNode("lang"); txt=x.nodeValue; Getting attribute value

44 newatt=xmlDoc.createAttribute("edition"); newatt.nodeValue="first"; x=xmlDoc.getElementsByTagName("title"); x[0].setAttributeNode(newatt); Creating attributes

45 x=xmlDoc.getElementsByTagName('book'); x[0].setAttribute("category","food"); Or x=xmlDoc.getElementsByTagName("book")[0] y=x.getAttributeNode("category"); y.nodeValue="food"; Setting the attribute value

46 x=xmlDoc.getElementsByTagName("book"); x[0].removeAttribute("category"); Removing attributes

47 Given the following XML fileXML file How shall we display –Two buttons “Get CD info” and display the Titles and the Composer “Get CD info abridged” and display the Titles only Exercise

48 The code What’s the result? Answer (1)

49 Answer (2)

50 Questions?