Xml dom & sax by bhavsingh maloth
-
Upload
bhavsingh-maloth -
Category
Education
-
view
320 -
download
4
description
Transcript of Xml dom & sax by bhavsingh maloth
Document Object Model
Slide
2
Exercises
Validate and tranform an XML document
Working with the XML DOM
Introducing the DOM Objects
Introduction
MBSMBS
Introducing the W3C DOM DOM Implementation
Document Object Model
Slide
3
MBSMBS
XML Document Object Model (DOM)◦ W3C standard recommendation◦ Build tree structure in memory for XML
documents◦ Provides an Application Programming Interface
(API) for dynamically accessing and manipulating a document
◦ DOM-based parsers parse these structures Exist in several languages (Java, C, C++, Python,
Perl, etc.)
Document Object Model
Slide
4
MBSMBS
Tree Structure of a Document:◦Node is the basic building block of the DOM tree
structure.◦Nodes represent elements, attributes, content,
comments, etc.
Document Object Model
Slide
5
MBSMBS
◦ Example
<?xml version = "1.0"?><message from = "Paul" to = "Tem"> <body>Hi, Tim!</body></message>
Node created for element message Element message has child node for body element Element body has child node for text "Hi, Tim!" Attributes from and to also have nodes in tree
Document Object Model
Slide
6
MBSMBS
XML Document
MSXML Library or other libraries
Parsed Document
Parser DOM Tree
Root Child
Text Child
Text
Application
Document Object Model
Slide
7
The following figure represents how a DOM tree is used by applications to access data:
MBSMBS
DOM-based parsers◦ Sun Microsystem’s JAXP◦ Apache Xerces◦ XML4J◦ DOM4J◦ JDOM◦ Microsoft’s msxml◦ 4DOM◦ XML::DOM◦ …
Document Object Model
Slide
8
MBSMBS
Class/Interface Description
Document interface Represents the XML document’s top-level node, which provides access to all the document’s nodes—including the root element.
Node interface Represents an XML document node.
NodeList interface Represents a read-only list of Node objects.
Element interface Represents an element node. Derives from Node.
Attr interface Represents an attribute node. Derives from Node.
CharacterData interface Represents character data. Derives from Node.
Text interface Represents a text node. Derives from CharacterData.
Comment interface Represents a comment node. Derives from CharacterData.
ProcessingInstruction interface
Represents a processing instruction node. Derives from Node.
CDATASection interface Represents a CDATA section. Derives from Text.
Document Object Model
Slide
9
MBSMBS
Method Name Description
createElement Creates an element node.
createAttribute Creates an attribute node.
createTextNode Creates a text node.
createComment Creates a comment node.
createProcessingInstruction Creates a processing instruction node.
createCDATASection Creates a CDATA section node.
getDocumentElement Returns the document’s root element.
appendChild Appends a child node.
getChildNodes Returns the child nodes.
Document Object Model
Slide 10
MBSMBS
Following are the associated properties for the Document object methods:◦ childNodes◦ documentElement◦ firstChild◦ lastChild◦ parseError◦ validateOnParse
Document Object Model
Slide 11
MBSMBS
Method Name Description
appendChild Appends a child node.
cloneNode Duplicates the node.
getAttributes Returns the node’s attributes.
getChildNodes Returns the node’s child nodes.
getNodeName Returns the node’s name.
getNodeType Returns the node’s type (e.g., element, attribute, text, etc.).
getNodeValue Returns the node’s value.
getParentNode Returns the node’s parent.
hasChildNodes Returns true if the node has child nodes.
removeChild Removes a child node from the node.
replaceChild Replaces a child node with another node.
setNodeValue Sets the node’s value.
insertBefore Appends a child node in front of a child node.
Document Object Model
Slide 12
MBSMBS
Following are the associated properties for the Node object methods: ◦ nodeName ◦ nodeType ◦ nodeValue ◦ childNodes◦ firstChild ◦ lastChild◦ text
Document Object Model
Slide 13
MBSMBS
Node Type Description
Node.ELEMENT_NODE Represents an element node.
Node.ATTRIBUTE_NODE Represents an attribute node.
Node.TEXT_NODE Represents a text node.
Node.COMMENT_NODE Represents a comment node.
Node.PROCESSING_INSTRUCTION_NODE Represents a processing instruction node.
Node.CDATA_SECTION_NODE Represents a CDATA section node.
Document Object Model
Slide 14
MBSMBS
Method Name Description
getAttribute Returns an attribute’s value.
getTagName Returns an element’s name.
removeAttribute Removes an element’s attribute.
setAttribute Sets an attribute’s value.
Document Object Model
Slide 15
MBSMBS
<?xml version="1.0"?><article><title>XML Demo</title><date>August 22, 2007</date><author>
<fname>Nguyen Van</fname><lname>Teo</lname>
</author><summary>XML is pretty easy.</summary><content>Once you have mastered HTML, XML is easily
learned. You must remember that XML is not for displaying information but for managing information.
</content></article>
Document Object Model
Slide 16
MBSMBS
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head> <title>A DOM Example</title></head>
<body>
<script type = "text/javascript" language = "JavaScript">
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.load("article.xml");
var element=xmlDoc.documentElement;
Document Object Model
Slide 17
Element script allows for including scripting code
Instantiate Microsoft XML
DOM object
Load article.xml into memory; msxml parses article.xml and
stores it as tree structure
MBSMBS
var element=xmlDoc.documentElement;// get the root element
document.writeln( "<p>Here is the root node of the document:" ); document.writeln( "<strong>" + element.nodeName + "</strong>" );
document.writeln( "<br>The following are its child elements:" ); document.writeln( "</p><ul>" );
// traverse all child nodes of root element for ( i = 0; i < element.childNodes.length; i++ ) { var curNode = element.childNodes.item(i); // print node name of each child element document.writeln( "<li><strong>" + curNode.nodeName + "</strong></li>" ); }
document.writeln( "</ul>" );
// get the first child node of root element var currentNode = element.firstChild;
Document Object Model
Slide 18
Assign article as root element
Place root element’s name in element strong and write it to browser
Assign index to each child node of root node
Retrieve root node’s first child node (title)
MBSMBS
document.writeln( "<p>The first child of root node is:" ); document.writeln( "<strong>" + currentNode.nodeName + "</strong>" ); document.writeln( "<br>whose next sibling is:" ); // get the next sibling of first child var nextSib = currentNode.nextSibling; document.writeln( "<strong>" + nextSib.nodeName + "</strong>." ); document.writeln( "<br>Value of <strong>" + nextSib.nodeName + "</strong> element is:" );
var value = nextSib.firstChild;
// print the text value of the sibling document.writeln( "<em>" + value.nodeValue + "</em>" ); document.writeln( "<br>Parent node of " ); document.writeln( "<strong>" + nextSib.nodeName + "</strong> is:" ); document.writeln( "<strong>" + nextSib.parentNode.nodeName + "</strong>.</p>" );
</script></body></html>
Document Object Model
Slide 19
Siblings are nodes at same level in document (e.g., title, date,
author, summary and content)
Get first child’s next sibling (date)
Get first child of date
Get parent of date (article)
MBSMBS
Document Object Model
Slide 20
MBSMBS
Validate XML document with DTD Read XML document
Document Object Model
Slide 21
MBSMBS
To be continued…
Document Object Model
Slide 22
MBSMBS
XML How to program http://www.w3.org XML tutorial
http://www.w3schools.com/w3c/
Document Object Model
Slide 23
MBSMBS
Feel free to post questions at http://yht4ever.blogspot.com
or email to: [email protected] or [email protected]
Document Object Model
Slide 24
MBSMBS
MBSMBS