44
Working with the XML Document Object Model ©NIIT eXtensible Markup Language/Lesson 7/Slide 1 of 43 Objectives In this lesson, you will learn to: Identify the need for XML Document Object Model (DOM) Use the following XML DOM objects to access different parts of an XML document: DOMDocument IXMLDOMNode IXMLDOMNodeList IXMLDOMParseError 

XML - Lesson 7

Embed Size (px)

Citation preview

Page 1: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 1/44

Page 2: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 2/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 2 of 43

The XML Document Object Model

The Document Object Model  (DOM) defines the

logical structure of documents and the way a

document is accessed and manipulated. It provides anApplication Programming Interface (API) for XML and

HTML documents.

XML DOM views an XML document as being

composed of objects. Each object has properties andbehavior that can be manipulated using the methods

provided by a DOM interface.

Page 3: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 3/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 3 of 43

The XML Document Object Model (Contd.)

Using XML DOM, programmers can create and build

XML documents, navigate the structure of documents,

and add, modify, or delete elements and their content.

The objects and methods provided by XML DOM can

be used with any programming language, such as

Java, C++, and Visual Basic. These objects can also

be used with scripting languages, such as VBScript

and JavaScript.

To be able to use the features of XML DOM, you need

to use a DOM-enabled parser.

Page 4: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 4/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 4 of 43

The XML Document Object Model (Contd.)

A DOM-enabled parser reads an XML document and

parses it to ensure that it is a valid document. Then, it

creates an in-memory representation of the XMLdocument by organizing its contents in a logical tree-

structure. The tree-structure is made up of nodes.

MSXML is an example of a DOM-enabled XML parser.

Page 5: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 5/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 5 of 43

Implementation of DOM in the MSXML Parser When the MSXML parser loads an XML document, it

reads the document and creates a tree structure that

represents the various components of the XMLdocument.

Page 6: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 6/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 6 of 43

Implementation of DOM in the MSXML Parser 

(Contd.)

The following diagram depicts how DOM allows

applications to access information stored in XMLdocuments:

XML

Document

MSXML Library

Parsed

Document

Parser DOM Tree

RootChild

text

Child

text

Application

Page 7: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 7/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 7 of 43

Tree structure of a Document

The basic building block of the tree structure is the

node.

Nodes are generic containers that hold information

about the elements, attributes, content, comments,

and processing instructions that are stored in an XML

document.

The XML document can be viewed as a single node

that contains all the other nodes.

Page 8: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 8/44

Page 9: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 9/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 9 of 43

XML DOM Objects and Methods

The main objects and methods provided by XML DOMthat enable you to traverse, read, and manipulate the

structure and content of an XML document are listedbelow:

The DOMDocument object

The IXMLDOMNode object

The IXMLDOMNodeList object

The IXMLDOMParseError object

Page 10: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 10/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 10 of 43

The DOMDocument object

The DOMDocument object is the top-level object in

XML DOM. It implements all the basic DOM methods

required to work with an XML document. It also hasmethods, which support XSLT.

This object is associated with various methods that

help you navigate, query, and modify the content and

the structure of an XML document.

Page 11: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 11/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 11 of 43

The DOMDocument object (Contd.)

Some of the most commonly used methods of the

DOMDocument object are:

createElement(elementname): Creates an element

node.

createAttribute(attributename): Creates an attribute

node.

CreateNode(type, name, namespace-URI): Creates

a node of the specified type. Also supports

namespace URI.

getElementsByTagName(elementname): Returns a

collection of element nodes that have the specified

element name.

Page 12: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 12/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 12 of 43

The DOMDocument object (Contd.)

load(XMLdocument): Loads the specified XMLdocument.

appendChild(child node): Appends the specifiedchild node to the document.

save(destination): Saves the XML documentrepresented by the DOMDocument object at thespecified destination.

Some of the commonly used properties of theDOMDocument object are:

async: Allows you to specify whether the XMLdocument can be loaded asynchronously.

childNodes: Returns a list of child nodes.

firstChild: Returns the first child in the document.

Page 13: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 13/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 13 of 43

The DOMDocument object (Contd.)

readyState: Returns the information about the

state of the XML document (loading, loaded,

interactive, complete).

parseError: Returns an object of 

IXMLDOMParseError, which can be used to

retrieve error information.

xml: Returns the XML representation of a

node.

validateOnParse: Specifies whether the parser should check for validity of an XML document.

Page 14: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 14/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 14 of 43

Creating a DomDocument object

The following is the code segment written using

JavaScript to create an instance of the DOMDocument 

object:

var myxmldoc = new

ActiveXObject("Msxml2.DOMDocument.4.0"); 

A reference to the newly created object is stored inthe variable myxmldoc, which can be used to load

and manipulate XML documents.

Page 15: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 15/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 15 of 43

Loading an XML file

To load an XML file by using a URL, you can write the

following code in JavaScript :

myxmldoc.load("http://www.sb.com/

employee.xml"); 

The load() method can also be used to load a file

from the local host by providing the path, as shown inthe following example: 

myxmldoc.load("c:\examples\employees.xml

");

Page 16: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 16/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 16 of 43

Loading an XML file (Contd.)

To load an XML document synchronously, you can

write the following code in JavaScript:

var myxmldoc = new

ActiveXObject("Msxml2.DOMDocument.4.0");

myxmldoc.async=false;

myxmldoc.load("employee.xml");

Page 17: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 17/44

Page 18: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 18/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 18 of 43

Using the readyState Property (Contd.)

INTERACTIVE (3): This state indicates that the object

model is available with partially retrieved data set and

is in the read-only mode.

COMPLETED (4): This state indicates that the

loading process is complete.

Page 19: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 19/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 19 of 43

Creating a New Node

You can create a new node by using thecreateNode() method.

The following is the code segment to create a rootelement by using the createNode() method:

var myxmldoc = newActiveXObject("Msxml2.DOMDocument.4.0");

if (myxmldoc.childnodes.length==0){

myroot=myxmldoc.createNode(1,"ORDERDETAI

LS"," ");myxmldoc.appendChild(myroot);

myxmldoc.save(myxmldoc.XMLDocument);

}

Page 20: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 20/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 20 of 43

Creating New Elements

You can create new elements for the document by

using the createElement() method of the

myxmldoc DOMDocument object. ThecreateElement() method takes one parameter, the

name of the element that is to be created.

The following example creates a new element node

called “salary”:

var xmlelement;

xmlelement=myxmldoc.createElement

("salary");

Page 21: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 21/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 21 of 43

Accessing Elements From an XML File

To access the elements from an XML document, youcan begin at the root element and navigate through the

document tree. You can also query for a specific nodeor nodes.

To reach a particular node or nodes within thedocument tree structure, you can use thegetElementsByTagName() method.

Page 22: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 22/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 22 of 43

The IXMLDOMNode object

The IXMLDOMNode object represents a node in theXML document tree structure.

The node could be an element that contains childelements.

The IXMLDOMNode object provides methods to workwith child elements.

Some commonly used methods of the IXMLDOMNode object are:

appendChild(newchild): Appends a new child tothe node.

insertBefore(newNode, refNode): Inserts a newnode before the specified node.

removeChild(nodeName): Removes the specifiednode.

Page 23: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 23/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 23 of 43

The IXMLDOMNode object (Contd.)

replaceChild(newNode, oldNode): Replaces theoldNode with newNode.

hasChildNodes(): Specifies whether the node haschild nodes.

cloneNode(boolean): Creates a clone of the noderepresented by the IXMLDOMNode object.

Some of the commonly used properties of theIXMLDOMNode object are:

nodeName: Returns the name of the node.

nodeType: Specifies the type of the node.

nodeValue: Returns the text contained in the node.

childNodes: Returns the child nodes of the node.

Page 24: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 24/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 24 of 43

The IXMLDOMNode object (Contd.)

firstChild: Returns the first child of the node.

lastChild: Returns the last child of the node.

text: Returns the text contained in the node.

xml: Returns the XML code for the node.

 

Page 25: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 25/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 25 of 43

Accessing Text Values of Elements

The text enclosed within the tags in an XML file is

used as a node value in DOM. A node value can be

the value of an attribute or the text within an element.

You can display the text within the elements or 

attributes by using the text property, as shown

below:

alert(myelement.text);

You can also set the value of an element by using this

property, as shown in the example given below:

myelement.text=“Barbie Doll”; 

Page 26: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 26/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 26 of 43

Appending a New Child Node

The following code segment creates a new element

by using the createElement() method of the

DOMDocument object. Then, it appends the newlycreated node to the last child of myelement by using

the appendchild() method:

var myelement=myxmldoc.documentElement;

var newElement=myxmldoc.createElement

("lastchild");

myelement.appendChild(newElement);

Page 27: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 27/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 27 of 43

Inserting a Node Before Two Existing Nodes

The following code segment creates a new element

called department and replaces and existing node

with the new element:

var newElement= myxmldoc.createElement

("department");

varoldElement=myxmldoc.documentElement.chil

dNodes.item(0).firstChild;

myxmldoc.documentElement.childNodes.item(1).insertBefore(newElement,

oldElement); 

Page 28: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 28/44

W ki i h h XML D Obj M d l

Page 29: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 29/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 29 of 43

Replacing a Node (Contd.)

In the following example, the second element in the

document is replaced with the new element named

department:

Var newElement=

myxmldoc.createElement("department");

VaroldElement=myxmldoc.documentElement.chil

dNodes.item(0).firstChild;

myxmldoc.documentElement.childNodes.item(1

).replaceChild(newElement, oldElement);

W ki ith th XML D t Obj t M d l

Page 30: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 30/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 30 of 43

The IXMLDOMNodeList object

The IXMLDOMNodeList object enables you to iterate

through a collection of nodes.

Some methods of the IXMLDOMNodeList object are:

item(number):Returns the node at the position

indicated by the specified number.

nextNode(): Returns the next node in the list.

To obtain the length of the node list, use the length 

property. The length property can also be used to

traverse through the list of child nodes.

W ki ith th XML D t Obj t M d l

Page 31: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 31/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 31 of 43

The IXMLDOMNodeList object (Contd.)

The following code traverses through the child nodes

of myelement:

var

myelement=myxmlDoc.getElementsByTagName(

"emp");

for(i=0;i< myelement.length –1;i++)alert(myelement.item(i).xml); 

W ki ith th XML D t Obj t M d l

Page 32: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 32/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 32 of 43

The IXMLDOMParseError Object

The IXMLDOMParseError object returns information

about the most recent parse error.

The IXMLDOMParseError object provides properties

to retrieve information, such as the error code, the

error text, and the line that caused the error.

Working with the XML Document Object Model

Page 33: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 33/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 33 of 43

The IXMLDOMParseError Object (Contd.)

The properties of the IXMLDOMParseError object are:

errorCode: Returns the error code.

reason: Returns a string explaining the reasonfor the error.

line: Returns the line number where the error occurred.

linePos: Returns the position in the line wherethe error occurred.

srcText: Returns a string containing the linethat caused the error.

Working with the XML Document Object Model

Page 34: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 34/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 34 of 43

Using the IXMLDOMParseError object

You can use the IXMLDOMParseError object todisplay information about the errors that areencountered while parsing an XML document.

Consider the following example:

var prodxml = newActiveXObject("Msxml2.DOMDocument.4.0");

prodxml.async = false;

prodxml.load("product.xml");

if (prodxml.parseError.errorCode != 0)

{ alert("A parse error occurred.");}else

{alert(prodxml.documentElement.xml);} 

Working with the XML Document Object Model

Page 35: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 35/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 35 of 43

Problem Statement 7.D.1

The product details of CyberShoppe are stored in an

XML document. The structure of the XML document is

defined in a DTD. The data held in the XML documentmust be validated against the rules specified for the

data store.

Working with the XML Document Object Model

Page 36: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 36/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 36 of 43

Task List

Identify a mechanism to access an XML document

programmatically.

Identify the objects required to access the XML

document.

Write the code to access the document.

Execute the code.

Working with the XML Document Object Model

Page 37: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 37/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 37 of 43

Task 1: Identify a mechanism to access an XML

document programmatically.

Result:

The contents of an XML document can be accessed

and manipulated from any application by using XML

DOM. Therefore, to access an XML document

programmatically, the best solution is to use XML

DOM. Applications developed using VBScript,JavaScript, C/C++, or Visual Basic can use XML DOM

objects.

Working with the XML Document Object Model

Page 38: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 38/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 38 of 43

Task 2: Identify the objects required to access

the XML document.

Result:

You can validate the contents of an XML document

against a DTD by using the following XML DOM

objects:

 DOMDocument 

IXMLDOMParseError 

Working with the XML Document Object Model

Page 39: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 39/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 39 of 43

Task 3: Write the code to access the document

To write a script that validates an XML document

against the DTD, you need to follow the steps given

below: Create the user interface for accepting the XML file

name.

Write the script to load the named XML document.Write the script to validate the XML document

against the DTD.

Working with the XML Document Object Model

Page 40: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 40/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 40 of 43

Task 4: Execute the script

Working with the XML Document Object Model

Page 41: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 41/44

Working with the XML Document Object Model

©NIIT eXtensible Markup Language/Lesson 7/Slide 41 of 43

Just a Minute…

The details about products sold at CyberShoppe are

stored in an XML document called product.xml. Write

the code to display the price of all products by usingDOM objects.

Working with the XML Document Object Model

Page 42: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 42/44

o g e ocu e Objec ode

©NIIT eXtensible Markup Language/Lesson 7/Slide 42 of 43

Summary

In this lesson, you learned that:

DOM is an application programming interface that

enables an application to access the contents of anXML document.

DOM objects enable you to access and manipulate

XML documents.

When the MSXML parser loads an XML document, it

creates a tree structure that represents the various

components of the XML document.

Working with the XML Document Object Model

Page 43: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 43/44

g j

©NIIT eXtensible Markup Language/Lesson 7/Slide 43 of 43

Summary (Contd.)

The basic building block of the tree structure is a

node. A node is a container that holds information

about the elements, attributes, and content stored inan XML document.

Some XML DOM objects that are used to manipulate

data in a document are:

DOMDocument 

IXMLDOMNode 

IXMLDOMNodeList 

IXMLDOMParseError 

Working with the XML Document Object Model

Page 44: XML - Lesson 7

8/14/2019 XML - Lesson 7

http://slidepdf.com/reader/full/xml-lesson-7 44/44

g j

©NIIT eXtensible Markup Language/Lesson 7/Slide 44 of 43

Summary (Contd.)

The DOMDocument object is the top-level object in

XML DOM. This object provides various properties

and methods that help you to navigate, query, andmodify the content and structure of XML documents.

The IXMLDOMNode object represents a node in the

XML document structure. This object provides

methods to work with child elements.

The IXMLDOMNodeList object enables you to iterate

through a collection of nodes.

The IXMLDOMParseError object returns informationabout the most recent error.