Problem using XML Code embedded in an HTML file.

Hi,

I encountered runtime error :"Object required".

<html>
<!-- This is the first example of how to embed XML code into HTML files.-->
<head>
<title>Second example of XML Code Islands</title>
<script language="JavaScript">
<!--
function StartUp()
{
  var RootElement1=message.documentElement;
  var nodecount=RootElement1.childNodes.length;  // runtime error - "object
required"
  var loopindex;
  var CurrentNode;
  var ErrorMessage="Error, data not defined";

// error handling
  if(RootElement1==null)
  {
   alert("The root is null.");
   document.write("<B><BIG>The data of the XML cannot be
accessed</BIG></B><BR>");
  }
  else
  {
  
  for(loopindex=0;loopindex<nodecount;loopindex++)
  {
   CurrentNode=RootElement1.childNodes.item(loopindex);
   switch(CurrentNode.NodeName)
     {
      case "TO":
         todata.innerText=CurrentNode.text;
         break;
      case "FROM":
         fromdata.innerText=CurrentNode.text;
         break;
      case "SUBJECT":
         subjectdata.innerText=CurrentNode.text;
         break;
      case "BODY":
         bodydata.innerText=CurrentNode.text;
         break;
      default:
         todata.innerText=ErrorMessage;                 
         fromdata.innerText=ErrorMessage;
         subjectdata.innerText=ErrorMessage;
         bodysata.innerText=ErrorMessage;
      }
     } 
    }
   }
//-->
</script>

</head>

<body bgcolor="ffffff" onLoad="StartUp()" >
<XML ID=message>
<MESSAGE>
  <TO>Student</TO>
  <FROM>Author</FORM>
  <SUBJECT>XML Code Islands</SUBJECT>
  <BODY>This is an example of XML code embedded inside HTML code</BODY>
</MESSAGE>
</XML>

<DIV ID=to STYLE="font-weight:bold;color:blue">
TO:
<SPAN ID=todata</SPAN>
</DIV>
<DIV ID=from STYLE="font-weight:bold;color:teal">
FROM:
<SPAN ID=fromdata</SPAN>
</DIV>
<DIV ID=subject STYLE="font-weight:bold;color:red">
SUBJECT:
<SPAN ID=subjectdata</SPAN>
</DIV>
<DIV ID=body STYLE="font-weight:bold;color:magenta">
BODY:
<SPAN ID=bodydata</SPAN>
</DIV>
</body>
</html>


The error is caused by this statement:-

var nodecount=RootElement1.childNodes.length;

Can anyone help ?

Thks.
Simon

Received on Wednesday, 11 October 2000 21:00:55 UTC