SlideShare a Scribd company logo
1 of 55
DOM
DOCUMENT OBJECT MODEL
INTRODUCTION
 W3C standard recommendation
 For building a tree structure in memory for XML
documents
 DOM Application Programming Interface (API)
is a a programmatic library
 Allows manipulation of the contents of an XML
document.
 The DOM interfaces are platform and language
independent.
DOM STRUCTRE MODEL
 Based on OO(Object Oriented) concepts:
– methods (to access or change object’s state)
– interfaces (declaration of a set of methods)
– objects (encapsulation of data and methods)
LIMITATIONS
 Since DOM tree resides in the memory.
Hence larger the XML document the more
memory resource it will consume.
 As its resource needs are high, its
processing is slower as compared to other
XML parsers.
DOM LEVELS
 3 different levels:
– Core DOM - standard model for any structured
document
– XML DOM - standard model for XML
documents
– HTML DOM - standard model for HTML
documents
What is a XML Parser?
XML DOM PARSER
 The XML DOM parser converts XML into a
JavaScript accessible object (the XML DOM).
 Firstly XML document must be loaded into an
XML DOM object.
 An XML parser reads XML, and converts it
into an XML DOM object that can be accessed
with JavaScript.
 Most browsers have a built-in XML parser.
DOM BASED PARSERS
Parser Description
JAXP Sun Microsystem’s Java API for XML Parsing (JAXP)
XML4J IBM’s XML Parser for Java (XML4J)
msxml Microsoft’s XML parser (msxml) version 2.0 is built-into Internet
Explorer 5.5
4DOM 4DOM is a parser for the Python programming language.
XML::DOM XML::DOM is a Perl module to manipulate XML documents
using Perl.
Xerces Apache’s Xerces Java Parser
Core Interfaces: Node & its variants
Notes 3.2: Document Object Model 9
Node
Comment
DocumentFragment Attr
Text
Element
CDATASection
ProcessingInstruction
CharacterData
EntityDocumentType Notation
EntityReference
“Extended
interfaces”
Document
DOM NODE TYPES
NODE DESCRIPTION CHILDREN
Document entire document (the
root-node of the DOM
tree)
Element (max.
one),
ProcessingInstruc
tion, Comment,
DocumentType
DocumentFragment Represents a
"lightweight" Document
object, which can hold a
portion of a document
Element,
ProcessingInstruc
tion, Comment,
Text,
CDATASection,
EntityReference
DocumentType Provides an interface to
the entities defined for
the document
None
ProcessingInstruction Represents a processing
instruction
None
NODE TYPES(contd.)
NODE DESCRIPTION CHILDREN
EntityReference Represents an entity
reference
Element,
ProcessingInstruction,
Comment, Text,
CDATASection,
EntityReference
Element Represents an element Element, Text, Comment,
ProcessingInstruction,
CDATASection,
EntityReference
Attr Represents an attribute Text, EntityReference
Text Represents textual content
in an element or attribute
None
NODE TYPES(contd.)
NODE DESCRIPTION CHILDREN
CDATASection Represents a CDATA section in a
document (text that will NOT be
parsed by a parser)
None
Comment Represents a comment None
Entity Represents an entity Element,
ProcessingInstruction,
Comment, Text,
CDATASection,
EntityReference
Notation Represents a notation declared in
the DTD
None
NODE TYPE
 specifies the type of node.
 nodeType is read only property.
 The most important node types are:
Node type NodeType(Numeric value)
Element 1
Attribute 2
Text 3
Comment 8
Document 9
NODE RELATIONSHIP
 node relationships are defined as properties
to the nodes:
– parentNode
– childNodes
– firstChild
– lastChild
– nextSibling
– previousSibling
EXAMPLE
<?xml version = "1.0"?>
<message from = "Paul" to = "Tem">
<body>Hi, Tem!</body>
</message>
XML DOM NODE TREE
ROOT Element
message
Attribute: from Attribute: to
Element: body
Text: Hi,Tem!
DOM PROPERTIES
 Let x is a node object
 Some typical DOM properties of x:
– x.nodeName - the name of node x
– x.nodeValue - the value of node x
– x.parentNode - the parent node of node x
– x.childNodes - the child nodes of node x
– x.attributes - the attributes nodes of node x
XML DOM METHODS
METHOD DESCRIPTION
appendChild() Appends a child node.
cloneNode() Duplicates the node.
getAttributes() Returns the node’s attributes.
getChildNodes() Returns the node’s child nodes.
getNodeName() Returns the node’s name.
getNodeType() Returns the node’s type
getNodeValue() Returns the node’s value.
getParentNode() Returns the node’s parent.
hasChildNodes() Returns true if the node has child nodes.
removeChild() Removes a child node from the node.
replaceChild() Replaces a child node with another node.
setNodeValue() Sets the node’s value.
insertBefore() Appends a child node in front of a child node.
Let us consider a XML document for bookstore
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
</book>
<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
XML DOM Load Functions
The function is stored in an external file called "loadxmldoc.js",
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else
{
xhttp=new ActiveXObject("Microsoft .XMLHTTP");
}
xhttp.open("GET", dname , false);
xhttp.send();
return xhttp.responseXML;
}
Contd…
 Method: open(method, url, async)
 Specifies the type of request, the URL, and
if the request should be handled
asynchronously or not.
method: the type of request: GET or POST
url: the location of the file on the server
async: true (asynchronous) or
false (synchronous)
XML loadXMLString() Function
function loadXMLString(txt)
{
if (window.DOMParser)
{
parser=new DOMParser();
xmlDoc=parser.parseFromString(txt, "text/xml");
}
else // Internet Explorer
{
xmlDoc=new
ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.loadXML(txt);
}
return xmlDoc;
}
Example
<html><head><script src="loadxmlstring.js"></script></head>
<body><script>
text="<bookstore><book>";
text=text+"<title>Everyday Italian</title>";
text=text+"<author>Giada De Laurentiis</author>";
text=text+"<year>2005</year>";
text=text+"</book></bookstore>";
xmlDoc=loadXMLString(text);
document.write(xmlDoc.getElementsByTagName("title")[0].childNo
des[0].nodeValue);
document.write("<br>");
document.write(xmlDoc.getElementsByTagName("author")[0].child
Nodes[0].nodeValue);
document.write("<br>");
document.write(xmlDoc.getElementsByTagName("year")[0].childN
odes[0].nodeValue);
</script></body>
</html>
ACCESSING NODES
 There are 3 ways:
1. By using the getElementsByTagName() method
2. By looping through (traversing) the nodes tree.
3. By navigating the node tree, using the node
relationships.
getElementsByTagName()
method
 Syntax:
node.getElementsByTagName("tagname”);
 returns an array of all elements with the specified tagname that
are descendents of element “node”.
 The elements in the node list returned can be accessed using
index number that starts with 0.
 To get all elements with specific tagname in the XML document
use:
– xmlDoc.getElementsByTagName(“tagname”);
 where xmlDoc is the document itself (document node).
Example
<html>
<head><script src="loadxmldoc.js"></script></head>
<body><script>
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>");
}
</script></body>
</html>
Traversing the Tree
 Load xml document.
 Get the child nodes of the root element.
 For each child node, check the node type of
the node.
 If the node type is matches the node type of
the desired node process that node.
EXAMPLE
<html>
<head><script src="loadxmldoc.js"></script></head>
<body><script>
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.documentElement.childNodes;
for (i=0;i<x.length;i++){
if (x[i].nodeType==1)
{//Process only element nodes (type 1)
} }
</script></body>
</html>
Using the Node Relationships.
 Load xml document.
 Get the child nodes of the first element
 Set the "y" variable to be the first child node of the first
element.
 For each child node (starting with the first child node
"y"). Check the node type.
 If the node type is desired node type process that node.
 Set the "y" variable to be the next sibling node, and run
through the loop again
Example
<html>
<head><script src="loadxmldoc.js"></script></head>
<body><script>
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book")[0].childNodes;
y=xmlDoc.getElementsByTagName("book")[0].firstChild;
for (i=0;i<x.length;i++){
if (y.nodeType==1)
{ document.write(y.nodeName + "<br>");}
y=y.nextSibling;
}
</script></body>
</html>
CHANGE NODE VALUES
 The nodeValue property is used to change a
node value.
 The setAttribute() method is used to change
an attribute value.
Changing value of Element node
code changes the text node value of the first <title> element i.e.
Everyday Italian to Easy Cooking
<html>
<head><script src="loadxmldoc.js"> </script></head>
<body>
<script>
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue="Easy Cooking";
</script>
</body>
</html>
Change an Attribute Using
setAttribute()
Change the attribute “category” to “food” of element book
<html>
<head><script src="loadxmldoc.js"> </script></head>
<body><script>
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('book');
x[0].setAttribute("category","food");
document.write(x[0].getAttribute("category"));
</script></body>
</html>
Change an Attribute Using nodeValue
<html>
<head><script src="loadxmldoc.js"> </script></head>
<body><script>
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book")[0]
y=x.getAttributeNode("category");
y.nodeValue="food";
</script></body>
</html>
Remove Nodes
Remove an Element Node
<html>
<head><script src="loadxmldoc.js"> </script></head>
<body><script>
xmlDoc=loadXMLDoc("books.xml");
document.write(xmlDoc.getElementsByTagName('book').length);
document.write("<br>");
y=xmlDoc.getElementsByTagName("book")[0];
xmlDoc.documentElement.removeChild(y);
document.write(xmlDoc.getElementsByTagName('book').length);
</script></body>
</html>
Remove the Current Node
Navigate to the node to be removed then use parentNode property and
removeChild ()
<html>
<head><script src="loadxmldoc.js"> </script></head>
<body><script>
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book")[0];
x.parentNode.removeChild(x);
</script></body>
</html>
Remove the Text Node
<html>
<head><script src="loadxmldoc.js"> </script></head>
<body><script>
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0];
y=x.childNodes[0];
x.removeChild(y);
</script></body>
</html>
Remove the Text of Text Node
<html>
<head><script src="loadxmldoc.js"> </script></head>
<body><script>
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0];
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
document.write("Value: " + x.nodeValue);
document.write("<br>");
x.nodeValue="";
document.write("Value: " + x.nodeValue);
</script></body>
</html>
Remove Attribute
<html>
<head><script src="loadxmldoc.js"> </script></head>
<body><script>
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('book');
document.write(x[0].getAttribute('category'));
document.write("<br>");
x[0].removeAttribute('category');
document.write(x[0].getAttribute('category'));
</script></body>
</html>
CREATING NODES
Create a New Element Node
<html>
<head><script src="loadxmldoc.js"> </script></head>
<body><script>
xmlDoc=loadXMLDoc("books.xml");
newel=xmlDoc.createElement("edition");
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);
</script></body>
</html>
Create a New Attribute Node
<html>
<head><script src="loadxmldoc.js"> </script></head>
<body><script>
xmlDoc=loadXMLDoc("books.xml");
newatt=xmlDoc.createAttribute("edition");
newatt.nodeValue="first";
x=xmlDoc.getElementsByTagName("title");
x[0].setAttributeNode(newatt);
</script>
</body>
</html>
Create an Attribute Using setAttribute()
<html>
<head><script src="loadxmldoc.js"> </script></head>
<body><script>
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title");
x[0].setAttribute("edition","first");
</script></body>
</html>
Create a CDATA SECTION Node
<html>
<head><script src="loadxmldoc.js"></script></head>
<body><script>
xmlDoc=loadXMLDoc("books.xml");
newCDATA=xmlDoc.createCDATASection("Special Offer &
Book Sale");
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newCDATA);
document.write(x.lastChild.nodeValue);
</script></body>
</html>
Create a TEXT Section Node
<html>
<head><script src="loadxmldoc.js"> </script></head>
<body><script>
xmlDoc=loadXMLDoc("books.xml");
newel=xmlDoc.createElement("edition");
newtext=xmlDoc.createTextNode("first");
newel.appendChild(newtext);
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);
</script></body>
</html>
APPENDING NODES
After Last Child Node
<html>
<head><script src="loadxmldoc.js"> </script></head>
<body><script>
xmlDoc=loadXMLDoc("books.xml");
newel=xmlDoc.createElement("edition");
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);
</script></body>
</html>
Before a Specified Child Node
<html>
<head><script src="loadxmldoc.js"> </script></head>
<body><script>
xmlDoc=loadXMLDoc("books.xml");
newNode=xmlDoc.createElement("book");
x=xmlDoc.documentElement;
y=xmlDoc.getElementsByTagName("book")[3];
x.insertBefore(newNode , y);
</script></body>
</html>
Add Text to a Text Node - insertData()
The insertData() method has two parameters:
offset - Where to begin inserting characters (starts at zero)
string - The string to insert
<html>
<head><script src="loadxmldoc.js"> </script></head>
<body><script>
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.insertData(0,"Easy ");
</script></body>
</html>
REPLACE NODES
Replace an Element Node
<html>
<head><script src="loadxmldoc.js"> </script></head>
<body><script>
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.documentElement;
newNode=xmlDoc.createElement("book");
newTitle=xmlDoc.createElement("title");
newText=xmlDoc.createTextNode("A Notebook");
newTitle.appendChild(newText);
newNode.appendChild(newTitle);
y=xmlDoc.getElementsByTagName("book")[0]
x.replaceChild(newNode,y);
</script></body>
</html>
Replace Data In a Text Node
<html>
<head><script src="loadxmldoc.js"> </script></head>
<body><script>
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue="Easy Italian";
</script></body>
</html>
Cloning the nodes
Example
The cloneNode() method has a parameter (true or false). This parameter
indicates if the cloned node should include all attributes and child
nodes of the original node.
<html>
<head><script src="loadxmldoc.js"> </script></head>
<body><script>
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('book')[0];
cloneNode=x.cloneNode(true);
xmlDoc.documentElement.appendChild(cloneNode);
</script></body>
</html>

More Related Content

What's hot

JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An IntroductionManvendra Singh
 
Locators in selenium - BNT 09
Locators in selenium - BNT 09Locators in selenium - BNT 09
Locators in selenium - BNT 09weekendtesting
 
Lesson 1: Introduction to HTML
Lesson 1: Introduction to HTMLLesson 1: Introduction to HTML
Lesson 1: Introduction to HTMLOlivia Moran
 
CSS Day: CSS Grid Layout
CSS Day: CSS Grid Layout CSS Day: CSS Grid Layout
CSS Day: CSS Grid Layout Rachel Andrew
 
HTML5: features with examples
HTML5: features with examplesHTML5: features with examples
HTML5: features with examplesAlfredo Torre
 
An Introduction to the DOM
An Introduction to the DOMAn Introduction to the DOM
An Introduction to the DOMMindy McAdams
 
jQuery from the very beginning
jQuery from the very beginningjQuery from the very beginning
jQuery from the very beginningAnis Ahmad
 
Intro to HTML and CSS basics
Intro to HTML and CSS basicsIntro to HTML and CSS basics
Intro to HTML and CSS basicsEliran Eliassy
 
HTML and CSS crash course!
HTML and CSS crash course!HTML and CSS crash course!
HTML and CSS crash course!Ana Cidre
 
Spring Boot and REST API
Spring Boot and REST APISpring Boot and REST API
Spring Boot and REST API07.pallav
 

What's hot (20)

Spring Boot
Spring BootSpring Boot
Spring Boot
 
Basic-CSS-tutorial
Basic-CSS-tutorialBasic-CSS-tutorial
Basic-CSS-tutorial
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An Introduction
 
Locators in selenium - BNT 09
Locators in selenium - BNT 09Locators in selenium - BNT 09
Locators in selenium - BNT 09
 
Angular Data Binding
Angular Data BindingAngular Data Binding
Angular Data Binding
 
Lesson 1: Introduction to HTML
Lesson 1: Introduction to HTMLLesson 1: Introduction to HTML
Lesson 1: Introduction to HTML
 
Laravel Tutorial PPT
Laravel Tutorial PPTLaravel Tutorial PPT
Laravel Tutorial PPT
 
Clean code
Clean codeClean code
Clean code
 
CSS Day: CSS Grid Layout
CSS Day: CSS Grid Layout CSS Day: CSS Grid Layout
CSS Day: CSS Grid Layout
 
HTML Fundamentals
HTML FundamentalsHTML Fundamentals
HTML Fundamentals
 
Introduction to HTML and CSS
Introduction to HTML and CSSIntroduction to HTML and CSS
Introduction to HTML and CSS
 
jQuery
jQueryjQuery
jQuery
 
HTML5: features with examples
HTML5: features with examplesHTML5: features with examples
HTML5: features with examples
 
An Introduction to the DOM
An Introduction to the DOMAn Introduction to the DOM
An Introduction to the DOM
 
jQuery from the very beginning
jQuery from the very beginningjQuery from the very beginning
jQuery from the very beginning
 
html5.ppt
html5.ppthtml5.ppt
html5.ppt
 
HTML5 & CSS3
HTML5 & CSS3 HTML5 & CSS3
HTML5 & CSS3
 
Intro to HTML and CSS basics
Intro to HTML and CSS basicsIntro to HTML and CSS basics
Intro to HTML and CSS basics
 
HTML and CSS crash course!
HTML and CSS crash course!HTML and CSS crash course!
HTML and CSS crash course!
 
Spring Boot and REST API
Spring Boot and REST APISpring Boot and REST API
Spring Boot and REST API
 

Viewers also liked

Viewers also liked (14)

Document Object Model
Document Object ModelDocument Object Model
Document Object Model
 
DOM ( Document Object Model )
DOM ( Document Object Model )DOM ( Document Object Model )
DOM ( Document Object Model )
 
Dom structure
Dom structureDom structure
Dom structure
 
Document Object Model (DOM) Level 1 Specification
Document Object Model (DOM) Level 1 SpecificationDocument Object Model (DOM) Level 1 Specification
Document Object Model (DOM) Level 1 Specification
 
Document Object Model
Document Object ModelDocument Object Model
Document Object Model
 
Ppt url
Ppt urlPpt url
Ppt url
 
Understanding XML DOM
Understanding XML DOMUnderstanding XML DOM
Understanding XML DOM
 
XML Document Object Model (DOM)
XML Document Object Model (DOM)XML Document Object Model (DOM)
XML Document Object Model (DOM)
 
Document object model(dom)
Document object model(dom)Document object model(dom)
Document object model(dom)
 
Url
UrlUrl
Url
 
Scripting languages
Scripting languagesScripting languages
Scripting languages
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
Url Presentation
Url PresentationUrl Presentation
Url Presentation
 
Js ppt
Js pptJs ppt
Js ppt
 

Similar to Dom (20)

java API for XML DOM
java API for XML DOMjava API for XML DOM
java API for XML DOM
 
Dom
DomDom
Dom
 
Unit 2
Unit 2 Unit 2
Unit 2
 
The xml
The xmlThe xml
The xml
 
Ch23
Ch23Ch23
Ch23
 
Ch23 xml processing_with_java
Ch23 xml processing_with_javaCh23 xml processing_with_java
Ch23 xml processing_with_java
 
Xml session
Xml sessionXml session
Xml session
 
04 sm3 xml_xp_07
04 sm3 xml_xp_0704 sm3 xml_xp_07
04 sm3 xml_xp_07
 
Document Object Model
Document Object ModelDocument Object Model
Document Object Model
 
Jquery 2
Jquery 2Jquery 2
Jquery 2
 
Document Object Model (DOM)
Document Object Model (DOM)Document Object Model (DOM)
Document Object Model (DOM)
 
Unit 10: XML and Beyond (Sematic Web, Web Services, ...)
Unit 10: XML and Beyond (Sematic Web, Web Services, ...)Unit 10: XML and Beyond (Sematic Web, Web Services, ...)
Unit 10: XML and Beyond (Sematic Web, Web Services, ...)
 
Xml And JSON Java
Xml And JSON JavaXml And JSON Java
Xml And JSON Java
 
DOM Quick Overview
DOM Quick OverviewDOM Quick Overview
DOM Quick Overview
 
06 xml processing-in-.net
06 xml processing-in-.net06 xml processing-in-.net
06 xml processing-in-.net
 
Xml 215-presentation
Xml 215-presentationXml 215-presentation
Xml 215-presentation
 
advDBMS_XML.pptx
advDBMS_XML.pptxadvDBMS_XML.pptx
advDBMS_XML.pptx
 
DATA INTEGRATION (Gaining Access to Diverse Data).ppt
DATA INTEGRATION (Gaining Access to Diverse Data).pptDATA INTEGRATION (Gaining Access to Diverse Data).ppt
DATA INTEGRATION (Gaining Access to Diverse Data).ppt
 
Xml nisha dwivedi
Xml nisha dwivediXml nisha dwivedi
Xml nisha dwivedi
 
Java script
Java scriptJava script
Java script
 

More from Surinder Kaur

More from Surinder Kaur (11)

Lucene
LuceneLucene
Lucene
 
Agile
AgileAgile
Agile
 
MapReduce
MapReduceMapReduce
MapReduce
 
Apache Hive
Apache HiveApache Hive
Apache Hive
 
JSON Parsing
JSON ParsingJSON Parsing
JSON Parsing
 
Analysis of Emergency Evacuation of Building using PEPA
Analysis of Emergency Evacuation of Building using PEPAAnalysis of Emergency Evacuation of Building using PEPA
Analysis of Emergency Evacuation of Building using PEPA
 
Skype
SkypeSkype
Skype
 
NAT
NATNAT
NAT
 
XSLT
XSLTXSLT
XSLT
 
intelligent sensors and sensor networks
intelligent sensors and sensor networksintelligent sensors and sensor networks
intelligent sensors and sensor networks
 
MPI n OpenMP
MPI n OpenMPMPI n OpenMP
MPI n OpenMP
 

Recently uploaded

How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 

Recently uploaded (20)

How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 

Dom

  • 2. INTRODUCTION  W3C standard recommendation  For building a tree structure in memory for XML documents  DOM Application Programming Interface (API) is a a programmatic library  Allows manipulation of the contents of an XML document.  The DOM interfaces are platform and language independent.
  • 3. DOM STRUCTRE MODEL  Based on OO(Object Oriented) concepts: – methods (to access or change object’s state) – interfaces (declaration of a set of methods) – objects (encapsulation of data and methods)
  • 4. LIMITATIONS  Since DOM tree resides in the memory. Hence larger the XML document the more memory resource it will consume.  As its resource needs are high, its processing is slower as compared to other XML parsers.
  • 5. DOM LEVELS  3 different levels: – Core DOM - standard model for any structured document – XML DOM - standard model for XML documents – HTML DOM - standard model for HTML documents
  • 6. What is a XML Parser?
  • 7. XML DOM PARSER  The XML DOM parser converts XML into a JavaScript accessible object (the XML DOM).  Firstly XML document must be loaded into an XML DOM object.  An XML parser reads XML, and converts it into an XML DOM object that can be accessed with JavaScript.  Most browsers have a built-in XML parser.
  • 8. DOM BASED PARSERS Parser Description JAXP Sun Microsystem’s Java API for XML Parsing (JAXP) XML4J IBM’s XML Parser for Java (XML4J) msxml Microsoft’s XML parser (msxml) version 2.0 is built-into Internet Explorer 5.5 4DOM 4DOM is a parser for the Python programming language. XML::DOM XML::DOM is a Perl module to manipulate XML documents using Perl. Xerces Apache’s Xerces Java Parser
  • 9. Core Interfaces: Node & its variants Notes 3.2: Document Object Model 9 Node Comment DocumentFragment Attr Text Element CDATASection ProcessingInstruction CharacterData EntityDocumentType Notation EntityReference “Extended interfaces” Document
  • 10. DOM NODE TYPES NODE DESCRIPTION CHILDREN Document entire document (the root-node of the DOM tree) Element (max. one), ProcessingInstruc tion, Comment, DocumentType DocumentFragment Represents a "lightweight" Document object, which can hold a portion of a document Element, ProcessingInstruc tion, Comment, Text, CDATASection, EntityReference DocumentType Provides an interface to the entities defined for the document None ProcessingInstruction Represents a processing instruction None
  • 11. NODE TYPES(contd.) NODE DESCRIPTION CHILDREN EntityReference Represents an entity reference Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference Element Represents an element Element, Text, Comment, ProcessingInstruction, CDATASection, EntityReference Attr Represents an attribute Text, EntityReference Text Represents textual content in an element or attribute None
  • 12. NODE TYPES(contd.) NODE DESCRIPTION CHILDREN CDATASection Represents a CDATA section in a document (text that will NOT be parsed by a parser) None Comment Represents a comment None Entity Represents an entity Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference Notation Represents a notation declared in the DTD None
  • 13. NODE TYPE  specifies the type of node.  nodeType is read only property.  The most important node types are: Node type NodeType(Numeric value) Element 1 Attribute 2 Text 3 Comment 8 Document 9
  • 14. NODE RELATIONSHIP  node relationships are defined as properties to the nodes: – parentNode – childNodes – firstChild – lastChild – nextSibling – previousSibling
  • 15. EXAMPLE <?xml version = "1.0"?> <message from = "Paul" to = "Tem"> <body>Hi, Tem!</body> </message>
  • 16. XML DOM NODE TREE ROOT Element message Attribute: from Attribute: to Element: body Text: Hi,Tem!
  • 17. DOM PROPERTIES  Let x is a node object  Some typical DOM properties of x: – x.nodeName - the name of node x – x.nodeValue - the value of node x – x.parentNode - the parent node of node x – x.childNodes - the child nodes of node x – x.attributes - the attributes nodes of node x
  • 18. XML DOM METHODS METHOD DESCRIPTION appendChild() Appends a child node. cloneNode() Duplicates the node. getAttributes() Returns the node’s attributes. getChildNodes() Returns the node’s child nodes. getNodeName() Returns the node’s name. getNodeType() Returns the node’s type getNodeValue() Returns the node’s value. getParentNode() Returns the node’s parent. hasChildNodes() Returns true if the node has child nodes. removeChild() Removes a child node from the node. replaceChild() Replaces a child node with another node. setNodeValue() Sets the node’s value. insertBefore() Appends a child node in front of a child node.
  • 19. Let us consider a XML document for bookstore <bookstore> <book category="cooking"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> </book> <book category="children"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> </book> <book category="web"> <title lang="en">XQuery Kick Start</title> <author>James McGovern</author>
  • 20. XML DOM Load Functions The function is stored in an external file called "loadxmldoc.js", function loadXMLDoc(dname) { if (window.XMLHttpRequest) { xhttp=new XMLHttpRequest(); } else { xhttp=new ActiveXObject("Microsoft .XMLHTTP"); } xhttp.open("GET", dname , false); xhttp.send(); return xhttp.responseXML; }
  • 21. Contd…  Method: open(method, url, async)  Specifies the type of request, the URL, and if the request should be handled asynchronously or not. method: the type of request: GET or POST url: the location of the file on the server async: true (asynchronous) or false (synchronous)
  • 22. XML loadXMLString() Function function loadXMLString(txt) { if (window.DOMParser) { parser=new DOMParser(); xmlDoc=parser.parseFromString(txt, "text/xml"); } else // Internet Explorer { xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async=false; xmlDoc.loadXML(txt); } return xmlDoc; }
  • 23. Example <html><head><script src="loadxmlstring.js"></script></head> <body><script> text="<bookstore><book>"; text=text+"<title>Everyday Italian</title>"; text=text+"<author>Giada De Laurentiis</author>"; text=text+"<year>2005</year>"; text=text+"</book></bookstore>"; xmlDoc=loadXMLString(text); document.write(xmlDoc.getElementsByTagName("title")[0].childNo des[0].nodeValue); document.write("<br>"); document.write(xmlDoc.getElementsByTagName("author")[0].child Nodes[0].nodeValue); document.write("<br>"); document.write(xmlDoc.getElementsByTagName("year")[0].childN odes[0].nodeValue); </script></body> </html>
  • 24. ACCESSING NODES  There are 3 ways: 1. By using the getElementsByTagName() method 2. By looping through (traversing) the nodes tree. 3. By navigating the node tree, using the node relationships.
  • 25. getElementsByTagName() method  Syntax: node.getElementsByTagName("tagname”);  returns an array of all elements with the specified tagname that are descendents of element “node”.  The elements in the node list returned can be accessed using index number that starts with 0.  To get all elements with specific tagname in the XML document use: – xmlDoc.getElementsByTagName(“tagname”);  where xmlDoc is the document itself (document node).
  • 27. Traversing the Tree  Load xml document.  Get the child nodes of the root element.  For each child node, check the node type of the node.  If the node type is matches the node type of the desired node process that node.
  • 29. Using the Node Relationships.  Load xml document.  Get the child nodes of the first element  Set the "y" variable to be the first child node of the first element.  For each child node (starting with the first child node "y"). Check the node type.  If the node type is desired node type process that node.  Set the "y" variable to be the next sibling node, and run through the loop again
  • 31. CHANGE NODE VALUES  The nodeValue property is used to change a node value.  The setAttribute() method is used to change an attribute value.
  • 32. Changing value of Element node code changes the text node value of the first <title> element i.e. Everyday Italian to Easy Cooking <html> <head><script src="loadxmldoc.js"> </script></head> <body> <script> xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]; x.nodeValue="Easy Cooking"; </script> </body> </html>
  • 33. Change an Attribute Using setAttribute() Change the attribute “category” to “food” of element book <html> <head><script src="loadxmldoc.js"> </script></head> <body><script> xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName('book'); x[0].setAttribute("category","food"); document.write(x[0].getAttribute("category")); </script></body> </html>
  • 34. Change an Attribute Using nodeValue <html> <head><script src="loadxmldoc.js"> </script></head> <body><script> xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("book")[0] y=x.getAttributeNode("category"); y.nodeValue="food"; </script></body> </html>
  • 36. Remove an Element Node <html> <head><script src="loadxmldoc.js"> </script></head> <body><script> xmlDoc=loadXMLDoc("books.xml"); document.write(xmlDoc.getElementsByTagName('book').length); document.write("<br>"); y=xmlDoc.getElementsByTagName("book")[0]; xmlDoc.documentElement.removeChild(y); document.write(xmlDoc.getElementsByTagName('book').length); </script></body> </html>
  • 37. Remove the Current Node Navigate to the node to be removed then use parentNode property and removeChild () <html> <head><script src="loadxmldoc.js"> </script></head> <body><script> xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("book")[0]; x.parentNode.removeChild(x); </script></body> </html>
  • 38. Remove the Text Node <html> <head><script src="loadxmldoc.js"> </script></head> <body><script> xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0]; y=x.childNodes[0]; x.removeChild(y); </script></body> </html>
  • 39. Remove the Text of Text Node <html> <head><script src="loadxmldoc.js"> </script></head> <body><script> xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0]; x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]; document.write("Value: " + x.nodeValue); document.write("<br>"); x.nodeValue=""; document.write("Value: " + x.nodeValue); </script></body> </html>
  • 40. Remove Attribute <html> <head><script src="loadxmldoc.js"> </script></head> <body><script> xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName('book'); document.write(x[0].getAttribute('category')); document.write("<br>"); x[0].removeAttribute('category'); document.write(x[0].getAttribute('category')); </script></body> </html>
  • 42. Create a New Element Node <html> <head><script src="loadxmldoc.js"> </script></head> <body><script> xmlDoc=loadXMLDoc("books.xml"); newel=xmlDoc.createElement("edition"); x=xmlDoc.getElementsByTagName("book")[0]; x.appendChild(newel); </script></body> </html>
  • 43. Create a New Attribute Node <html> <head><script src="loadxmldoc.js"> </script></head> <body><script> xmlDoc=loadXMLDoc("books.xml"); newatt=xmlDoc.createAttribute("edition"); newatt.nodeValue="first"; x=xmlDoc.getElementsByTagName("title"); x[0].setAttributeNode(newatt); </script> </body> </html>
  • 44. Create an Attribute Using setAttribute() <html> <head><script src="loadxmldoc.js"> </script></head> <body><script> xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title"); x[0].setAttribute("edition","first"); </script></body> </html>
  • 45. Create a CDATA SECTION Node <html> <head><script src="loadxmldoc.js"></script></head> <body><script> xmlDoc=loadXMLDoc("books.xml"); newCDATA=xmlDoc.createCDATASection("Special Offer & Book Sale"); x=xmlDoc.getElementsByTagName("book")[0]; x.appendChild(newCDATA); document.write(x.lastChild.nodeValue); </script></body> </html>
  • 46. Create a TEXT Section Node <html> <head><script src="loadxmldoc.js"> </script></head> <body><script> xmlDoc=loadXMLDoc("books.xml"); newel=xmlDoc.createElement("edition"); newtext=xmlDoc.createTextNode("first"); newel.appendChild(newtext); x=xmlDoc.getElementsByTagName("book")[0]; x.appendChild(newel); </script></body> </html>
  • 48. After Last Child Node <html> <head><script src="loadxmldoc.js"> </script></head> <body><script> xmlDoc=loadXMLDoc("books.xml"); newel=xmlDoc.createElement("edition"); x=xmlDoc.getElementsByTagName("book")[0]; x.appendChild(newel); </script></body> </html>
  • 49. Before a Specified Child Node <html> <head><script src="loadxmldoc.js"> </script></head> <body><script> xmlDoc=loadXMLDoc("books.xml"); newNode=xmlDoc.createElement("book"); x=xmlDoc.documentElement; y=xmlDoc.getElementsByTagName("book")[3]; x.insertBefore(newNode , y); </script></body> </html>
  • 50. Add Text to a Text Node - insertData() The insertData() method has two parameters: offset - Where to begin inserting characters (starts at zero) string - The string to insert <html> <head><script src="loadxmldoc.js"> </script></head> <body><script> xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]; x.insertData(0,"Easy "); </script></body> </html>
  • 52. Replace an Element Node <html> <head><script src="loadxmldoc.js"> </script></head> <body><script> xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.documentElement; newNode=xmlDoc.createElement("book"); newTitle=xmlDoc.createElement("title"); newText=xmlDoc.createTextNode("A Notebook"); newTitle.appendChild(newText); newNode.appendChild(newTitle); y=xmlDoc.getElementsByTagName("book")[0] x.replaceChild(newNode,y); </script></body> </html>
  • 53. Replace Data In a Text Node <html> <head><script src="loadxmldoc.js"> </script></head> <body><script> xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]; x.nodeValue="Easy Italian"; </script></body> </html>
  • 55. Example The cloneNode() method has a parameter (true or false). This parameter indicates if the cloned node should include all attributes and child nodes of the original node. <html> <head><script src="loadxmldoc.js"> </script></head> <body><script> xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName('book')[0]; cloneNode=x.cloneNode(true); xmlDoc.documentElement.appendChild(cloneNode); </script></body> </html>