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

Similar documents
Extensible Markup Language (XML) What is XML? Structure of an XML document. CSE 190 M (Web Programming), Spring 2007 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? An example XML file. CSE 190 M (Web Programming), Spring 2008 University of Washington

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

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

CSE 154 LECTURE 23: XML

11. EXTENSIBLE MARKUP LANGUAGE (XML)

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

DOM Interface subset 1/ 2

XML (Extensible Markup Language)

INLEDANDE WEBBPROGRAMMERING MED JAVASCRIPT INTRODUCTION TO WEB PROGRAMING USING JAVASCRIPT

XML extensible Markup Language

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

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

COMS W3101: SCRIPTING LANGUAGES: JAVASCRIPT (FALL 2018)

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

AJAX: The Basics CISC 282 March 25, 2014

AJAX: The Basics CISC 282 November 22, 2017

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

Ajax. Ronald J. Glotzbach

INTERNET & WEB APPLICATION DEVELOPMENT SWE 444. Fall Semester (081) Module 4 (III): XSL

IIUG Conference Announcements

Chapter 11 Objectives

EXtensible Markup Language XML

Bioinforma)cs Resources XML / Web Access

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

Document Object Model (DOM)

Web Programming Paper Solution (Chapter wise)

XML and Databases. Outline

CSC System Development with Java Working with XML

A) XML Separates Data from HTML

CSCC43H: Introduction to Databases. Lecture 10

XML: Tools and Extensions

Module 2 (III): XHTML

XML: Tools and Extensions

CSE 154 LECTURE 12: XML

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

Computer Science 571 Exam #1 Prof. Papa Friday, February 9, 2016, 6:00pm 7:20pm

Application Note AN Copyright InduSoft Systems LLC 2006

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

Document Object Model (DOM) Java API for XML Parsing (JAXP) DOM Advantages & Disadvantage &6&7XWRULDO (GZDUG;LD

XML Document Object Model. - English version -

INDEX SYMBOLS See also

Introducing the JavaScript DOM

Module7: AJAX. Click, wait, and refresh user interaction. Synchronous request/response communication model. Page-driven: Workflow is based on pages

Fall Semester (081) Module7: AJAX

DIPLOMOVÁ PRÁCE. Maroš Vranec XML Benchmarking

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

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

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

CSC Web Programming. JavaScript Browser Objects

Part IV. DOM Document Object Model

Ingegneria del Software T. XML Programming

COMP9321 Web Application Engineering. Extensible Markup Language (XML)

Part IV. DOM Document Object Model. Torsten Grust (WSI) Database-Supported XML Processors Winter 2008/09 70

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

CGS 3066: Spring 2015 JavaScript Reference

Javascript. UNIVERSITY OF MASSACHUSETTS AMHERST CMPSCI 120 Fall 2010

JavaScript: Events, the DOM Tree, jquery and Timing

Web architectures Laurea Specialistica in Informatica Università di Trento. DOM architecture

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

The DOM approach has some obvious advantages:

Module 2 (VI): CSS [Part 3]

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

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

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

Webservices often/usually return XML, very verbose Arabidopsis Genome XML (660 MByte)

RXML: PATH-BASED AND XML DOM APPROACHES FOR INTEGRATING BETWEEN RELATIONAL AND XML DATABASES

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

XML in the Development of Component Systems. The Document Object Model

Recall: Document Object Model (DOM)

XML Data Management. 5. Extracting Data from XML: XPath

Programming the Web VALIDATING FORM INPUT

CSE 214 Computer Science II Introduction to Tree

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

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

IS 242 Web Application Development I

XML Extensible Markup Language

Jeff Offutt. SWE 642 Software Engineering for the World Wide Web

Web Programming Step by Step

Javascript. Many examples from Kyle Simpson: Scope and Closures

AJAX: Introduction CISC 282 November 27, 2018

INTERNET PROGRAMMING XML

Web services CSCI 470: Web Science Keith Vertanen Copyright 2011

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

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

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

Navigating an XML Document

[MS-DOM1X]: Microsoft XML Document Object Model (DOM) Level 1 Standards Support

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

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

CSC Javascript

HTML 5 and CSS 3, Illustrated Complete. Unit L: Programming Web Pages with JavaScript

NATIONAL DIPLOMA IN COMPUT TER TECHNOLOGY

COMP519 Web Programming Lecture 16: JavaScript (Part 7) Handouts

CSC Web Technologies, Spring Web Data Exchange Formats

Web Design. Lecture 6. Instructor : Cristina Mîndruță Site : Cristina Mindruta - Web Design

Module 2 (VII): CSS [Part 4]

Transcription:

INTERNET & WEB APPLICATION DEVELOPMENT SWE 444 Fall Semester 2008-2009 (081) Module 4 (VII): XML DOM Dr. El-Sayed El-Alfy Computer Science Department King Fahd University of Petroleum and Minerals alfy@kfupm.edu.sa Objectives/Outline Objectives Understand the role of XML DOM Learn how to manipulate XML document using the XML DOM Outline What is DOM? The XML DOM The XML DOM Nodes XML DOM Parsers Node Properties Node Methods Examples 4.2

What is DOM? DOM is a W3C standard. DOM defines a standard for accessing documents like XML and HTML: "The W3C Document Object Model (DOM) is a platform and language-neutral interface that allows programs and scripts to dynamically access and update the content, structure, and style of a document. DOM is separated into 3 different parts / levels: Core DOM - standard model for any structured document XML DOM - standard model for XML documents HTML DOM - standard model for HTML documents All nodes can be accessed through the tree. Their contents can be modified or deleted, and new elements can be created. Properties and methods define the programming interface to the DOM. 4.3 The XML DOM The XML DOM defines a standard way for accessing and manipulating XML documents. It views an XML document as a tree-structure called a node-tree. XML elements, attributes, and text as nodes The tree starts at the root node and branches out to the text nodes at the lowest level of the tree: 4.4

The XML DOM (cont.) The XML DOM is a W3C Recommendation XML is provides a standard programming interface for XML documents that is platformand language-independent In other words: The XML DOM is a standard for how to get, change, add, or delete XML elements. 4.5 The XML DOM Nodes In XML DOM, everything in an XML document 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 A common error in XML DOM processing is to expect an element node to contain text. However, the text of an element node is always stored in a text node E.g.: for <year>2005</year>, the element node <year>, holds a text node with the value "2005". In other words, "2005" is not the value of the <year> element! 4.6

DOM Example 1 4.7 DOM Example 2 <?xml version="1.0" encoding="iso-8859-1"?> <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> 4.8

DOM Example 2 (cont.) <book category="web"> <title lang="en">xquery Kick Start</title> <author>james McGovern</author> <author>per Bothner</author> <author>kurt Cagle</author> <author>james Linn</author> <author>vaidyanathan Nagarajan</author> <year>2003</year> <price>49.99</price> </book> <book category="web" cover="paperback"> <title lang="en">learning XML</title> <author>erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore> 4.9 Relationship between Nodes The nodes in the node tree have a hierarchical relationship to each other. The terms parent, child, and sibling are used to describe the relationships. Parent nodes have children. Children on the same level are called siblings (brothers or sisters). 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 4.10

Parsing the XML DOM Most browsers have a build-in XML parser that can be used to read and manipulate XML. The parser reads XML into memory and converts it into an XML DOM object that can be accesses with JavaScript. Microsoft's XML parser is built into IE 5 and higher. There are some differences between Microsoft's XML parser and the parsers used in other browsers. Microsoft parser supports loading of both XML files and XML strings (text), while other browsers use separate parsers. However, all parsers contain functions to traverse XML trees, access, insert, and delete nodes. However, before an XML document can be accessed and manipulated, it must be loaded into an XML DOM object. In this tutorial we will show you how to create scripts that will work in both IE and other browsers. 4.11 Loading XML with Microsoft's XML Parser The following JavaScript fragment loads an XML document ("books.xml") into the parser: xmldoc=new ActiveXObject("Microsoft.XMLDOM"); xmldoc.async="false"; xmldoc.load("books.xml"); first line creates an empty Microsoft XML document object. second line turns off asynchronized loading, to make sure that the parser will not continue execution of the script before the document is fully loaded. third line tells the parser to load an XML document called "books.xml". The following JavaScript fragment loads a string called txt into the parser: xmldoc=new ActiveXObject("Microsoft.XMLDOM"); xmldoc.async="false"; xmldoc.loadxml(txt); 4.12

XML Parser in Firefox and Other Browsers The following JavaScript fragment loads an XML document ("books.xml") into the parser: xmldoc=document.implementation.createdocument("","",null); xmldoc.async="false"; xmldoc.load("books.xml"); first line creates an empty XML document object. second line turns off asynchronized loading, third line tells the parser to load an XML document called "books.xml". The following JavaScript fragment loads a string called txt into the parser: parser=new DOMParser(); xmldoc=parser.parsefromstring(txt,"text/xml"); 4.13 Parsing an XML File - A Cross browser Example 4.14

Error: Access Across Domains For security reasons, modern browsers does not allow access across domains. Thus both the web page and the XML file it tries to load, must be located on the same server. Otherwise the xmldoc.load() method, will generate the error "Access is denied". 4.15 Parsing an XML String - A Cross browser Example 4.16

XML DOM Load Function The code for loading XML documents can be stored in a function can be stored in the <head> section of an HTML page, and called from a script in the page can be stored in an external file to make sure the same code is used in all pages, 4.17 XML DOM Load Function Using an external load function stored in a file called "loadxmldoc.js 4.18

Node Properties Each node is an object with the following important properties: nodename firstchild nodevalue lastchild nodetype previoussibling parentnode nextsibling childnodes attributes Properties are often referred to as something that is (i.e. nodename is "book"), Examples 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 The documentelement property of the XML document is the root node 4.19 Node Properties (cont.) The nodename property specifies the name of a node. 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 The nodevalue property specifies the value of a node. nodevalue for element nodes is undefined nodevalue for text nodes is the text itself nodevalue for attribute nodes is the attribute value Example1: Get the Value of an Element txt=x.nodevalue; Example 2: Change the Value of an Element x.nodevalue="easy Cooking"; 4.20

Node Properties (cont.) The nodetype property specifies the type of node. nodetype is read only. A node may be a reference to an element, its attributes, or text from the document The most important node types are: Node type NodeType Element 1 Attribute 2 Text 3 Comment 8 Document 9 4.21 Node Methods Methods are often referred to as something that is done (i.e. delete "book") Common methods are getelementsbytagname method allows accessing an element with a specified tag name setattribute method changes the attribute clonenode method - duplicates a node getnodename method - returns the node s name getnodetype method - returns the node's type getnodevalue method - returns the node's value getparentnode method - returns the node's parent's name haschildnodes method - true if has child nodes insertbefore method - inserts a node before a specified child node removechild method - removes the child node replacechild method - replaces one child with another setnodevalue method - sets node's value appendchild method - adds a child node to an existing node removechild method removes a child node removeattribute method removes an attribute 4.22

Accessing Nodes A node can be accessed by the getelementsbytagname() method Syntax node.getelementsbytagname("tagname"); returns a node list which is an array of nodes Examples x.getelementsbytagname("title"); returns all title elements under the x element x.getelementsbytagname("title"); returns all title elements in the document elements in the node list can be accessed by index number (starting from 0), e.g. xmldoc=loadxmldoc("books.xml"); x=xmldoc.getelementsbytagname("title"); y=x[2]; // to access the third title element 4.23 Example Loop through all title elements xmldoc=loadxmldoc("books.xml"); x=xmldoc.getelementsbytagname("title"); for (i=0;i<x.length;i++) { document.write(x[i].childnodes[0].nodevalue); document.write("<br />"); } 4.24

Navigating DOM Nodes Accessing nodes in the node tree via the relationship between nodes, is often called "navigating nodes". In the XML DOM, node relationships are defined as properties to the nodes: parentnode childnodes firstchild lastchild nextsibling previoussibling 4.25 Example 1 The following code loops through the child nodes, that are also element nodes, of the root node 4.26

Example 2 Traversing the Node Tree 4.27 Example 3 Ignoring Empty Text Between Elements 4.28

Change the Value of an Attribute In the DOM, attributes are nodes. Unlike element nodes, attribute nodes have text values. The way to change the value of an attribute, is to change its text value. This can be done using the setattribute() method or using the nodevalue property of the attribute node. Example 1 Using setattribute() xmldoc=loadxmldoc("books.xml"); x=xmldoc.getelementsbytagname('book'); x[0].setattribute("category","food"); 4.29 Change the Value of an Attribute (cont.) Example 2 - Using nodevalue xmldoc=loadxmldoc("books.xml"); x=xmldoc.getelementsbytagname("book")[0] y=x.getattributenode("category"); y.nodevalue="food"; 4.30

Creating XML Using DOM Methods <script type="text/javascript"> var comment, stud1, stud2, studs function mkrecord(){ comment = document.createcomment( My Tullab records'); studs = document.createelement('tullab'); stud1 = createstudent("g. Al-Good", 4.0); stud2 = createstudent("p. Al-Probation", 1.7); studs.appendchild(comment); studs.appendchild(stud1); studs.appendchild(stud2); showobject(studs); } function createstudent(name, gpa){ var result = document.createelement('talib'); nm = document.createelement('name'); GPA = document.createelement('gpa'); nm.appendchild(document.createtextnode(name)); GPA.appendChild(document.createTextNode(gpa)); result.appendchild(nm); result.appendchild(gpa); return result; } </script> 4.31 Q & A? 4.32

References Some useful links with examples and other resources: W3School DOM Tutorial http://www.w3schools.com/dom/default.asp MSXML 4.0 SDK 4.33