Document Object Model - PowerPoint PPT Presentation

1 / 73
About This Presentation
Title:

Document Object Model

Description:

Manipulate an XML document using the DOM object properties, methods, and events ... are set of excellent methods provided in the W3C DOM API specification. ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 74
Provided by: bhai
Category:
Tags: document | dom | model | object

less

Transcript and Presenter's Notes

Title: Document Object Model


1
Document Object Model
  • Chapter 4

2
Objectives
  • Define DOM
  • Define the different models of DOM
  • Linear model
  • Tree model
  • Object model
  • Describe the tree structure of an XML document
  • Create an XML document object
  • Manipulate an XML document using the DOM object
    properties, methods, and events

3
Document Models
  • The three document models are
  • Linear model
  • Tree model
  • Object model
  • In the linear document model, the document is
    read through character by character, line by
    line.
  • In the tree document model, the document is first
    read from the root, and then the branch and other
    sub branches.
  • In the object model, each part of the document is
    considered as a object.

4
Factory Methods
  • Factory methods are set of excellent methods
    provided in the W3C DOM API specification.
  • This methods help us to build complex documents
    from scratch, to add an element, text, or comment
    in a document

5
Tree Structure of an XML Document
  • The DOM converts a well-formed XML document into
    a tree structure.

XML document ltsamplegt ltstartgt Hello XML
lt/startgt ltendgtGoodbye HTML lt/endgt lt/sample
6
Objects in XML Document
  • Objects found in the Sample XML document
  • The document object
  • The sample object
  • The start object
  • Goodbye HTML text object
  • The end object
  • Hello XML text object

Example
Sample XML document ltsamplegt ltstartgt Hello XML
lt/startgt ltendgt Goodbye HTML lt/endgt lt/samplegt
7
Creating an XML Document Object
  • The syntax used for creating XML document objects
    are
  • Dim xmlDoc
  • Set xmlDocCreateObject(Microsoft.XMLDOM)
  • An XML document will be loaded as a document
    object if it is well formed.
  • MSXML parser will load an XML document only after
    it is well formed and if a DTD is present, then
    the parser validates the document against the
    DTD.

8
Example of Document Validation
ltscript language"vbscript"gt Dim xmlDoc
Set xmlDoc CreateObject("microsoft.xmlDOM")
xmlDoc.async False xmlDoc.load("sample1.xm
l") If xmlDoc.parseError.errorcode ltgt 0
Then MsgBox xmlDoc.parseError.reason
Else MsgBox xmlDoc.documentElement.xml
End If lt/scriptgt
The parseError method is used to validate the
document. The document will not load in case of
an error.
9
Output of the Example
10
Sample XML Document
11
XML DOM Base Objects
  • XML DOM Base Object These objects are used to
    support namespaces, data types, XML schemas, XSL
    operations, asynchronous loading, and save
    documents.
  • DOM Objects These objects are used to represent
    implementations of the XML DOM interfaces.

12
XML DOM Objects
13
DOMDocument Object
  • It represents the top level of the XML source.
  • It includes methods and properties used to obtain
    or create all other XML objects.
  • It creates only one object the document
  • A few properties of the DOMDocument Object
  • async
  • attributes
  • baseName
  • childNodes
  • documentElement
  • A few Methods of the DOMDocument Object
  • createNode
  • appendChild

14
async Property
  • It indicates whether the XML document is
    downloaded asynchronously or synchronously.

asynctrue
15
Attribute Property
  • It is a read-only property.
  • It returns an XMLDOMNamedNodeMap object for
    nodes that can return attributes.
  • The XMLDOMNamedNodeMap is an XML interface
    specifically for working with attributes.

Example ltscript language"vbscript"gt Dim
xmlDoc Dim oNodeMap Set xmlDoc
CreateObject("microsoft.xmldom")
xmlDoc.load("candidate.xml") Set oNodeMap
xmlDoc.documentElement.attributes MsgBox
oNodeMap.length lt/scriptgt
16
baseName Property
  • It is a read-only property.
  • It always returns a nonempty string.

Example ltscript language"vbscript"gt Dim xmlDoc
Dim MyStr Set xmlDoc CreateObject("microsoft.xm
ldom") xmlDoc.load("candidate.xml") MyStr
xmldoc.documentElement.childNodes.item(0).baseName
MsgBox MyStr lt/scriptgt
17
childNode Property
  • It is a read-only property.

Example ltscript language"vbscript"gt Dim
xmlDoc Dim root Dim oList Dim Item Set xmlDoc
CreateObject("microsoft.xmldom") xmlDoc.load("can
didate.xml") Set root xmldoc.documentElement Set
oList root.childNodes For Each Item in
oList document.write(Item.xml"ltbrgt") Next lt/scrip
tgt
18
documentElement Property
  • It is the root element of the document.
  • It is a read/write property.
  • It returns the XMLDOMElement object that
    represents the root of the XML document tree.

Example ltscript language"vbscript"gt Dim root Dim
xmlDoc Set xmlDoc CreateObject("microsoft.xmldom
") xmlDoc.async False xmlDoc.load("candidate.xml
") Set root xmlDoc.documentElement For i 0
To (root.childNodes.length -1)
document.write(root.childNodes.item(i).childNodes.
item(0) .text "ltbrgt") Next lt/scriptgt
19
DOMdocument Object Methods Example
  • lthtmlgt
  • ltbodygt
  • ltscript language"vbs"gt
  • Dim xmlDoc
  • Dim root
  • Dim newNode
  • Set xmlDoc CreateObject("microsoft.xmldom
    ")
  • xmlDoc.async False
  • xmlDoc.load("candidate.xml")
  • Set root xmlDoc.documentElement
  • MsgBox root.xml
  • Set newNode xmlDoc.createNode (1,
    "SampleNode", "")
  • root.appendChild(newNode)
  • MsgBox root.xml
  • lt/scriptgt
  • lt/bodygt
  • lt/htmlgt

20
createNode Method
  • It creates a node using the supplied type, name,
    and namespace.
  • Syntax
  • objNodeoDocument.createNode(type,name,namespaceUR
    I)
  • type parameter uniquely identifies the node
    type
  • name parameter is a string containing the value
    for the new nodes nodeName property
  • namespaceURI is a string defining the namespace
    URI
  • The node is created in the context of the
    namespace mentioned.
  • If the namespace is not specified, the node is
    created in the document namespace.

21
appendchild Method
  • It adds a new node.
  • It returns the new child node successfully
    appended to the list.

Example ltscript language"vbs"gt Dim
xmlDoc Dim root Dim newNode
Set xmlDoc CreateObject("microsoft.xmldom")
xmlDoc.async False xmlDoc.load("candid
ate.xml") Set root xmlDoc.documentElement
MsgBox root.xml Set newNode
xmlDoc.createNode (1, "SampleNode", "")
root.appendChild(newNode) MsgBox
root.xml lt/scriptgt
22
ondatavaliable Event
  • It indicates that XML document data is available.
  • While working with asynchronous data, it is
    useful to start processing in parallel with the
    download as soon as data becomes available.
  • It fires each time a new chuck of data arrives.
  • The readstate property defines several states
    that specify the current status of the
    asynchronous download.

23
XMLDOMNode Object
Some of the DOM objects that inherit XMLDOMNODE
object DOMDocument XTLRuntime XMLDOMAttribute XML
DOMCDATASection XMLDOMComment XMLDOMDocumentType
XMLDOMNode Object
24
XMLDOMNode Object Example
  • lthtmlgt
  • ltbodygt
  • ltscript language"vbscript"gt
  • Dim cNode
  • Set xmlDoc CreateObject("microsoft.xmldom")
  • xmlDoc.async false
  • xmlDoc.load("candidate.xml")
  • Set cNode xmlDoc.documentElement.childNodes.item
    (0)
  • MsgBox cNode.xml
  • lt/scriptgt
  • lt/bodygt
  • lt/htmlgt

25
XMLDOMNode Object Properties
26
lastChild Property
  • The property returns the last child node.

Example lthtmlgt ltbodygt ltscript language"vbscript"gt
Dim xmlDoc Dim currNode Set xmlDoc
CreateObject("microsoft.xmldom") xmlDoc.load("cand
idate.xml") Set currNode xmlDoc.documentElement.
lastchild MsgBox currNode.xml lt/scriptgt lt/bodygt lt/
htmlgt
27
nextSibiling Property
  • It contains the next node of the current nodes in
    the parents child list.

Example ltscript language"vbscript"gt Dim
xmlDoc Dim cNode Dim nextNode Set xmlDoc
CreateObject("microsoft.xmldom") xmlDoc.load("cand
idate.xml") Set cNode xmlDoc.documentElement.chi
ldNodes.item(0) Set nextNode cNode.nextSibling M
sgBox nextNode.xml lt/scriptgt
28
Output of the Example
29
Xml Property
  • It contains the XML representation of the code
    and all its descendants.
  • It is a read-only property.
  • It always returns a UNICODE string, which removes
    the original encoding attribute

lt?xml version1.0 encodingUTF-8?gt appears in
the xml property as lt?xml version1.0?gt
30
Methods of XMLDOMNode Object
31
selectNodes Method
  • It returns the list of matching nodes to an
    XMLDOMNodeList object.

Example lthtmlgt ltbodygt ltscript language"vbscript"gt
Set xmlDoc CreateObject("microsoft.xmldom") xml
Doc.async False xmlDoc.load("candidate.xml") Set
oNList xmlDoc.documentElement.selectNodes("PERS
ON/NAME") MsgBox oNList.length lt/scriptgt lt/bodygt lt
/htmlgt
32
XMLDOMNodeList Object
  • It supports iteration in addition to indexed
    access.
  • A node list collection is alive when the changes,
    addition, and removals are reflected immediately
    in the collection.

33
Length Property
  • It provides the number of nodes in the collection
    of nodes contained in the XMLDOMNodeList object.
  • It is a read-only property.

Example lthtmlgt ltbodygt ltscript
language"vbscript"gt Dim xmlDoc Dim
objNList Set xmlDoc CreateObject("microsoft.
xmldom") xmlDoc.load("candidate.xml") Set
objNList xmlDoc.getElementsByTagName("PERSON")
For i0 To (objNList.length -1) MsgBox
(objNList.item(i).text) Next lt/scriptgt
lt/bodygt lt/htmlgt
34
Output of the Example
35
nextNode Method
  • It returns the next node in the collection.
  • It returns a NULL value if there is no next node.
  • The iterator is initially set to 0 so that the
    first call to nextNode returns the first node
    in the list.

Example ltscript language"vbscript"gt Dim
xmlDoc Dim objNList Dim objNode Set xmlDoc
CreateObject("microsoft.xmldom") xmlDoc.load("cand
idate.xml") Set objNList xmlDoc.getElementsByTag
Name("PERSON") For i0 To (objNList.length -1)
Set objNode objNList.nextNode MsgBox
objNode.text Next lt/scriptgt
36
XMLDOMNamedNodeMap Object
  • It is the other collection object in the DOM.
  • It can be accessed by an index.
  • It is used to iterate through the attributes for
    a specific element.
  • Any change within a node, such as removal and
    addition of nodes is immediately reflected in the
    collection.

Properties of XMLDOMNamedNodeMap
37
Methods of XMLDOMNamedNodeMap Object
  • getNamedItem Method It returns the XMLDOMNode
    object for the specified attribute.
  • Syntax
  • Set varXMLDOMNodevarXMLDOMNamedNodeMap.getNamedIt
    em(aname)
  • removeNamedItem Method It returns the node
    removed from the collection
  • Syntax
  • Set varXMLDOMNodevarXMLDOMNamedNodeMap.removeName
    dItem (aname)

38
XMLDOMParseError Object
  • It returns information about the last parse
    error.
  • The information includes the error number, line
    number, character position, and a text
    description.

39
line Property
  • It specifies the line number that contains the
    error.
  • Example
  • ltscript language"vbscript"gt
  • Dim xmlDoc
  • Set xmlDoc CreateObject("microsoft.xmldom")
  • xmlDoc.async False
  • xmlDoc.load("candidate1.xml")
  • If xmlDoc.parseError.errorcode ltgt 0 Then
  • MsgBox ("A parse error occurred on line "
    xmlDoc.parseError.line)
  • Else
  • MsgBox xmlDoc.documentElement.xml
  • End If
  • lt/scriptgt

output
40
Linepos Property
  • It contains the character position within the
    line where the error occurred.
  • It is a read-only property.

Example ltscript language"vbscript"gt Dim
xmlDoc Set xmlDoc CreateObject("microsoft.xmlDO
M") xmlDoc.async False xmlDoc.load("candidate1.x
ml") If xmlDoc.parseError.errorcode ltgt 0
Then MsgBox ("A parse error occurred on line "
xmlDoc.parseError.line " at position "
xmlDoc.parseError.linepos) Else MsgBox
xmlDoc.documentElement.xml End If lt/scriptgt
41
srcText Property
  • It returns the full text of the line that
    contains an error.
  • It is a read-only property.
  • It returns an empty string if
  • an error occurs due to the document is not well
    formed and
  • an error cannot be located in a specific line

42
Example of srcText Property
ltscript language"vbscript"gt Dim xmlDoc Set
xmlDoc CreateObject("microsoft.xmlDOM") xmlDoc.
async False xmlDoc.load("candidate1.xml") If
xmlDoc.parseError.errorcode ltgt 0 Then MsgBox
xmlDoc.parseError.srcText Else MsgBox
xmlDoc.documentElement.xml End If lt/scriptgt
output
43
XMLDOMAttribute Object
  • It represents an attribute of an element.
  • Attributes are properties of an element and not
    the child nodes of the element.
  • Attributes are considered properties of their
    associated elements rather than independent and
    separate.
  • The objects parentNode, previousSibiling,
    nextsibiling have the value Null.

44
nodeType Property
  • It specifies the XML DOM node type.
  • It is a read-only property.
  • The nodeTypeString property returns the node
    type in string form.
  • Syntax
  • xValueoXMLNode.nodeType
  • Valid values of nodeType property are
  • NODE_ELEMENT(1)
  • NODE_ATTRIBUTE(2)
  • NODE_TEXT(3)

45
Example of nodeType property
ltscript language"vbscript"gt Dim xmlDoc Dim
cNode Set xmlDoc CreateObject("microsoft.xmldom"
) xmlDoc.async False xmlDoc.load("candidate.xml"
) Set currNode xmlDoc.documentElement.childNodes
.item(0) MsgBox currNode.nodeType lt/scriptgt
46
value Property
  • It contains the value of the attribute.
  • The values to this property can be both read and
    written.
  • For the attributes with subnodes, the value
    property contains a string.
  • This string is the concatenated text of all
    subnodes, with character and general entity
    references replaced with their values
  • The string contents are unparsed.

Example Dim xmlDoc Dim xVar Set xmlDoc
CreateObject("Microsoft.xmldom") xmlDoc.async
False xmlDoc.load("candidate.xml") Set root
xmlDoc.documentElement myVar root.attributes.ite
m(0).value MsgBox xVar
47
text Property
  • It contains the text content of a node, or a
    concatenated text representing a node, and its
    declaration.

48
ownerDocument Property
  • It returns the root of the document that contains
    the specified node.
  • It is a read-only property.
  • All nodes are created in the context of a
    document, and the ownerDocument property is
    maintained until the node is added to another
    document.
  • For a node removed from a document, this property
    indicates the document in which the node was last
    included.

49
Example of ownerDocument Property
Example Dim xmlDoc Dim cNode Dim ownerDoc Set
xmlDoc CreateObject("Msxml2.DOMDocument") xmlDoc
.async False xmlDoc.load("candidate.xml") Set
cNode xmlDoc.documentElement.childNodes.item(0).
childNodes.item(1) Set ownerDoc
cNode.ownerDocument MsgBox ownerDoc.documentElemen
t.tagName
50
XMLDOMCDATASection Object
  • It represents the content of the CDATA section.
  • CDATA sections are used to escape blocks of text
    from being interpreted as markup language.
  • gt is the delimiter recognized in a CDATA
    section.
  • It has no unique properties and methods of its
    own.
  • It inherits the same objects and properties as
    the XMLDOMText object.

51
specified Property
  • It indicates if the node is explicitly specified
    or derived from a default value in the DTD or
    schema.
  • It returns a Boolean value.
  • It is a read-only property.
  • The value returned is true if the attribute is
    explicitly specified in the element.
  • The value is false if the attribute value comes
    from the DTD or schema.

52
Example of specified Property
Example ltscript language"vbscript"gt Dim
xmlDoc Dim oNamedNodeMap Dim cNode Dim myNode Set
xmlDoc CreateObject("microsoft.xmldom") xmlDoc.
async False xmlDoc.load("candidate.xml") Set
cNode xmlDoc.documentElement.childNodes.item(0)
Set oNamedNodeMap cNode.attributes Set myNode
oNamedNodeMap.item(0) MsgBox myNode.specified lt/sc
riptgt
53
splitText Method
  • It splits the text node into two text nodes, and
    inserts the new text node into the tree as
    sibling that immediately follows these nodes.
  • The parameter of the method is a long integer.
  • It specifies the number of characters at which to
    split this text node into two nodes, starting
    from zero.
  • The method returns an object of the new text
    node.
  • Syntax
  • oXMLDOMTextoXMLDOMText.splitText(parameter)

54
substringData Method
  • It retrieves a substring from the full string.
  • The first parameter is a long integer value,
    indicating the number of characters, from the
    beginning of the string from where the extraction
    has to start.
  • Zero value indicates that data is being copied
    from the start of the data.
  • The second parameter is also a long integer
    value, which indicates the number of characters
    to be retrieved.
  • Syntax
  • strValueoXMLDOMCharacterData.substringData(x,y)

55
Example of substringData Method
Example ltscript language"vbscript"gt Dim
xmlDoc Dim comment Dim xStr Set xmlDoc
CreateObject("microsoft.xmldom") xmlDoc.async
False xmlDoc.load("candidate.xml") Set comment
xmlDoc.createComment("Aptech Ltd.!") xStr
comment.subStringData(0,6) msgbox xStr lt/scriptgt
56
XMLDOMDocuemntType Object
  • It contains information about all the entities
    and notations in a declared document type
    declaration of the XML file.
  • Every DOMDocument object includes a doctype
    property that identifies the documents
    XMLDOMDocumentType object.
  • Its properties are read-only.
  • A DTD cannot be added to a DOMDocument.

57
entities and name Properties
  • entities Property It contains a list of both
    entities external and internal entities that are
    declared in the document type declaration.
  • Syntax
  • oXMLDOMNamedNodeMapoXMLDOMDocumentType.entities
  • name Property It contains the name of the
    document type
  • Syntax
  • sValueoXMLDOMDocuemntType.name

58
nodeTypeString Property
  • It returns the string version of the node type.
  • Syntax
  • strValueoXMLDOMNode.nodeTypeString

59
Example of nodeTypeString
Example ltscript language"vbscript"gt Dim
xmlDoc Dim cNode Set xmlDoc CreateObject("micros
oft.xmldom") xmlDoc.async False xmlDoc.load("can
didate.xml") Set cNode xmlDoc.documentElement.chi
ldNodes .item(0) MsgBox cNode.nodeTypeString
lt/scriptgt
msgbox displayed
60
replaceChild Method
61
removeChild Method
62
XMLDOMEntity Object
  • It represents a parsed or unparsed entity in the
    XML document.
  • It represents the entity itself rather than the
    entity declaration.
  • It represents an entity in the childNodes
    property of the docType (DTD) property of the
    DOMDocument.

63
notationName Property
  • It returns the notation name of the node.
  • It is a read-only property.
  • It contains the name of the notation for the
    unparsed entities.
  • It contains the empty string for the parsed
    entities.
  • The name of the entity is available using the
    nodeName property.
  • Syntax
  • sValueoXMLDOMEntity.notationName

64
prefix and publicId Property
  • prefix Property It returns the namespace prefix
    specified on the object.
  • An empty string is returned if no prefix is
    specified.
  • Syntax sValueoXMLDOMNode.prefix
  • publicId Property It contains the public
    identifier associated with the entity.
  • It contains an empty string if the public
    identifier is not specified.
  • It is a read-only property.
  • Syntax sValueoXMLDOMEntity.publicId

65
systemId Property
  • It contains the system identifier associated with
    the entity.
  • It contains the empty string if the system
    identifier is not specified.
  • Syntax
  • strValueoXMLDOMEntity.systemId

66
transformNode Method
  • It processes the node and its children using the
    supplied XSL style sheet, and returns the
    resulting transforming.
  • The parameter passed to the method is an object.
  • It is a valid XML document or DOM node, which
    consists of XSL elements that direct the
    transformation of this node.
  • It returns a string that contains the product of
    the transformation of the XML document based on
    the XSL style sheet.
  • Syntax sValue oXMLDOMNode.transformNode (x)

67
XMLDOMProcessingInstruction Object
  • It represents a processing instruction.
  • The content of the XMLDOMProcessingInstruction
    node comprises the entire content between the
    delimiters of the processing instruction (PI) and
    the end tag.
  • It is writeable, it can be added to a DOMDocument.

Example Dim xmlDoc Dim pi Set xmlDoc
CreateObject("Microsoft.xmldom") Set
pi xmlDoc.createProcessingInstruction(xml,vers
ion1.0) Msgbox pi.xml
68
parsed Property
  • It returns True if a node and all its
    descendants have been parsed and instantiated.
  • It returns False if any node remains to be
    parsed.
  • It is a read only property.
  • Syntax bValueoXMLDOMNode.parsed

69
Example of parsed property
Example ltscript languagevbscriptgt Dim
xmlDoc Dim root Set xmlDoc CreateObject("micros
oft.xmldom") xmlDoc.async True xmlDoc.load("cand
idate.xml") Set root xmlDoc.documentElement MsgB
ox root.parsed lt/scriptgt
70
cloneNode Method
Node and its clone
Node to be cloned
71
SAX
  • It stands for Simple API for XML.
  • It is a set of APIs for working with XML.
  • It does not consume much memory.
  • It is frequently used in server side applications
    as it is fast. Therefore, many client systems can
    be attached to the server.
  • It was initially defined for the Java programming
    language.
  • ContentHandler is the primary SAX interface.

72
SAX vs. DOM
  • These are the two most popular APIs used to
    manipulate an XML document.

It is possible to combine SAX and DOM within a
single system.
73
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com