In this session, you will learn to:

Slides:



Advertisements
Similar presentations
J0 1 Marco Ronchetti - Web architectures – Laurea Specialistica in Informatica – Università di Trento Java XML parsing.
Advertisements

XML, Uploading, Importing... Joshua Scotton.
The Web Warrior Guide to Web Design Technologies
XML Parsing Using Java APIs AIP Independence project Fall 2010.
SAX A parser for XML Documents. XML Parsers What is an XML parser? –Software that reads and parses XML –Passes data to the invoking application –The application.
XML DOM and SAX Parsers By Omar RABI. Introduction to parsers  The word parser comes from compilers  In a compiler, a parser is the module that reads.
Technical Track Session XML Techie Tools Tim Bornholt.
PHP with XML Dequan Chen and Narith Kun ---Term Project--- for WSU 2010 Summer Course - CS366 s:
DHTML. What is DHTML?  DHTML is the combination of several built-in browser features in fourth generation browsers that enable a web page to be more.
XML eXtensible Markup Language by Darrell Payne. Experience Logicon / Sterling Federal C, C++, JavaScript/Jscript, Shell Script, Perl XML Training XML.
1 XML at a neighborhood university near you Innovation 2005 September 16, 2005 Kwok-Bun Yue University of Houston-Clear Lake.
JavaScript, Fourth Edition Chapter 12 Updating Web Pages with AJAX.
XSLT for Data Manipulation By: April Fleming. What We Will Cover The What, Why, When, and How of XSLT What tools you will need to get started A sample.
XP New Perspectives on XML, 2 nd Edition Tutorial 10 1 WORKING WITH THE DOCUMENT OBJECT MODEL TUTORIAL 10.
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
Execution Environment for JavaScript " Java Script Window object represents the window in which the browser displays documents. " The Window object provides.
SAX Parsing Presented by Clifford Lemoine CSC 436 Compiler Design.
CIS 375—Web App Dev II DOM. 2 Introduction to DOM The XML Document ________ Model (DOM) is a programming interface for XML documents. It defines the way.
Advanced Java Session 9 New York University School of Continuing and Professional Studies.
XML Parsers Overview  Types of parsers  Using XML parsers  SAX  DOM  DOM versus SAX  Products  Conclusion.
Parsing with DOM using MSXML Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
SAX. What is SAX SAX 1.0 was released on May 11, SAX is a common, event-based API for parsing XML documents Primarily a Java API but there implementations.
Electronic Commerce COMP3210 Session 4: Designing, Building and Evaluating e-Commerce Initiatives – Part II Dr. Paul Walcott Department of Computer Science,
DOM Robin Burke ECT 360. Outline XHTML in Schema JavaScript DOM (MSXML) Loading/Parsing Transforming parameter passing DOM operations extracting data.
Working with the XML Document Object Model ©NIITeXtensible Markup Language/Lesson 7/Slide 1 of 44 Objectives In this lesson, you will learn to: *Identify.
The XML Document Object Model (DOM) Aug’10 – Dec ’10.
XML 6.4 DOM 6. The XML ‘Alphabet Soup’ XMLExtensible Markup Language Defines XML documents XSLExtensible Stylesheet Language Language for expressing stylesheets;
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
1 Dr Alexiei Dingli XML Technologies XML Advanced.
1 XSLT An Introduction. 2 XSLT XSLT (extensible Stylesheet Language:Transformations) is a language primarily designed for transforming the structure of.
XML Document Object Model Anthony Borquez. The Document Object Model a programming interface for HTML and XML documents. It defines the way a document.
McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Scripting with the DOM Ellen Pearlman Eileen Mullin Programming the Web.
Sheet 1XML Technology in E-Commerce 2001Lecture 3 XML Technology in E-Commerce Lecture 3 DOM and SAX.
Introduction to XML This presentation covers introductory features of XML. What XML is and what it is not? What does it do? Put different related technologies.
Web Technologies COMP6115 Session 4: Adding a Database to a Web Site Dr. Paul Walcott Department of Computer Science, Mathematics and Physics University.
1 Dr Alexiei Dingli XML Technologies SAX and DOM.
XML Study-Session: Part III
Introduction to the Document Object Model Eugenia Fernandez IUPUI.
SAX2 and DOM2 Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
XML and SAX (A quick overview) ● What is XML? ● What are SAX and DOM? ● Using SAX.
CISC 3140 (CIS 20.2) Design & Implementation of Software Application II Instructor : M. Meyer Address: Course Page:
When we create.rtf document apart from saving the actual info the tool saves additional info like start of a paragraph, bold, size of the font.. Etc. This.
XML Parser using JavaScript. 2 Microsoft To manipulate an XML document in javascript, you need an XML parser. Today all browsers come with in-built parsers.
1 Introduction JAXP. Objectives  XML Parser  Parsing and Parsers  JAXP interfaces  Workshops 2.
CO1552 – Web Application Development Further JavaScript: Part 1: The Document Object Model Part 2: Functions and Events.
Martin Kruliš by Martin Kruliš (v1.1)1.
Document Object Model.  The XML DOM (Document Object Model) defines a standard way for accessing and manipulating XML documents.  The DOM presents an.
7-Mar-16 Simple API XML.  SAX and DOM are standards for XML parsers-- program APIs to read and interpret XML files  DOM is a W3C standard  SAX is an.
XML DOM Week 11 Web site:
XML & JSON. Background XML and JSON are to standard, textual data formats for representing arbitrary data – XML stands for “eXtensible Markup Language”
21-Jun-16 Document Object Model DOM. SAX and DOM SAX and DOM are standards for XML parsers-- program APIs to read and interpret XML files DOM is a W3C.
Week-9 (Lecture-1) XML DTD (Data Type Document): An XML document with correct syntax is called "Well Formed". An XML document validated against a DTD is.
DHTML.
XML Parsers Overview Types of parsers Using XML parsers SAX DOM
In this session, you will learn to:
In this session, you will learn to:
Unit 4 Representing Web Data: XML
Beginning XML 4th Edition.
Java XML IS
Introduction to the Document Object Model
DOM Robin Burke ECT 360.
CHAPTER 9 JAVA AND XML.
Chapter 7 Representing Web Data: XML
JavaScript Introduction
Week 11 Web site: XML DOM Week 11 Web site:
XML Parsers Overview Types of parsers Using XML parsers SAX DOM
In this session, you will learn to:
A parser for XML Documents
XML Programming in Java
XML and Web Services (II/2546)
Presentation transcript:

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

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

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);

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>

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.

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.

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.

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>01-8282882</phone-number> <!-- parser invokes endElement() method--> </Address>

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

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.

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.

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”);

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.

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.

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);

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);

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()

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;

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

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

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