The Parser and DOM - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

The Parser and DOM

Description:

A software component sitting between the application and the XML files ... DHTML. HTML. IE 5.0. XML Parser (msxml) XML. Loading XML. Doc. XML DOM. XML. Tags & Data ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 26
Provided by: ccNct
Category:
Tags: dom | dhtml | parser

less

Transcript and Presenter's Notes

Title: The Parser and DOM


1
The Parser and DOM
2
Introduction
3
XML Parser
  • XML Parser
  • A software component sitting between the
    application and the XML files
  • Read and interpret the XML syntax
  • Validating and Non-validating parsers

4
The Parser and the Application
5
The Architecture of an XML Program
  • Interface between Parser and Application
  • Object-Based Interface
  • Event-Based Interface

6
Object-Based Interface
  • Parser explicitly builds a tree of objects
    containing all the element in an XML document
  • Example prices.xml

7
Event-Based Interface
  • The parser reads an XML file and generates events
    as it finds elements, attributes, or text in the
    file

8
Object-based VS Event-based
  • Object-based interface
  • For applications that manipulate XML documents
    such as browsers, XSL processors
  • Event-based interface
  • Applications that maintain their own data
    structure in a non-XML format
  • Import XML documents in a databases
  • More efficient

9
Standards
  • Object-based interface
  • DOM Document Object Model
  • http//www.w3.org/DOM/
  • Event-based interface
  • SAX Simple API for XML
  • http//www.megginson.com/SAX/

10
DOM
  • Originally developed for browsers
  • DOM Level
  • Level 1 well-formed documents
  • Level 2 valid documents
  • under development
  • DOM defines classes of objects to represent every
    element in an XML file.

11
The Hierarchy in DOM
12
Getting Started with DOM
13
Client-Side Scripting with XML DOM
Loading XML Doc.
XML
IE 5.0
HTML
XML DOM
XML Parser(msxml)
JS Code
  • DHTML
  • HTML

XML Tags Data
14
Example
  • Essential files
  • prices.xml
  • convert.html
  • conversion.js

15
XML Data Island
history
window
forms
navigator
document
images
XML data island
etc
XML Document
16
DOM Node
  • The core object in DOM tree is Node
  • Generic objects in DOM
  • Most DOM objects are derived from Node
  • searchPrice() tests if the current node is an
    element
  • Node Properties
  • nodeType
  • parentNode
  • childNodes
  • firstChild
  • lastChild
  • previousSibling
  • nextSibling
  • attributes
  • nodeName
  • nodeValue

17
nodeType Code
  • Element 1
  • Attribute 2
  • Text 3
  • CDATA Section 4
  • Entity reference 5
  • Entity 6
  • Processing instruction7
  • Comment 8
  • Document 9
  • Document Type 10
  • Document fragment 11
  • Notation 12

18
Document Object
  • The topmost element in a DOM tree is Document
  • Inherits from Node
  • Two more properties
  • documentElement XML top-element
  • doctype Document type (DOM Level 2)
  • Document is similar to the root in XSL path
  • To return a tree, the parser returns a Document
    Object
  • xmldocument returned in parse() function is a
    Document object

19
Walking the Element Tree
  • Recursively traverse the DOM tree
  • Do any node-specific processing, such as
    outputing data
  • Visit all its children
  • searchPrice()
  • childNodes child list
  • length
  • item(i)

20
Element Object
  • Element
  • Descendant of node to represent XML elements
  • Property and method
  • tagName for an elements tag name
  • getElementsByTagName()
  • normalize()

21
Text Object
  • textNode.data to get the content of a text object
  • ltpgtThe element can contain text and other
    elements such as images ltimg srclogo.gif /gt or
    others. lt/pgt
  • ltpgt contains two text objects and one element
    object (ltimggt)
  • To retrieve the text of an element, it is safer
    to iterate over the elements children

22
Managing the State
  • It is common to collect information from several
    elements or to process elements only if they are
    children of other elements.
  • Must maintain state information
  • Remember where its coming from or what children
    it is expecting
  • Example
  • convert2.html
  • conversion2.js
  • walkNode()
  • walkProduct()

23
Attributes (I)
  • Example convert3.html
  • walkRate() Manipulating attributes
  • Element objects have an attributes property
  • The attributes property is a NamedNodeMap object
  • NamedNodeMap
  • A list of nodes with a named attached to them
  • length and item(I)
  • getNamedItem()
  • setNamedItem()
  • removeNamedItem()

24
Attributes (II)
  • Attr objects
  • Represent the attributes
  • A Node descendant
  • Properties
  • name name of the attribute
  • value value of the attribute
  • specified true if the attribute was given a
    value in the document false if the attribute has
    taken a default value from the DTD

25
DOM in Other Languages
  • DOM and Java
  • IBM XML for Java
  • Xerces-J (Apache)
  • XML Parser for C
  • Xerces-C (Apache)
  • XML Parser for Perl
  • XML Parser Module
  • MSXML Technology Preview SDK
Write a Comment
User Comments (0)
About PowerShow.com