XML DOM

 Prepared by: Pawan Dhawan
     Software Engineer
        Jan -30-2012
BOSS Webtech Private Limited
   www.bosswebtech.com
Points we will covered

•   What is XML DOM .
•   Its structure.
•   Its advantages and disadvantages
•   Its uses.
WHAT IS DOM ?
• DOM is the Document Object Model.
• The DOM is a W3C (World Wide Web
  Consortium) standard.
• The DOM defines a standard for accessing
  documents like XML and HTML:
• DOM is memory-based, this making it
  traversable and editable.
• DOM is not language-specific, nor is it
  platform-specific.
What is the XML DOM?

The XML DOM is:
  •   A standard object model for XML
  •   A standard programming interface for XML
  •   Platform- and language-independent
  •   A W3C standard
DOM Nodes

According to the DOM, everything in an XML
  document is a node.
The DOM says:
• The entire document is a document node
• Every XML element is an element node
• The text in the XML elements are text nodes
• Every attribute is an attribute node
• Comments are comment nodes
DOM Example
<?xml version="1.0" encoding="ISO-8859-1"?>
  <bookstore>
       <book category="cooking">
              <title lang="en">Everyday Italian</title>
              <author>Giada De Laurentiis</author>
              <year>2005</year>
              <price>30.00</price>
       </book>
       <book category="web" cover="paperback">
              <title lang="en">Learning XML</title>
              <author>Erik T. Ray</author>
              <year>2003</year>
              <price>39.95</price>
       </book>
  </bookstore>
The XML DOM Node Tree
XML DOM Properties

These are some typical DOM properties:
• x.nodeName - the name of x
• x.nodeValue - the value of x
• x.parentNode - the parent node of x
• x.childNodes - the child nodes of x
• x.attributes - the attributes nodes of x

Note: In the list above, x is a node object.
XML DOM Methods

• x.getElementsByTagName(name) - get all
  elements with a specified tag name
• x.appendChild(node) - insert a child node to x
• x.removeChild(node) - remove a child node
  from x

Note: In the list above, x is a node object.
Example
txt=xmlDoc.getElementsByTagName("title")[0].c
  hildNodes[0].nodeValue
• xmlDoc - the XML DOM object .
• getElementsByTagName("title")[0] - the first
  <title> element
• childNodes[0] - the first child of the <title>
  element (the text node)
• nodeValue - the value of the node (the text
  itself)
Traversing the Node Tree
<html>
   <head>
        <script type="text/javascript" src="loadxmlstring.js"></script>
   </head>
   <body>
        <script type="text/javascript">
                  text="<book>";
                  text=text+"<title>Everyday Italian</title>";
                  text=text+"<author>Giada De Laurentiis</author>";
                  text=text+"<year>2005</year>";
                  text=text+"</book>";
                    xmlDoc=loadXMLString(text);
                    // documentElement always represents the root node
                    x=xmlDoc.documentElement.childNodes;
                    for (i=0;i<x.length;i++)
                    {
                               document.write(x[i].nodeName);
                               document.write(": ");
                               document.write(x[i].childNodes[0].nodeValue);
                               document.write("<br />");
                    }
        </script>
   </body>
</html>
Out Put
title: Everyday Italian
author: Giada De Laurentiis
year: 2005
Advantages of XML DOM
• XML structure is traversable.
  Each node can be randomly accessed (one or
  more times) by traversing the tree.
• XML structure is modifiable.
  Since the XML structure is resident in memory,
  values can be added, changed, and removed.
• The DOM standard is maintained by the World
  Wide Web Consortium.
Disadvantages of XML DOM
• Resource intensive
  Since the XML structure is resident in memory,
  the larger the XML structure is, the more
  memory it will consume.
• Relative speed
  In comparison to SAX, DOM can be much
  slower due to its resource usage/ needs.
Uses of XML DOM
• The XML DOM defines a standard way for
  accessing and manipulating XML documents.
• The DOM presents an XML document as a
  tree-structure.
• Knowing the XML DOM makes working easier
  with XML.
ThankYou!!
About BOSS Webtech
o   BOSS Webtech is a process oriented design house specializing in web design,
    web development, backend web programming, mobile application
    development and other web and mobile related design and support services.

o   Recently launched BizPlus – Mobile based survey software. Check it more
    here http://bizplusonline.com/

o   More products here http://www.bosswebtech.com/products/products.html

    Contact BOSS Webtech
o   Call 831-998-9121 at US EST/CST/MST/PST Zone
    or email info@bosswebtech.com

XML Document Object Model (DOM)

  • 1.
    XML DOM Preparedby: Pawan Dhawan Software Engineer Jan -30-2012 BOSS Webtech Private Limited www.bosswebtech.com
  • 2.
    Points we willcovered • What is XML DOM . • Its structure. • Its advantages and disadvantages • Its uses.
  • 3.
    WHAT IS DOM? • DOM is the Document Object Model. • The DOM is a W3C (World Wide Web Consortium) standard. • The DOM defines a standard for accessing documents like XML and HTML: • DOM is memory-based, this making it traversable and editable. • DOM is not language-specific, nor is it platform-specific.
  • 4.
    What is theXML DOM? The XML DOM is: • A standard object model for XML • A standard programming interface for XML • Platform- and language-independent • A W3C standard
  • 5.
    DOM Nodes According tothe DOM, everything in an XML document is a node. The DOM says: • The entire document is a document node • Every XML element is an element node • The text in the XML elements are text nodes • Every attribute is an attribute node • Comments are comment nodes
  • 6.
    DOM Example <?xml version="1.0"encoding="ISO-8859-1"?> <bookstore> <book category="cooking"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="web" cover="paperback"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore>
  • 7.
    The XML DOMNode Tree
  • 8.
    XML DOM Properties Theseare some typical DOM properties: • x.nodeName - the name of x • x.nodeValue - the value of x • x.parentNode - the parent node of x • x.childNodes - the child nodes of x • x.attributes - the attributes nodes of x Note: In the list above, x is a node object.
  • 9.
    XML DOM Methods •x.getElementsByTagName(name) - get all elements with a specified tag name • x.appendChild(node) - insert a child node to x • x.removeChild(node) - remove a child node from x Note: In the list above, x is a node object.
  • 10.
    Example txt=xmlDoc.getElementsByTagName("title")[0].c hildNodes[0].nodeValue •xmlDoc - the XML DOM object . • getElementsByTagName("title")[0] - the first <title> element • childNodes[0] - the first child of the <title> element (the text node) • nodeValue - the value of the node (the text itself)
  • 11.
    Traversing the NodeTree <html> <head> <script type="text/javascript" src="loadxmlstring.js"></script> </head> <body> <script type="text/javascript"> text="<book>"; text=text+"<title>Everyday Italian</title>"; text=text+"<author>Giada De Laurentiis</author>"; text=text+"<year>2005</year>"; text=text+"</book>"; xmlDoc=loadXMLString(text); // documentElement always represents the root node x=xmlDoc.documentElement.childNodes; for (i=0;i<x.length;i++) { document.write(x[i].nodeName); document.write(": "); document.write(x[i].childNodes[0].nodeValue); document.write("<br />"); } </script> </body> </html>
  • 12.
    Out Put title: EverydayItalian author: Giada De Laurentiis year: 2005
  • 13.
    Advantages of XMLDOM • XML structure is traversable. Each node can be randomly accessed (one or more times) by traversing the tree. • XML structure is modifiable. Since the XML structure is resident in memory, values can be added, changed, and removed. • The DOM standard is maintained by the World Wide Web Consortium.
  • 14.
    Disadvantages of XMLDOM • Resource intensive Since the XML structure is resident in memory, the larger the XML structure is, the more memory it will consume. • Relative speed In comparison to SAX, DOM can be much slower due to its resource usage/ needs.
  • 15.
    Uses of XMLDOM • The XML DOM defines a standard way for accessing and manipulating XML documents. • The DOM presents an XML document as a tree-structure. • Knowing the XML DOM makes working easier with XML.
  • 16.
  • 17.
    About BOSS Webtech o BOSS Webtech is a process oriented design house specializing in web design, web development, backend web programming, mobile application development and other web and mobile related design and support services. o Recently launched BizPlus – Mobile based survey software. Check it more here http://bizplusonline.com/ o More products here http://www.bosswebtech.com/products/products.html Contact BOSS Webtech o Call 831-998-9121 at US EST/CST/MST/PST Zone or email [email protected]