Upload
om-vikram-thapa
View
1.812
Download
3
Embed Size (px)
DESCRIPTION
Lets understand XML structure and different xml parsers for different browsers. Will discuss about the different xml parsing errors and methods.
Citation preview
Understanding XML DOM
(W3C Standard)
Created by :
Om Vikram Thapa
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!!
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
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.
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>
XML DOM NODES :
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>
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.
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);
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");
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
AN EXAMPLE :
txt=xmlObj.getElementsByTagName("title")[0].childNodes[0].nodeValue;
where
xmlObj - the XML DOM object created by the parser. getElementsByTagName("title")[0] - the first <title>
element childNodes[0] - the first child of the <title> element (the
text node)
nodeValue - the value of the node (the text itself)
NODE TYPES
NodeTypes Named Constants 1 ELEMENT_NODE 2 ATTRIBUTE_NODE 3 TEXT_NODE 4 CDATA_SECTION_NODE 5 ENTITY_REFERENCE_NODE 6 ENTITY_NODE 7
PROCESSING_INSTRUCTION_NODE 8 COMMENT_NODE 9 DOCUMENT_NODE 10 DOCUMENT_TYPE_NODE 11 DOCUMENT_FRAGMENT_NODE 12 NOTATION_NODE
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 LineposReturns 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
THANK YOU !!