Document Object Model XML http://yht4ever.blogspot.com [email_address] B070066 - NIIT Quang Trung 08/2007
Contents Exercises Validate and tranform an XML document Working with the XML DOM Introducing the DOM Objects Introduction
Introduction <ul><li>Introducing the W3C DOM </li></ul><ul><li>DOM Implementation </li></ul>
Introducing the W3C DOM <ul><li>XML Document Object Model (DOM) </li></ul><ul><ul><li>W3C standard recommendation </li></u...
Introducing the W3C DOM (cont.) <ul><li>Tree   Structure of a Document: </li></ul><ul><ul><li>Node is the basic building b...
Introducing the W3C DOM (cont.) <ul><ul><li>Example </li></ul></ul><ul><ul><li><?xml version =   &quot;1.0&quot; ?> <messa...
Introducing the W3C DOM (cont.) The following figure represents how a DOM tree is used by applications to access data:
DOM Implementation <ul><li>DOM-based parsers </li></ul><ul><ul><li>Sun Microsystem’s JAXP </li></ul></ul><ul><ul><li>Apach...
DOM classes and interfaces
Some  Document  methods.
Some  Document  properties <ul><li>Following are the associated properties for the Document object methods: </li></ul><ul>...
Node  methods
Some  Node  properties <ul><li>Following are the associated properties for the Node object methods:   </li></ul><ul><ul><l...
Some  Node  types
Element  methods
Example <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><article> </li></ul><ul><li><title>XML Demo</title> </li...
Example (cont.) <ul><li><!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; </li></ul><ul><li>&quot;http://www.w3....
Example (cont.) <ul><li>var element=xmlDoc.documentElement;// get the root element </li></ul><ul><li>document.writeln(  </...
Example (cont.) <ul><li>document.writeln( &quot;<p>The first child of root node is:&quot; ); </li></ul><ul><li>document.wr...
Example (cont.)
Examlple (cont.) <ul><li>Validate XML document with DTD </li></ul><ul><li>Read XML document </li></ul>
To be continued… <ul><li>To be continued… </li></ul>
Reference <ul><li>XML How to program </li></ul><ul><li>http://www.w3.org </li></ul><ul><li>XML tutorial http://www.w3schoo...
Q&A <ul><li>Feel free to post questions at  http://yht4ever.blogspot.com </li></ul><ul><li>or email to:  [email_address]  ...
http://yht4ever.blogspot.com Thank You !
Upcoming SlideShare
Loading in …5
×

Document Object Model

3,291 views
3,164 views

Published on

[root@localhost ~]# vi diemthi

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,291
On SlideShare
0
From Embeds
0
Number of Embeds
40
Actions
Shares
0
Downloads
206
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Document Object Model

  1. 1. Document Object Model XML http://yht4ever.blogspot.com [email_address] B070066 - NIIT Quang Trung 08/2007
  2. 2. Contents Exercises Validate and tranform an XML document Working with the XML DOM Introducing the DOM Objects Introduction
  3. 3. Introduction <ul><li>Introducing the W3C DOM </li></ul><ul><li>DOM Implementation </li></ul>
  4. 4. Introducing the W3C DOM <ul><li>XML Document Object Model (DOM) </li></ul><ul><ul><li>W3C standard recommendation </li></ul></ul><ul><ul><li>Build tree structure in memory for XML documents </li></ul></ul><ul><ul><li>Provides an Application Programming Interface (API) for dynamically accessing and manipulating a document </li></ul></ul><ul><ul><li>DOM-based parsers parse these structures </li></ul></ul><ul><ul><ul><li>Exist in several languages (Java, C, C++, Python, Perl, etc.) </li></ul></ul></ul>
  5. 5. Introducing the W3C DOM (cont.) <ul><li>Tree Structure of a Document: </li></ul><ul><ul><li>Node is the basic building block of the DOM tree structure. </li></ul></ul><ul><ul><li>Nodes represent elements, attributes, content, comments, etc. </li></ul></ul>
  6. 6. Introducing the W3C DOM (cont.) <ul><ul><li>Example </li></ul></ul><ul><ul><li><?xml version = &quot;1.0&quot; ?> <message from = &quot;Paul&quot; to = &quot;Tem&quot; > <body> Hi, Tim! </body> </message> </li></ul></ul><ul><ul><ul><li>Node created for element message </li></ul></ul></ul><ul><ul><ul><ul><li>Element message has child node for body element </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Element body has child node for text &quot; Hi, Tim! &quot; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Attributes from and to also have nodes in tree </li></ul></ul></ul></ul>
  7. 7. Introducing the W3C DOM (cont.) The following figure represents how a DOM tree is used by applications to access data:
  8. 8. DOM Implementation <ul><li>DOM-based parsers </li></ul><ul><ul><li>Sun Microsystem’s JAXP </li></ul></ul><ul><ul><li>Apache Xerces </li></ul></ul><ul><ul><li>XML4J </li></ul></ul><ul><ul><li>DOM4J </li></ul></ul><ul><ul><li>JDOM </li></ul></ul><ul><ul><li>Microsoft’s msxml </li></ul></ul><ul><ul><li>4DOM </li></ul></ul><ul><ul><li>XML::DOM </li></ul></ul><ul><ul><li>… </li></ul></ul>
  9. 9. DOM classes and interfaces
  10. 10. Some Document methods.
  11. 11. Some Document properties <ul><li>Following are the associated properties for the Document object methods: </li></ul><ul><ul><li>childNodes </li></ul></ul><ul><ul><li>documentElement </li></ul></ul><ul><ul><li>firstChild </li></ul></ul><ul><ul><li>lastChild </li></ul></ul><ul><ul><li>parseError </li></ul></ul><ul><ul><li>validateOnParse </li></ul></ul>
  12. 12. Node methods
  13. 13. Some Node properties <ul><li>Following are the associated properties for the Node object methods: </li></ul><ul><ul><li>nodeName </li></ul></ul><ul><ul><li>nodeType </li></ul></ul><ul><ul><li>nodeValue </li></ul></ul><ul><ul><li>childNodes </li></ul></ul><ul><ul><li>firstChild </li></ul></ul><ul><ul><li>lastChild </li></ul></ul><ul><ul><li>text </li></ul></ul>
  14. 14. Some Node types
  15. 15. Element methods
  16. 16. Example <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><article> </li></ul><ul><li><title>XML Demo</title> </li></ul><ul><li><date>August 22, 2007</date> </li></ul><ul><li><author> </li></ul><ul><li><fname>Nguyen Van</fname> </li></ul><ul><li><lname>Teo</lname> </li></ul><ul><li></author> </li></ul><ul><li><summary>XML is pretty easy.</summary> </li></ul><ul><li><content>Once you have mastered HTML, XML is easily </li></ul><ul><li>learned. You must remember that XML is not for </li></ul><ul><li>displaying information but for managing information. </li></ul><ul><li></content> </li></ul><ul><li></article> </li></ul>
  17. 17. Example (cont.) <ul><li><!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; </li></ul><ul><li>&quot;http://www.w3.org/TR/html4/strict.dtd&quot;> </li></ul><ul><li><html> </li></ul><ul><li><head> </li></ul><ul><li><title>A DOM Example</title> </li></ul><ul><li></head> </li></ul><ul><li><body> </li></ul><ul><li><script type = &quot;text/javascript&quot; language = &quot;JavaScript&quot;> </li></ul><ul><li>var xmlDoc=new ActiveXObject(&quot;Microsoft.XMLDOM&quot;); </li></ul><ul><li>xmlDoc.load(&quot;article.xml&quot;); </li></ul><ul><li>var element=xmlDoc.documentElement; </li></ul>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
  18. 18. Example (cont.) <ul><li>var element=xmlDoc.documentElement;// get the root element </li></ul><ul><li>document.writeln( </li></ul><ul><li>&quot;<p>Here is the root node of the document:&quot; ); </li></ul><ul><li>document.writeln( &quot;<strong>&quot; + element.nodeName </li></ul><ul><li>+ &quot;</strong>&quot; ); </li></ul><ul><li>document.writeln( </li></ul><ul><li>&quot;<br>The following are its child elements:&quot; ); </li></ul><ul><li>document.writeln( &quot;</p><ul>&quot; ); </li></ul><ul><li>// traverse all child nodes of root element </li></ul><ul><li>for ( i = 0; i < element.childNodes.length; i++ ) { </li></ul><ul><li>var curNode = element.childNodes.item(i); </li></ul><ul><li>// print node name of each child element </li></ul><ul><li>document.writeln( &quot;<li><strong>&quot; + curNode.nodeName </li></ul><ul><li>+ &quot;</strong></li>&quot; ); </li></ul><ul><li>} </li></ul><ul><li>document.writeln( &quot;</ul>&quot; ); </li></ul><ul><li>// get the first child node of root element </li></ul><ul><li>var currentNode = element.firstChild; </li></ul>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 )
  19. 19. Example (cont.) <ul><li>document.writeln( &quot;<p>The first child of root node is:&quot; ); </li></ul><ul><li>document.writeln( &quot;<strong>&quot; + currentNode.nodeName </li></ul><ul><li>+ &quot;</strong>&quot; ); </li></ul><ul><li>document.writeln( &quot;<br>whose next sibling is:&quot; ); </li></ul><ul><li>// get the next sibling of first child </li></ul><ul><li>var nextSib = currentNode.nextSibling; </li></ul><ul><li>document.writeln( &quot;<strong>&quot; + nextSib.nodeName </li></ul><ul><li>+ &quot;</strong>.&quot; ); </li></ul><ul><li>document.writeln( &quot;<br>Value of <strong>&quot; + nextSib.nodeName </li></ul><ul><li>+ &quot;</strong> element is:&quot; ); </li></ul><ul><li>var value = nextSib.firstChild; </li></ul><ul><li>// print the text value of the sibling </li></ul><ul><li>document.writeln( &quot;<em>&quot; + value.nodeValue + &quot;</em>&quot; ); </li></ul><ul><li>document.writeln( &quot;<br>Parent node of &quot; ); </li></ul><ul><li>document.writeln( &quot;<strong>&quot; + nextSib.nodeName </li></ul><ul><li>+ &quot;</strong> is:&quot; ); </li></ul><ul><li>document.writeln( &quot;<strong>&quot; + nextSib.parentNode.nodeName </li></ul><ul><li>+ &quot;</strong>.</p>&quot; ); </li></ul><ul><li></script> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>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 )
  20. 20. Example (cont.)
  21. 21. Examlple (cont.) <ul><li>Validate XML document with DTD </li></ul><ul><li>Read XML document </li></ul>
  22. 22. To be continued… <ul><li>To be continued… </li></ul>
  23. 23. Reference <ul><li>XML How to program </li></ul><ul><li>http://www.w3.org </li></ul><ul><li>XML tutorial http://www.w3schools.com/w3c/ </li></ul>
  24. 24. Q&A <ul><li>Feel free to post questions at http://yht4ever.blogspot.com </li></ul><ul><li>or email to: [email_address] or [email_address] </li></ul>
  25. 25. http://yht4ever.blogspot.com Thank You !

×