DOM is an acronym stands for Document Object Model. It defines a standard way to access and manipulate documents. The Document Object Model (DOM) is a programming API for HTML and XML documents. It defines the logical structure of documents and the way a document is accessed and manipulated.
As a W3C specification, one important objective for the Document Object Model is to provide a standard programming interface that can be used in a wide variety of environments and applications. The Document Object Model can be used with any programming language.
XML DOM defines a standard way to access and manipulate XML documents.
The XML DOM makes a tree-structure view for an XML document.
We can access all elements through the DOM tree.
We can modify or delete their content and also create new elements. The elements, their content (text and attributes) are all known as nodes.
For example, consider this table, taken from an HTML document:
<TABLE> <ROWS> <TR> <TD>A</TD> <TD>B</TD> </TR> <TR> <TD>C</TD> <TD>D</TD> </TR> </ROWS> </TABLE>
The Document Object Model represents this table like this:
Let's take an example to show how an XML document ("note.xml") is parsed into an XML DOM object.
This example parses an XML document (note.xml) into an XML DOM object and extracts information from it with JavaScript.
Let's see the XML file that contains message.
note.xml
<?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>sonoojaiswal@rookienerd.com</to> <from>vimal@rookienerd.com</from> <body>Hello XML DOM</body> </note>
Let's see the HTML file that extracts the data of XML document using DOM.
xmldom.html
<!DOCTYPE html> <html> <body> <h1>Important Note</h1> <div> <b>To:</b> <span id="to"></span><br> <b>From:</b> <span id="from"></span><br> <b>Message:</b> <span id="message"></span> </div> <script> if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.open("GET","note.xml",false); xmlhttp.send(); xmlDoc=xmlhttp.responseXML; document.getElementById("to").innerHTML= xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue; document.getElementById("from").innerHTML= xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue; document.getElementById("message").innerHTML= xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue; </script> </body> </html>
Output:
This example parses an XML string into an XM DOM object and then extracts some information from it with a JavaScript.
Let's see the HTML file that extracts the data of XML string using DOM.
xmldom.html
<!DOCTYPE html> <html> <body> <h1>Important Note2</h1> <div> <b>To:</b> <span id="to"></span><br> <b>From:</b> <span id="from"></span><br> <b>Message:</b> <span id="message"></span> </div> <script> txt1=""; txt=txt1+txt2+txt3+txt4+txt5; if (window.DOMParser) { parser=new DOMParser(); xmlDoc=parser.parseFromString(txt,"text/xml"); } else // Internet Explorer { xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async=false; xmlDoc.loadXML(txt); } document.getElementById("to").innerHTML= xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue; document.getElementById("from").innerHTML= xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue; document.getElementById("message").innerHTML= xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue; </script> </body> </html>Note"; txt2="Sania Mirza "; txt3="Serena William "; txt4="Don't forget me this weekend!"; txt5="
Output: