This document provides an introduction to XML DOM (Document Object Model) including:
- XML DOM defines a standard for accessing and manipulating XML documents and is a W3C standard.
- The DOM presents an XML document as a tree structure with elements, attributes, and text as nodes.
- The DOM is separated into three levels: Core DOM, XML DOM, and HTML DOM.
- DOM properties and methods allow accessing and modifying nodes, and DOM parsing converts an XML document into accessible DOM objects.
2. INDEX :
XML DOM – Introduction
DOM Nodes (Tree Structure)
DOM Parsing
DOM Load Functions (in IE & FireFox)
DOM Properties & Methods
DOM HttpRequest
DOM Node Types
DOM Parse Error Objects
Thank You!!
3. XML DOM (INTRODUCTION)
The XML DOM (Document Object Model) defines a standard
way for accessing and manipulating XML documents.
The DOM is a W3C (World Wide Web Consortium) standard.
DOM defines the objects and properties and methods (interface)
to access all XML elements.
The DOM presents an XML document as a tree structure, with
elements, attributes, and text as nodes.
The 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
4. What is an XML DOM?
The XML DOM is:
- A standard object model for XML
- A standard programming interface for XML
- Platform and language-independent
- A W3C standard
The XML DOM defines the objects and properties
of all XML elements, and the methods (interface) to
access them.
The XML DOM is a standard for how to get, change,
add or delete XML elements.
5. AN EXAMPLE OF books.xml:
<bookstore>
- <book category="cooking">
<title lang="en">Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
- <book category="language">
<title lang="en">English</title>
<author>Om Vikram</author>
<year>2005</year>
<price>50.00</price>
</book>
</bookstore>
7. DOM NODES DESCRIPTION
According to the 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.
In our ex.
- The root node <bookstore> holds four <book> nodes.
- Each <book> node contain 4 text node i.e.
<title>, <author>, <year>, and <price>
8. XML DOM PARSING:
Most browsers have a build-in XML parser to read
and manipulate XML.
The parser reads XML into memory and converts
XML it into XML DOM object which is accessible
from JavaScript .
There are some differences between Microsoft's
XML parser and the parsers used in other browsers.
The MS parser supports loading of both XML files
and XML strings (text)
Other browsers use Separate parsers.
However, all parsers contain functions to traverse
XML trees, access, insert, and delete nodes.
9. Loading XML with IE using MS
XML Parser:
The following JavaScript fragment loads an XML
document “book.xml” into the parser :
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.load("books.xml");
The following JavaScript fragment loads an XML text
into the parser :
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.loadXML(txt);
10. Loading XML with FireFox & Other
XML Parser :
The following JavaScript fragment loads an XML
document “book.xml” into the parser :
xmlDoc=document.implementation.createDocument("","",null)
;
xmlDoc.async="false";
xmlDoc.load("books.xml");
The following JavaScript fragment loads an XML text
into the parser :
parser=new DOMParser();
xmlDoc=parser.parseFromString(txt,"text/xml");
11. DOM PROPERTIES & METHODS:
XML DOM Properties:
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
(where x is a node object. )
XML DOM Methods:
x.getElementsByTagName(name) - get all elements
with a specified tag name
x.appendChild(node) - insert a child node to x
x.removeChild(node) - remove a child node from x
14. DOM PARSE ERROR OBJECTS
When trying to open an XML document, a parser-error may
occur.
With the parseError object, you can retrieve the error code, the
error text, the line that caused the error, and more.
Property Description
errorCode Returns a long integer error code
Reason Returns a string containing the reason for the error
Line Returns a long integer representing the line
number for the error
Linepos Returns a long integer representing the line
position for the error
srcText Returns a string containing the line that caused the
error
url Returns the URL pointing the loaded document
Filepos Returns a long integer file position of the error