Presentation is loading. Please wait.

Presentation is loading. Please wait.

In this session, you will learn to:

Similar presentations


Presentation on theme: "In this session, you will learn to:"— Presentation transcript:

1 In this session, you will learn to:
Objectives In this session, you will learn to: Categorize the types of DOM nodes Identify the SAX API Identify differences between the XPath and DOM data models

2 Some of the commonly used node types are:
Types of DOM Nodes Document Object Model (DOM) specifies that everything in an XML document is represented by a node. The XML parser creates a DOM tree for the XML document after parsing it. In the DOM tree, every node type is represented by a named constant and a corresponding numeric constant. Some of the commonly used node types are: Element Attr Text Comment Document

3 Types of DOM Nodes (Contd.)
The following code depicts the usage of the DOM nodes: <html> <head> <script type="text/javascript"> function loadXMLDoc(dname) { var xmlDoc; // Code for Internet Explorer if (window.ActiveXObject) { xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); } xmlDoc.async=false; xmlDoc.load(dname); return(xmlDoc);

4 Types of DOM Nodes (Contd.)
The following code depicts the usage of the DOM nodes: (Contd.) </script> </head> <body> <script type="text/javascript"> xmlDoc=loadXMLDoc("Book.xml"); var x=xmlDoc.documentElement; var y = x.childNodes; for (i=0;i<y.length;i++) { document.write("Nodename: " + y[i].nodeName); document.write(" (nodetype: " + y[i].nodeType + ")<br />");} </body> </html>

5 The SAX API Simple API for XML (SAX) is a public domain software created by the members of the XML-DEV mailing list. The SAX API is used to process XML documents. The software that implements SAX to process the XML documents is called a SAX parser. A SAX parser is an event-driven parser that reads the syntax constructs of the XML document serially from the beginning to the end. A SAX parser does not load the entire XML document in the memory but only a context of the XML document during processing.

6 The SAX API (Contd.) Comparing DOM and SAX: DOM SAX
DOM defines various methods and properties that can be used to process any element of XML document present in the DOM tree. SAX defines various event listeners that notify the SAX parser of events, such as start of a document, end of a document, start of a tag, and end of a tag. While traversing an XML document by using DOM, it is possible to navigate back to an upper node in the tree. In SAX, information can be accessed in the sequential order only. DOM consumes more memory than SAX because it builds an object tree representation of an XML document in the memory. SAX consumes lesser memory and is preferred for parsing large XML documents.

7 The SAX API (Contd.) Implementing SAX:
SAX uses classes and interfaces defined in the SAX API to process an XML document in the form of a stream of data. The SAX API also defines various event listeners that notify the SAX parser of various events. The SAX parser notifies your SAX application each time it recognizes any syntax constructs in the XML document that is being parsed. The notification is done by means of callback methods, such as startDocument(), characters(), or endDocument(). These methods are defined by handler interfaces, such as ContentHandler and ErrorHandler. A handler interface defines the methods for specific events. You need to implement the handler interfaces in the SAX application and override the callback methods to receive the notification of parsing events.

8 The SAX API (Contd.) The following code snippet depicts SAX implementation: <!-- parser invokes startElement() method --> <Address> <!-- parser invokes startElement() method, characters() method, and then endElement() method --> <name> Tony </name> <!-- parser invokes startElement() method, characters() method, and then endElement() method --> <street> 172,Churchill</street> <phone-number> </phone-number> <!-- parser invokes endElement() method--> </Address>

9 The SAX API (Contd.) Common SAX Parsers:
Parsers are computer programs or components of a program that analyze the input structure with respect to a given format. Programmers do not need to write low-level code to read and process the XML document text when using parsers. In SAX parsing, the parser reads the XML document and reports the data found to a handler interface. Some of the common SAX parsers that support SAX2 are: Xerces MSXML 6.0 JAXP

10 Difference Between the XPath and DOM Data Models
The following table lists differences between the XPath and the DOM data models: XPath Data Model DOM Data Model The value of an element or the root node is the concatenation of the text nodes of all its children. For example, XPATH value of <root> Hello </root> is the string Hello and the XPATH value of <root> Hello <name> John </name> </root> is the string HelloJohn. The value of an element or root node will be null. Does not have separate nodes for CDATA sections. Has a separate node for CDATA sections. Only attributes, elements, processing instructions, and namespace nodes have names. XPATH does not use pseudo-names such as #text and #comment for text and comment. Use pseudo-names for text and comment.

11 You should use a DOM-based parser in the following situations:
Best Practices You should not use DOM for document traversal. Instead, use XPath to find nodes or traverse the document, if possible. This is because DOM consumes a significant amount of system memory and will slow down the application. You should use a DOM-based parser in the following situations: When the size of the XML document is small. This is because the DOM parser constructs a DOM tree in the memory. When an entire document is to be processed. When you want to access a particular element in an XML document.

12 Tips and Tricks When you load an XML document by using DOM, the file is loaded asynchronously by default. In this case, you need to examine the ReadyState property of the XML document to ensure that the document is ready. To avoid this, you can set the async property of the XML document to false. In this case, the parser will not return the control to your code until the document is completely loaded and ready for manipulation. The use of the async property to disable asynchoronous loading is shown in the following example: var xmlDoc; // Code for Internet Explorer if (window.ActiveXObject) { xmlDoc=new ActiveXObject("Microsoft.XMLDOM");} xmlDoc.async=false; xmlDoc.load(“sample.xml”);

13 Why doesn’t the DOM specify anything on memory management?
FAQs Why doesn’t the DOM specify anything on memory management? The DOM specification does not define any methods related to memory management, such as releasing an object. This is because DOM is a programming language-independent API, whereas memory management is language-specific. How does XML fit with DOM? DOM provides an abstract API for constructing, accessing, and manipulating XML and HTML documents. The binding of DOM to a particular programming languages provides a concrete API. Microsoft and other vendors provide APIs that let you use the DOM to query and manipulate XML documents in memory.

14 What are the languages that can be used for DOM programming?
FAQs (Contd.) What are the languages that can be used for DOM programming? DOM programming can be done in any programming or scripting language which can access DOM. JavaScript and VBScript are the commonly used language for DOM programming. In addition, C# and Java also provides API that can be used for DOM programming. Does the DOM require an XML file to be well formed? Yes, DOM requires an XML file to be well formed. In case XML is not well formed the XML processor that builds the DOM tree will stop with a fatal error.

15 FAQs (Contd.) How can I use parseError object to display errors that may occur when I try to load an XML document which is not well formed? You can use different properties of parseError object to display errors that may occur when you try to load an XML document. The following code snippet tries to load a non well-formed XML file and displays error messages by using various properties of parseObject: var xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async="false"; xmlDoc.load("test.xml"); document.write("Error code: " + xmlDoc.parseError.errorCode); document.write("<br />Error reason: " + xmlDoc.parseError.reason); document.write("<br />Error line: " + xmlDoc.parseError.line);

16 Challenge A user has defined a string variable named, date that contains date information in the DD/MM/YYYY format. Now user wants to extract the month information from the date variable and wants to store this information in another string named, month. Which of the following options specifies the correct code to perform this operation? month = substring(date,4,2); month = substring(date,3,2); month = substring-before(date,’/’); month = substring-after(date,’/’); Answer: month = substring(date,4,2);

17 Challenge (Contd.) A user has defined a variable named price that contains price information of a product. Now the user wants to find out the largest integer which is less than or equal to the price variable. Which of the following functions should the user use to determine this value? sum() floor() ceiling() round() Answer: floor()

18 var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
Challenge (Contd.) A user is accessing an XML document, test.xml, by using XML DOM. The user writes the following code to load the test.xml file: var xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async="false"; xmlDoc.load("test.xml"); Now, the user wants to retrieve the reference of the root element of the XML document and wants to store the reference in a variable. Which of the following lines of code should the user append to the preceding code snippet to retrieve and store the reference? var root = xmlDoc.xml; var root = xmlDoc.firstChild; var root = xmlDoc.rootElement; var root = xmlDoc.documentElement;

19 Challenge (Contd.) Answer: var root = xmlDoc.documentElement; 19

20 Challenge (Contd.) A user is accessing an XML document, test.xml, by using XML DOM. The user has stored the reference of a node of test.xml in the Node object. Now, the user wants to retrieve the XML representation of the referred node and its child nodes. Which of the following properties of the Node object should the user use for this purpose? nodeName nodeValue xml childNodes Answer: xml

21 Challenge (Contd.) Tom has written a program to load XML data asynchronously by using XML DOM. Tom wants to start processing XML data as soon as it is available. Which of the following events should Tom handle to process the XML data immediately after it is available? onreadystatechange ondataavailable ontransformnode ondataloaded Answer: ondataavailable


Download ppt "In this session, you will learn to:"

Similar presentations


Ads by Google