xml part 6

33
Document Object Model (DOM) CCSA 222: XML and Web Services Part 7 1

Upload: noha-aw

Post on 09-Jan-2017

38 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Xml part 6

Document Object Model (DOM)

CCSA 222: XML and Web ServicesPart 7 1

Page 2: Xml part 6

Introduction

2

Page 3: Xml part 6

Introduction

3

Page 4: Xml part 6

Introduction

4

Page 5: Xml part 6

1-Javascript

5

Page 6: Xml part 6

Javascript

• Javascript (JS) is a dynamic programming language used for web-design.

• HTML is responsible for formatting a webpage. Then, JS code is embedded

in the HTML code to make it dynamic.

• JS is processed by the Browser. Therefore, it is considered a client-side

scripting language.

• Server-side scripting languages, such as PHP and ASP, are processed by the

server.

Page 7: Xml part 6

Javascript

• Javascript code must be inside the <script> and </script> tags.

• You can place JS code inside the <head> or <body> of the HTML code.

• Using Javascript to output:• Writing into an alert box, using window.alert().• Writing into the HTML output, using document.write().• Writing into an HTML element,

using document.getElementByID(“…”).innerHTML.

Page 8: Xml part 6

window.alert()• <!DOCTYPE html>

<html> <body>

<h1>My First Web Page</h1> <p>My first paragraph.</p>

<script> window.alert(“hi!”); </script>

</body></html>

Page 9: Xml part 6
Page 10: Xml part 6

document.write()• <!DOCTYPE html>

<html> <body>

<h1>My First Web Page</h1> <p>My first paragraph.</p>

<script> document.write(“hi!”); </script>

</body></html>

Page 11: Xml part 6
Page 12: Xml part 6

document.getElementByID(“…”).innerHTML• <!DOCTYPE html>

<html> <body>

<h1>My First Web Page</h1> <p id=“demo”>My first paragraph.</p>

<script> document.getElementByID(“demo”).innerHTML=“hi!”; </script>

</body></html>

Page 13: Xml part 6

JavaScript Variables• JavaScript variables are containers for storing data values.• Example:• var x = 5;

var y = 6;var z = x + y; • From the example above, you can expect:

x stores the value 5y stores the value 6z stores the value 11

Page 14: Xml part 6

Declaring (Creating) JavaScript Variables

• You declare a JavaScript variable with the var keyword.• Example: var carName;

• You can also assign a value to the variable when you declare it:• var carName = "Volvo";

Page 15: Xml part 6

Functions and Event Handlers

• The most common way to use JS and HTML is to place a function inside the JS, and call it from the HTML using an event handler.

• This way, the JS code does not get executed until a certain event occurs.• Example:

<html> <body> <script> function myFunction(){ ……….. } </script> <input type=“button” value=“click me” onclick=“myFunction()”/> </body></html>

Page 16: Xml part 6
Page 17: Xml part 6

Event HandlerEvent Description

onchange An HTML element has been changed

onclick The user clicks an HTML elementonmouseover The user moves the mouse over

an HTML elementonmouseout The user moves the mouse away

from an HTML elementonkeydown The user pushes a keyboard keyonload The browser has finished

loading the page

Page 18: Xml part 6

!DOCTYPE html><html> <body> <h1>My First Web Page</h1> <p id="demo" onclick="myFunction()">My first paragraph.</p> <script> function myFunction(){ document.getElementById("demo").innerHTML="hi!"; } </script> </body></html>

Page 19: Xml part 6

Using an External JS file

• <!DOCTYPE html><html> <body> <script src="myScript.js"></script> </body></html>

Page 20: Xml part 6

2-Document Object Model (DOM)

20

Page 21: Xml part 6

What is the DOM?• The 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."

21

Page 22: Xml part 6

The XML DOM• The XML DOM defines a standard way for accessing and

manipulating XML documents. • All XML elements can be accessed through the XML

DOM.• The XML DOM defines the objects, properties and

methods of all XML elements.• In other words: The XML DOM is a standard for how to

get, change, add, or delete XML elements.22

Page 23: Xml part 6

XML Example<?xml version="1.0" encoding="UTF-8"?><note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body></note>

23

Page 24: Xml part 6

Document Object Model (DOM)

• The XML DOM is an Object that represents the XML file as a node tree.

• This object allows programs and scripts to dynamically access and update

the content of an XML document.

• The XML DOM needs to be created first, then it can be used.

• When we create an XML DOM, we can use all the XML DOM properties

and methods to interact with the object.

Page 25: Xml part 6

The Node Tree• The DOM represents everything in an XML document as 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• Every comment is a comment node• Every processing instruction is a processing instruction node

Page 26: Xml part 6
Page 27: Xml part 6

Creating the DOM

27

Page 28: Xml part 6

Creating a DOM for the following XML document “books.xml”

<?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>

<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>

Page 29: Xml part 6

• You will create the DOM using a programming language such as javascript

• The javascript code will be inside an HTML file between the tags <script> ….. </script>

<html>

<head>

<script src="loadxmldoc.js">

</script>

</head>

<body>

<script>

var xmlDoc;

xmlDoc=loadXMLDoc("books.xml");

</script>

</body>

</html>

The steps of creating DOM with

JS:

1-Declare a variable xmlDoc

2- Call the function loadXMLDoc() and

give it the xml file “books.xml”

3- the function will return an xml

DOM for the books.xml file, and it

will be stored in the variable

xmlDoc

The steps of creating DOM with JS.

Page 30: Xml part 6

The variable xmlDoc will be the DOM which represents the xml document as a node tree

The XML DOM Node Tree

Page 31: Xml part 6

loadXMLDoc.js• We create an XML DOM by requesting the browser to create an XMLHttpRequest:

function loadXMLDoc(filename) { var xhttp; if (window.XMLHttpRequest) { xhttp = new XMLHttpRequest(); //code for IE7+ , Firefox , Chrome , Opera , Safari } else // code for IE5 and IE6 { xhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xhttp.open("GET", filename, false); xhttp.send(); return xhttp.responseXML;}

Page 32: Xml part 6

The XMLHttpRequest Object• The XMLHttpRequest object can be used to request data from a

web server. It is an object to handle the HTTP requests between the client and the server.

• All modern browsers have a built-in XMLHttpRequest object to request data from a server.

Page 33: Xml part 6

References• Carey, P. (2007). New perspectives on XML: Comprehensive (2nd

ed.). Australia: Thomson/Course Technology.

• XML DOM Tutorial. (n.d.). Retrieved November 7, 2015, from http://www.w3schools.com/xml/dom_intro.asp