document object model (dom): an abstract data structure for xml data alex dekhtyar department of...
Post on 19-Dec-2015
215 views
TRANSCRIPT
Document Object Model (DOM):
An Abstract Data Structure for XML data
Alex DekhtyarAlex DekhtyarDepartment of Computer Department of Computer Science Science University of KentuckyUniversity of Kentucky
About
Jobs for B.S. graduates often require work with Jobs for B.S. graduates often require work with XMLXML
Teach XML in undergraduate curriculumTeach XML in undergraduate curriculum DatabasesDatabases Web ProgrammingWeb Programming Data StructuresData Structures
Outline
XML SyntaxXML Syntax
XML as a treeXML as a tree
Document Object Model (DOM)Document Object Model (DOM)
DOM APIDOM API
<faculty> <name> <first> </first> <middle> </middle> <last> </last> </name> <dept> </dept> <course> <sem> </sem> <code> </code> <title> </title> </course> </faculty>
XML
Alexander M. Dekhtyar
Computer Science
Spring 2007 CS405 Database systems
Meta-language for encoding information
Information, content
XML elements
Opening tags: <name>Closing tags: <name/>Emply elements: <p/>
ordered
<faculty id =“27” > <name origin=“Ukraine”> <first> </first> <middle> </middle> <last> </last> </name> <dept> </dept> <course> <sem> </sem> <code> </code> <title> </title> </course> </faculty>
XML
Alexander M. Dekhtyar
Computer Science
Spring 2007 CS405 Database systems
Meta-language for encoding information
Information, content
XML elements
AttributesAdditional informationunordered
Opening tags: <name>Closing tags: <name/>Emply elements: <p/>
ordered
<faculty id =“27” > <name origin=“Ukraine”> <first> </first> <middle> </middle> <last> </last> </name> <dept> </dept> <course> <sem> </sem> <code> </code> <title> </title> </course> </faculty>
XML
Alexander M. Dekhtyar
Computer Science
Spring 2007 CS405 Database systems
Structure nested elements
Well-formed XML correctly nested elements
World Wide Web Consortium (W3C)
recommendation XML 1.0, 1998
APIAPI
XML and applications
documents data
XMLFiles
<?xml version=“1.0”><?xml version=“1.0”><r><r>
</r></r>
application
Parser InternalInternal data structuredata structure
APIAPI
XML and applications
documents data
XMLFiles
<?xml version=“1.0”><?xml version=“1.0”><r><r>
</r></r>
Parser InternalInternal data structuredata structure
application
XML and applications
InternalInternal data structuredata structure
APIAPI
Document Object Model (DOM)
W3C standard www.w3c.org/DOM
DOM Level 1 (core) – for XMLDOM Level 1 (core) – for XML
DOME Level 1 (HTML) – for HTML
Trees for XML<faculty id =“27” > <name origin=“Ukraine”> <first> </first> <middle> </middle> <last> </last> </name> <dept> </dept> <course> <sem> </sem> <code> </code> <title> </title> </course> </faculty>
Alexander M. Dekhtyar
Computer Science
Spring 2007 CS405 Database systems
facultyfaculty
namename deptdept coursecourse
Trees for XML
<faculty id =“27” > <name origin=“Ukraine”> <first> </first> <middle> </middle> <last> </last> </name> <dept> </dept> <course> <sem> </sem> <code> </code> <title> </title> </course> </faculty>
Alexander M. Dekhtyar
Computer Science
Spring 2007 CS405 Database systems
facultyfaculty
namename deptdept coursecourse
lastlastfirstfirst middlemiddle
Id=“27”
semsem titletitlecodecode
origin=“Ukraine”
AlexanderSpring 2007
M. Dekhtyar
Computer Science
CS 405Database Systems
Trees for XML
<faculty id =“27” > <name origin=“Ukraine”> <first> </first> <middle> </middle> <last> </last> </name> <dept> </dept> <course> <sem> </sem> <code> </code> <title> </title> </course> </faculty>
Alexander M. Dekhtyar
Computer Science
Spring 2007 CS405 Database systems
facultyfaculty
namename deptdept coursecourse
lastlastfirstfirst middlemiddle
Id=“27”
semsem titletitlecodecode
origin=“Ukraine”
AlexanderSpring 2007M. Dekhtyar
Computer Science
CS 405Database Systems
namename - Element node
Computer Science - Text node Id=“27” - Attribute node
facultyfaculty - Root node
Document Object Model
Abstract Data Type
Object-oriented
System of types/interfaces Attributes Methods
Functionality
facultyfaculty
namename deptdept coursecourse
lastlastfirstfirst middlemiddle
Id=“27”
semsem titletitlecodecode
origin=“Ukraine”
AlexanderSpring 2007
M. Dekhtyar
Computer Science
CS 405Database Systems
Creation of nodes
Insertion of nodes in into the DOM Tree
Traversal of the DOM Tree
DOM Type Structureinterface Nodeinterface Node
interface interface ElementElement
interface interface AttrAttr
interface interface DocumentDocument
interface interface DocumentTypeDocumentType
interface interface CommentComment
interface interface TextText
interface interface DocumentTypeDocumentType
interface interface CDATASectionCDATASection
… and a few more
subtype
interface interface NodeListNodeListlist of
interface interface NamedNodeMapNamedNodeMap
set of
interface Node
DOMString nodeName DOMString nodeValue short nodeTypeNode parentNode NodeList childNodes Node firstChildNode lastChild Node previousSiblingNode nextSiblingNamedNodeMap attributes Document ownerDocument
facultyfaculty
namename
firstfirst
origin=“Ukraine”
deptdept
documentdocument
middlemiddle
Node Typesinterface Nodeinterface Node
interface interface ElementElement
interface interface AttrAttr
interface interface DocumentDocument
interface interface DocumentTypeDocumentType
interface interface CommentComment
interface interface TextText
interface interface EntityEntity
interface interface CDATASectionCDATASection
… and a few more
subtype
nodeType
9
1
2
1
0
8
3
6
4(12 nodetypes altogether)
nodeName nodeValue
Tag null
AttName AttValue
#text content
interface Node methods
Node insertBeforeinsertBefore(in Node newChild, in Node refChild)Node replaceChildreplaceChild(in Node newChild, in Node oldChild)Node removeChildremoveChild(in Node oldChild)Node appendChildappendChild(in Node newChild) boolean hasChildNodeshasChildNodes() Node cloneNodecloneNode(in boolean deep)
DOM Type Structureinterface Nodeinterface Node
interface interface ElementElement
interface interface AttrAttr
interface interface DocumentDocument
interface interface DocumentTypeDocumentType
interface interface CommentComment
interface interface TextText
interface interface DocumentTypeDocumentType
interface interface CDATASectionCDATASection
… and a few more
subtype
interface interface NodeListNodeListlist of
interface interface NamedNodeMapNamedNodeMap
set of
interface DocumentAttributesDocumentType doctypeElement documentElement
Methods
documentdocument
DTDDTD
doctype
rootroot
Element createElement(in DOMString tagName)DocumentFragment createDocumentFragment() Text createTextNode(in DOMString data)Comment createComment(in DOMString data)CDATASection createCDATASection(in DOMString data) Attr createAttribute(in DOMString name)
NodeList getElementsByTagName(in DOMString tagname)
<a>
<a>
<a><a>
DOM Type Structureinterface Nodeinterface Node
interface interface ElementElement
interface interface AttrAttr
interface interface DocumentDocument
interface interface DocumentTypeDocumentType
interface interface CommentComment
interface interface TextText
interface interface DocumentTypeDocumentType
interface interface CDATASectionCDATASection
… and a few more
subtype
interface interface NodeListNodeListlist of
interface interface NamedNodeMapNamedNodeMap
set of
interface Elementinterface Element : Node { DOMString tagName;
DOMString getAttributegetAttribute(in DOMString name);
void setAttributesetAttribute(in DOMString name, in DOMString value) void removeAttributeremoveAttribute(in DOMString name)
Attr getAttributeNodegetAttributeNode(in DOMString name); Attr setAttributeNodesetAttributeNode(in Attr newAttr) Attr removeAttributeNoderemoveAttributeNode(in Attr oldAttr)
NodeList getElementsByTagNamegetElementsByTagName(in DOMString name)
};
Attribute management
DOM Type Structureinterface Nodeinterface Node
interface interface ElementElement
interface interface AttrAttr
interface interface DocumentDocument
interface interface DocumentTypeDocumentType
interface interface CommentComment
interface interface TextText
interface interface DocumentTypeDocumentType
interface interface CDATASectionCDATASection
… and a few more
subtype
interface interface NodeListNodeListlist of
interface interface NamedNodeMapNamedNodeMap
set of
DOM Type Structureinterface Nodeinterface Node
interface interface ElementElement
interface interface AttrAttr
interface interface DocumentDocument
interface interface DocumentTypeDocumentType
interface interface CommentComment
interface interface TextText
interface interface DocumentTypeDocumentType
interface interface CDATASectionCDATASection
… and a few more
subtype
interface interface NodeListNodeListlist of
interface interface NamedNodeMapNamedNodeMap
set of
interface NodeListinterface NodeList { Node itemitem(in unsigned long index); unsigned long lengthlength; }
interface NamedNodeMapinterface NamedNodeMap { Node getNamedItemgetNamedItem(in DOMString name); Node setNamedItemsetNamedItem(in Node arg); Node removeNamedItem(in DOMString name);
Node itemitem(in unsigned long index);
unsigned long lengthlength; };
Next…Implementations of DOM interfaces
1. Using pointers
nodes content
attributes
2. Using record arrays3. Wrappers over legacy implementations