Home  • Programming • Fltter

Scripting XML

1. Create a simple xml file name note.xml with the following:
<?xml version="1.0" encoding="iso-8859-1"?>
<notes>
   <note>
      <to>Imran</to>
     <from>Kashem</from>
     <heading>Reminder</heading>
     <body>Don't forget me this weekend!</body>
   </note>
   <note>
      <to>Jahid</to>
     <from>Ripon</from>
     <heading>Reminder</heading>
     <body>Good moring</body>
   </note>
</notes>
2. Create your output file name demo.htm having the following code:
<html>
<body>
<h1>Internal 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;

var to=document.getElementById("to");
var from=document.getElementById("from");
var message=document.getElementById("message");

record=0;
var dom_to=xmlDoc.getElementsByTagName("to")[record];
var dom_from=xmlDoc.getElementsByTagName("from")[record];
var dom_message=xmlDoc.getElementsByTagName("body")[record];


to.innerHTML=dom_to.childNodes[0].nodeValue;
from.innerHTML=dom_from.childNodes[0].nodeValue;
message.innerHTML=dom_message.childNodes[0].nodeValue;

</script>

</body>
</html>

Comments 0


Copyright © 2024. Powered by Intellect Software Ltd