Session 5


Published on

XML by Example Session 5

Published in: Technology, Education
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Session 5

  1. 1. Session 5 DOM and SAX, XML DOM and SAX Objects Create by ChungLD faculty XML by Example / Bachkhoa – Aptech Computer Education /31
  2. 2. Objectives <ul><li>Representing data </li></ul><ul><li>Parsers </li></ul><ul><li>DOM </li></ul><ul><li>Working with DOM </li></ul><ul><li>SAX </li></ul><ul><li>Microsoft XML DOM objects </li></ul>Create by ChungLD faculty /31
  3. 3. Representing Data <ul><li>Model: is a way of representing data or information. The data within XML documents can be represented using various models. </li></ul><ul><li>The various documents models available </li></ul>Create by ChungLD faculty /31 Linear model Tree model Object model
  4. 4. Linear Model <ul><li>The linear model can be applied to a static document object, such as book. </li></ul><ul><li>To go to a particular topic in a book, the </li></ul><ul><li>book name, page number, and the line </li></ul><ul><li>number on that page is the only </li></ul><ul><li>information required. </li></ul>Create by ChungLD faculty /31
  5. 5. Tree model <ul><li>XML documents can have a hierarchical structure which can be interpreted as a tree structure, known as XML tree. A tree consists of nodes and at every node in the tree there are character strings. </li></ul>Create by ChungLD faculty /31 Inventory Snacks Drink Fitzy Tipsy Popcorn Wafers Quantity Price Quantity Price Quantity Price Quantity Price
  6. 6. Object Model <ul><li>The XML object model is a collection of objects that is used to access and manipulate the data stored in an XML document. The XML document is modeled like a tree, in which each element in the tree is considered a node. Objects with various properties and methods are used to traverse the tree and its nodes. Each node contains the data in the document. </li></ul>Create by ChungLD faculty /31
  7. 7. Parsers <ul><li>An XML parser is a software package, a libraray or a module that reads XML documents first. The information present in the XML file is then made available by the parser to applications and other programming languages </li></ul>Create by ChungLD faculty /31
  8. 8. Parsing Techniques <ul><li>It is important to parse XML data efficiently, especially in applications that handle large volumes of data. Improper parsing will result in excessive memory usage and processing time that will hamper scalability. </li></ul>Create by ChungLD faculty /31 Parsing Event-driven Parsing Object-based Parsing
  9. 9. Types of parsers <ul><li>The different types of parsers are: </li></ul>Create by ChungLD faculty /31
  10. 10. Simple API for XML (SAX): <ul><ul><li>SAX gives an event based approach to XML parsing. In an event based parsing when the parser encounters an element, the parser returns the elements, its attribute and content. Event based parsing provides a data – centric view of XML. Events include XML tag, detecting errors and so on. </li></ul></ul><ul><ul><li>Advantages: </li></ul></ul><ul><ul><ul><li>Low memory consumption as the entire XML document is not loaded in the memory </li></ul></ul></ul><ul><ul><li>Disadvantages: </li></ul></ul><ul><ul><ul><li>No built-in document navigation support </li></ul></ul></ul><ul><ul><ul><li>No support for random access of XML document </li></ul></ul></ul><ul><ul><ul><li>No namespace and modifying XML document support in place </li></ul></ul></ul>Create by ChungLD faculty /31
  11. 11. Document Object Model(DOM) <ul><ul><li>DOM is a mature standard from W3C. DOM parser builds a hierarchical model of the parsed document. Each of the important locations in the document, various element and attribute containers and characteristics of the model is represented as nodes. </li></ul></ul><ul><ul><li>Advantages: </li></ul></ul><ul><ul><ul><li>Easy to use. Easy navigation by using the APIs. Random access to XML document as the tree is loaded in the memory </li></ul></ul></ul><ul><ul><li>Disadvantages: </li></ul></ul><ul><ul><ul><li>Parsing of the XML document is done once. High memory consumption and expensive as the entire tree structure is loaded in the memory </li></ul></ul></ul>Create by ChungLD faculty /31
  12. 12. Streaming API for XML (StAX) <ul><ul><li>The StAX is a new parsing model introduced recently. Like SAX it is an event-driven model. StAX uses pull model for event processing. In other words, StAX parser returns events that are requested by an application and the events can also be provided as objects. </li></ul></ul><ul><ul><li>Advantages: </li></ul></ul><ul><ul><ul><li>Ease in performance as it supports two parsing models. Parsing controlled by application </li></ul></ul></ul><ul><ul><li>Disadvantages: </li></ul></ul><ul><ul><ul><li>No built-in document navigation support </li></ul></ul></ul><ul><ul><ul><li>No support for random access XML document </li></ul></ul></ul><ul><ul><ul><li>No support for modifying XML document </li></ul></ul></ul>Create by ChungLD faculty /31
  13. 13. Introduction DOM <ul><li>A standard object model for XML </li></ul><ul><li>A standard programming interface for XML </li></ul><ul><li>Platform- and language-independent </li></ul><ul><li>A W3C standard </li></ul>Create by ChungLD faculty /31
  14. 14. DOM objects <ul><li>All elements present in the XML document including their contents is accessed using the DOM tree. In the DOM tree the contents can be added, modified and deleted </li></ul><ul><li>The characteristics of a node tree are: </li></ul><ul><ul><li>The top node represent the root. </li></ul></ul><ul><ul><li>A node has one parent node except root. </li></ul></ul><ul><ul><li>A node can have many children </li></ul></ul><ul><ul><li>A node with no child node is known as the leaf node. </li></ul></ul><ul><ul><li>Nodes having same parent node are known as siblings. </li></ul></ul>Create by ChungLD faculty /31 Click and see
  15. 15. Creation of XML Document Object <ul><li>Create DOM </li></ul><ul><li>Read all xmll </li></ul>Create by ChungLD faculty /31
  16. 16. Traversing a DOM Tree using Element Object <ul><li>An element is a Node object and hence inherits the properties and methods of the Node interface. The method getElementsByTagName() returns a nodeList of the elements and the length() method can be used to loop through the list. </li></ul>Create by ChungLD faculty /31 Click and see
  17. 17. Traversing a DOM Tree using Node Object <ul><li>The node object represents a single node in the document tree. It is the basic data type of the DOM. There are children for all node types </li></ul><ul><li>There different node types are: </li></ul><ul><ul><li>NODE_ELEMENT </li></ul></ul><ul><ul><li>NODE_ATTRIBUTE </li></ul></ul><ul><ul><li>NODE_TEXT </li></ul></ul><ul><ul><li>NODE_PROCESSING_INSTRUCTION </li></ul></ul><ul><ul><li>NODE_COMMENT </li></ul></ul><ul><ul><li>NODE_DOCUMENT </li></ul></ul>Create by ChungLD faculty /31 Click and see
  18. 18. Traversing a DOM Tree using NodeList <ul><li>The NodeList object represents a collection of Node objects. Any alterations to the properties of node are reflected in the list. Individual nodes can be accessed using indexes as well as one can iterate through the collection. </li></ul>Create by ChungLD faculty /31 Click and see
  19. 19. Traversing a DOM Tree using NameNodeMap Object <ul><li>NameNodeMap object represents a collection of nodes that is accessed by name. </li></ul>Create by ChungLD faculty /31 Click and see
  20. 20. Traversing a DOM Tree using Attribute Object <ul><li>The Attribute of an element object is represented by the Attr interface and is defined in DTD. The Attr object is a node and inherits the properties and methods of the Node object. Attribute is a property of a child node and not a child node. An attribute does not have a parent node. </li></ul>Create by ChungLD faculty /31 Click and see
  21. 21. XMLDOMParseError <ul><li>The XMLDOMPaserError object is an extension to the W3C specification. It can be used to get detailed information on the last error that occurred while either loading or parsing a document. The XMLDOMParseError </li></ul>Create by ChungLD faculty /31
  22. 22. Properties of XMLDOMParseError <ul><ul><li>errorCode </li></ul></ul><ul><ul><li>filepos </li></ul></ul><ul><ul><li>line </li></ul></ul><ul><ul><li>linepos </li></ul></ul><ul><ul><li>reason </li></ul></ul><ul><ul><li>srcText </li></ul></ul>Create by ChungLD faculty /31
  23. 23. SAX <ul><li>Simple API for XML is an XML parser which parses an XML document and extracts information from the document. </li></ul>Create by ChungLD faculty /31
  24. 24. XML Parsing using SAX <ul><li>SAX is not a W3C standard and has well-deffined APIs for parsing XML document. SAX generates events as it reads through the document and the event handlers handle these events and provide access to the content of the XML document </li></ul>Create by ChungLD faculty /31
  25. 25. The some common classes <ul><li>ContentHandler calss is used for accessing the contents of the XML document </li></ul>Create by ChungLD faculty /31
  26. 26. The DefaultHandler class <ul><li>This is the default base class for SAX2 event handlers, it provides defalt implementations for all of the callbacks in the four core SAX2 handler classes: Entityresolver, DTDHandler, ContentHandler, ErrorHandler. </li></ul>Create by ChungLD faculty /31
  27. 27. The XML Reader Interface <ul><li>This is allows an application to set and query features and properties in the parser, to register event handlers for document processing and to begin parsing of ta document. </li></ul>Create by ChungLD faculty /31
  28. 28. Microsoft XML DOM Objects <ul><li>The Microsoft implementation of the XML Document Object Model(DOM) provides a set of classes and interface that map to the W3C DOM. </li></ul>Create by ChungLD faculty /31
  29. 29. Properties <ul><li>Properties </li></ul><ul><ul><li>async:This property indicates whether asynchronous download is permitted or not. When set to true, the load() method returns control to the called document before the download is finished. </li></ul></ul><ul><ul><li>doctype: this property return an object representing the root element of the XML document. </li></ul></ul><ul><ul><li>implementation: this property return an implementation object that handles this document. </li></ul></ul><ul><ul><li>nodeName: this property returns a read-only property that returns a string containing the Node’s name depending on the type of Node </li></ul></ul>Create by ChungLD faculty /31
  30. 30. Event <ul><li>ondataavailable:is raised when new data is available </li></ul><ul><li>ontransformnode: is raised before a stylesheet is applied to a node. </li></ul>Create by ChungLD faculty /31
  31. 31. Method Create by ChungLD faculty /31 Click and see
  32. 32. Properties and Method of an XMLDOMNode object <ul><li>Properties </li></ul><ul><ul><li>namespaceURI </li></ul></ul><ul><ul><li>parsed </li></ul></ul><ul><ul><li>xml </li></ul></ul><ul><ul><li>nodeName </li></ul></ul><ul><ul><li>nodeType </li></ul></ul><ul><li>Method </li></ul><ul><ul><li>hasChildNodes </li></ul></ul><ul><ul><li>insertBefore(child1,child2) </li></ul></ul><ul><ul><li>replaceChild(child1,child2) </li></ul></ul><ul><ul><li>removeChild(child) </li></ul></ul><ul><ul><li>appendChild(newChild) </li></ul></ul>Create by ChungLD faculty /31 Click and see
  33. 33. Properties and method of an XMLDOMNodeList <ul><li>Properties </li></ul><ul><ul><li>lengthproperty </li></ul></ul><ul><li>Method </li></ul><ul><ul><li>item </li></ul></ul><ul><ul><li>nextNode </li></ul></ul><ul><ul><li>reset </li></ul></ul>Create by ChungLD faculty /31 Click and see
  34. 34. XMLDOMNamedNodeMap <ul><li>Properties </li></ul><ul><ul><li>length returns the number of nodes in the map </li></ul></ul><ul><li>Methods </li></ul>Create by ChungLD faculty /31 Click and see
  35. 35. Summary and workshop Create by ChungLD faculty /31