I am trying to learn XML from a "Teach yourself" book. Below I have ran into a problem that is making me pull my hair out and I do not have much left. I have a HTML doc that should pull data from a external xml file in the same directory, however all I keep getting is "The root node is null" (which in the code I presume goes back to the rootelement which is set to doc1 which in turn is loading the test.xml file). I am not sure if my problem lies in the HTML, XML or script.
Test.xml
Test.html
Test.xml
Code:
<xml version="1.0"?>
<Book xmlns:dt="urn:schemas-microsoft-com:datatypes">
<Author dt:dt="string">Good Writer</Author>
<Title dt:dt="string">Good Book</Title>
<PriceHardBack dt:dt="number">12.95</PriceHardBack>
<PricePaperBack dt:dt="number">2.95</PricePaperBack>
<Rank dt:dt="int">32</Rank>
<Grade dt:dt="char">A</Grade>
<InStock dt:dt="boolean">0</InStock>
<PublicationDate dt:dt="date">2000-03-30</PublicationDate>
<PreciseMoment dt:dt="dateTime.tz">2000-03-30T12:18</PreciseMoment>
<OneSignedInteger dt:dt="dt=i1">12</OneSignedInteger>
<TwoSignedInteger dt:dt="dt=i2">-12</TwoSignedInteger>
<OneUnsignedInteger dt:dt="dt=ui1">12</OneUnsignedInteger>
<TwoUnsignedInteger dt:dt="dt=ui2">12</TwoUnsignedInteger>
<OneRealNumber dt:dt="r4">12345.67891</OneRealNumber>
</Book>
Test.html
Code:
<html>
<head>
<title>USING THE ARRAY PROPERITES OF THE NODELIST TO READ AN XML FILE</title>
<SCRIPT LANGUAGE="JavaScript">
<!--
var RootElement1;
var nodecount;
var xmlDoc1=new ActiveXObject("microsoft.xmldom");
xmlDoc1.load("test.xml");
function StartUp()
{
if(xmlDoc1.readyState=="4")
{
StartLoading();
}
else
{
alert("Process could not start");
}
}
function StartLoading()
{
var loopindex;
RootElement1=xmlDoc1.documentElement;
if(RootElement1==null)
{
alert("The root node is null");
document.write("<B><BIG>The data of the XML file cannot be accessed.</BIG></B>");
}
else
{
nodecount=RootElement1.childNodes.length;
for(loopindex=0;loopindex<nodecount;loopindex++)
{
document.write(RootElement1.childNodes.item(loopindex).nodeName+": ");
document.write(RootElement1.childNodes.item(loopindex).text+"<br>");
document.write("The data type is " +RootElement1.childNodes.item(loopindex).dataType+"<br>");
str1="The value of the nodeTypedVaule property is ";
document.write("str1+RootElement1.childNodes.item(loopindex).nodeTypedValue+<br><br>");
}
}
}
// -->
</script>
</head>
<BODY onLoad="StartUp()">
</BODY>
</html>