XML EXTERNAL VIEW

Examples of XML files in a web page

Given the following XML file "messages.xml"

<?xml version="1.0" encoding="UTF-8"?> <message>     <message>          <to>John</to>          <from>Peter</from>          <title>Meeting</title>          <text>Remember out meeting</text>     </message> </message>

Now, let's load or display the above XML in the following website "messages.htm"

<html> <body> <h1>XML DOM</h1> <div> <b>To:</b> <span id="to"></span> <b>From:</b> <span id="from"></span> <b>Title:</b> <span id="title"></span> <b>Text:</b> <span id="text"></span> </div> <script type="text/javascript"> if (window.XMLHttpRequest) { // Firefox, Chrome, IE7+, Opera, Safari xmlhttp=new XMLHttpRequest(); } else { // Para IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.open("GET","messages.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("title").innerHTML= xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue; document.getElementById("text").innerHTML= xmlDoc.getElementsByTagName("text")[0].childNodes[0].nodeValue; </script> </body> </html>