XML DOM

Document object Model (DOM) is foundation of XML. It is tree based information called nodes; it is a way of describing those nodes and the relationship between them. A DOM is said to be tree based. A DOM document is a collection of nodes organized in hierarchy. The hierarchy allows the developers to navigate through the tree to looking for specific information. In XML DOM it provides an API to developer to add, edit, move or remove nodes in tree at any place in order to create the application. Example

<!DOCTYPE html>  
<html>  
    <body>  
        <h1> tutorialandexample </h1>  
        <div>  
            <b>Name:</b> <span id="name"></span><br>  
            <b>Company:</b> <span id="company"></span><br>  
            <b>Phone:</b> <span id="phone"></span><br>  
        </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?,"/xml/address.xml",false);  
            xmlhttp.send();  
            xmlDoc=xmlhttp.responseXML;  
            document.getElementById("name").inner.HTML=  
            xmlDoc.getElementsByTagName("name") [0].childnodes[0].nodevalue;  
            document.getElementById("company").innerHTML=  
            xmlDoc.getElementsByTagName("company")[0].childNodes[0].nodeValue;  
            document.getElementById(?phone?).innerHTML=  
            xmlDoc.getElementsByTagName("phone") [0].childnodes[0].nodevalue;  
        </script>  
    </body>  
</html>
In above example (sample.html) parses an XML document ("address.html") into a XML DOM object and then extracts some information from it with JavaScript. Contents of address.xml
<?xml version="1.0"?>  
<contact-info>  
    <name>Ajay Singh</name>  
    <company> SSSIT Pvt.Ltd</company>  
    <phone>0120-4256464, 9990449935</phone>  
</contact-info>

Program execute

Keep these two files sample.htm and address.xml in same directory and execute the sample.html by opening it in browser and see the output.



ADVERTISEMENT
ADVERTISEMENT