#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Posts
    20
    Rep Power
    0

    DOM Question: "Undefined Doc" error


    Hi I'm new to dom and

    I'm getting this error on "line 54 doc undefined" I can't figure out what is wrong. Below is my xml and html respectively:
    I also attached a zip of my code because I cannot post the whole code on this message.

    xml:

    <?xml version="1.0"?>
    <?xml:stylesheet type="text/xsl" href="mainPage_xsl.xsl" ?>

    <computerStore storeName="Mac Store">
    <model picture="imac.jpg">
    <name>iMac</name>
    <package>A</package>
    <cpu>600MHz PowerPC G3</cpu>
    <memory>128MB SDRAM</memory>
    <hd>40GB Ultra ATA drive</hd>
    <drive>CD-ROM</drive>
    <video>Rage 128 Ultra graphics</video>
    <modem>56K internal modem</modem>
    <price>799</price>
    <tax>0.14</tax>
    <shipping>20</shipping>
    </model>
    .....

    html file:

    <HTML><HEAD><TITLE>XML DOM Scripting</TITLE>
    <XML ID="gigi" SRC="macStore_xml.xml"></XML>
    <SCRIPT>


    function Compute_Stat(Doc, Which)
    {
    var Total = 0;
    var StudentCount = 0;
    var Marks = 0;

    var Highest = 0;
    var Lowest = 999;
    var HighestLastName = "";
    var HighestFirstName = "";
    var LowestLastName = "";
    var LowestFirstName = "";

    var Computer = Doc.documentElement.firstChild;
    /*while (Student != null)
    {
    Marks = parseInt(Student.selectSingleNode("Marks").text)
    LastName = Student.selectSingleNode("Last").text
    FirstName = Student.selectSingleNode("First").text



    if (Marks > Highest)
    {
    Highest = Marks;
    HighestFirstName = FirstName;
    HighestLastName = LastName;
    }
    if (Marks < Lowest)
    {
    Lowest = Marks;
    LowestFirstName = FirstName;
    LowestLastName = LastName;
    }



    Total += Marks
    Student = Student.nextSibling;
    StudentCount++;
    }
    if (Which == "avg")
    return Total/StudentCount;
    else if (Which == "high")
    return HighestFirstName + " " + HighestLastName + " Marks " + Highest;
    else if (Which == "low")
    return LowestFirstName + " " + LowestLastName + " Marks " + Lowest;
    */}

    var Student = Doc.documentElement.firstChild;
    var NameArray = new Array();
    var CPUArray = new Array();
    var MemoryArray = new Array();
    var HDArray = new Array();
    var DriveArray = new Array();
    var VideoArray = new Array();
    var ModemArray = new Array();
    var PriceArray = new Array();

    function LoadArrays(Doc)
    {
    var Count = 0;
    var LastName;

    var Computer = Doc.documentElement.firstChild;
    while (Computer != null)
    {
    Name = Computer.selectSingleNode("name").text
    CPU = Computer.selectSingleNode("cpu").text
    Memory= Computer.selectSingleNode("memory").text
    HD = Computer.selectSingleNode("hd").text
    Drive= Computer.selectSingleNode("drive").text
    Video= Computer.selectSingleNode("video").text
    Modem= Computer.selectSingleNode("modem").text
    Price = parseInt(Computer.selectSingleNode("price").text)

    NameArray[Count] = "<TD>" + Name + "</TD>"
    NameArray[Count] += "<TD>" + CPU + "</TD>"
    NameArray[Count] += "<TD>" + Memory + "</TD>"
    NameArray[Count] += "<TD>" + HD + "</TD>"
    NameArray[Count] += "<TD>" + Drive + "</TD>"
    NameArray[Count] += "<TD>" + Video + "</TD>"
    NameArray[Count] += "<TD>" + Modem + "</TD>"
    NameArray[Count] += "<TD>" + Price + "</TD>"

    CPUArray[Count] = "<TD>" + CPU + "</TD>"
    CPUArray[Count] += "<TD>" + Name + "</TD>"
    CPUArray[Count] += "<TD>" + Memory + "</TD>"
    CPUArray[Count] += "<TD>" + HD + "</TD>"
    CPUArray[Count] += "<TD>" + Drive + "</TD>"
    CPUArray[Count] += "<TD>" + Video + "</TD>"
    CPUArray[Count] += "<TD>" + Modem + "</TD>"
    CPUArray[Count] += "<TD>" + Price + "</TD>"


    MemoryArray[Count] = "<TD>" + Memory + "</TD>"
    MemoryArray[Count] += "<TD>" + CPU + "</TD>"
    MemoryArray[Count] += "<TD>" + Name + "</TD>"
    MemoryArray[Count] += "<TD>" + HD + "</TD>"
    MemoryArray[Count] += "<TD>" + Drive + "</TD>"
    MemoryArray[Count] += "<TD>" + Video + "</TD>"
    MemoryArray[Count] += "<TD>" + Modem + "</TD>"
    MemoryArray[Count] += "<TD>" + Price + "</TD>"

    /*HDArray[Count] +=
    DriveArray[Count] +=
    VideoArray[Count] +=
    PriceArray[Count] +=*/

    Count++;
    Computer = Computer.nextSibling;
    }
    }

    function SortStats(How)
    {
    var Result = "";
    var Prev = "";

    Result += "<TABLE border=1 align=center>"

    if (How == "Name")
    {
    NameArray.sort();
    for (i = 0; i < NameArray.length; i++)
    {
    Result += "<TR>" + NameArray[i] + "</TR>"
    Prev = NameArray[i];
    }
    }

    if (How == "CPU")
    {
    CPUArray.sort();
    for (i = 0; i < CPUArray.length; i++)
    {
    Result += "<TR>" + CPUArray[i] + "</TR>"
    Prev = CPUArray[i];
    }
    }

    if (How == "Memory")
    {
    MemoryArray.sort();
    for (i = 0; i < MemoryArray.length; i++)
    {
    Result += "<TR>" + MemoryArray[2] + "</TR>"
    Prev = MemoryArray[i];
    }
    }

    Result += "</TABLE>"
    return Result;
    }
    </SCRIPT>
    </HEAD>
    <BODY STYLE="font-family:Verdana;" ONLOAD="LoadArrays(gigi)">
    <table width="100%">
    <tr>
    <td style="border: #999999 1px solid;"><TABLE align="center">
    <TR>
    <TD colspan="3" ALIGN=CENTER> <table width="100%" border="0">
    <tr>
    <td style="border: #999999 1px solid;"><div align="center"><strong>Apple
    Store</strong></div></td>
    </tr>
    </table>
    </TR>
    <TR>
    <!--<TD ALIGN=CENTER><input name="BUTTON" type=BUTTON onClick="ResultDiv.innerHTML = 'Highest Mark: ' + Compute_Stat(CISY2412Source, 'high')" value="Highest Mark"/>
    <TD ALIGN=CENTER><input name="BUTTON22" type=BUTTON onClick="ResultDiv.innerHTML = 'Lowest Mark: ' + Compute_Stat(CISY2412Source, 'low')" value="Lowest Mark"/>
    <TD ALIGN=CENTER><input name="BUTTON23" type=BUTTON onClick="ResultDiv.innerHTML = 'Average Mark: ' + Compute_Stat(CISY2412Source, 'avg')" value="Average Mark"/>--></TR>
    <TR>
    <TD ALIGN=CENTER><INPUT name="BUTTON1" TYPE=BUTTON ONCLICK="ResultDiv.innerHTML = SortStats('Name')" VALUE="Sort by Student Name"/>
    &nbsp;
    <TD ALIGN=CENTER><INPUT name="BUTTON2" TYPE=BUTTON ONCLICK="ResultDiv.innerHTML = SortStats('CPU')" VALUE="Sort by Instructor"/>
    &nbsp;
    <TD ALIGN=CENTER><INPUT name="BUTTON3" TYPE=BUTTON ONCLICK="ResultDiv.innerHTML = SortStats('Memory')" VALUE="Sort by Program"/>
    </TR>
    </TABLE></td>
    </tr>
    </table>
    <BR>
    <DIV ID=ResultDiv></DIV>
    </BODY>
    </HTML>

    Please help

    getchoo
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Posts
    20
    Rep Power
    0

    forgot to attach file


    whoops forgot to attach file
    Attached Files
  4. #3
  5. Senior Member
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2000
    Location
    Enschede, The Netherlands
    Posts
    1,527
    Rep Power
    16
    Hi, could you please mark line 54? To be honest I'm not in the mood to count lines
    There are 10 types of people in this world - those who understand binary and those who don't...

    PHP | MySQL | DevShed Forum Search | Google Search
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Posts
    20
    Rep Power
    0

    ok here are lines marked


    Sorry about that I wasn't sure u want me to marke the pasted code or rezipped the files with the marked lines so I did both

    <HTML><HEAD><TITLE>XML DOM Scripting</TITLE>
    <XML ID="gigi" SRC="macStore_xml.xml"></XML>
    <SCRIPT>


    function Compute_Stat(Doc, Which)
    {
    var Total = 0;
    var StudentCount = 0;
    var Marks = 0;

    var Highest = 0;
    var Lowest = 999;
    var HighestLastName = "";
    var HighestFirstName = "";
    var LowestLastName = "";
    var LowestFirstName = "";

    var Computer = Doc.documentElement.firstChild;
    /*while (Student != null)
    {
    Marks = parseInt(Student.selectSingleNode("Marks").text)
    LastName = Student.selectSingleNode("Last").text
    FirstName = Student.selectSingleNode("First").text



    if (Marks > Highest)
    {
    Highest = Marks;
    HighestFirstName = FirstName;
    HighestLastName = LastName;
    }
    if (Marks < Lowest)
    {
    Lowest = Marks;
    LowestFirstName = FirstName;
    LowestLastName = LastName;
    }



    Total += Marks
    Student = Student.nextSibling;
    StudentCount++;
    }
    if (Which == "avg")
    return Total/StudentCount;
    else if (Which == "high")
    return HighestFirstName + " " + HighestLastName + " Marks " + Highest;
    else if (Which == "low")
    return LowestFirstName + " " + LowestLastName + " Marks " + Lowest;
    */}

    var Computer = Doc.documentElement.firstChild;/*Doc undefined Line 54*/

    var NamesArray = new Array();
    var CPUArray = new Array();
    var MemoryArray = new Array();
    var HDArray = new Array();
    var DriveArray = new Array();
    var VideoArray = new Array();
    var ModemArray = new Array();
    var PriceArray = new Array();

    function LoadArrays(Doc)
    {
    var Count = 0;
    //var LastName;
    var Computer = Doc.documentElement.firstChild;//line69 object required.
    while (Computer != null)
    {
    Name = Computer.selectSingleNode("name").text
    CPU = Computer.selectSingleNode("cpu").text
    Memory= Computer.selectSingleNode("memory").text
    HD = Computer.selectSingleNode("hd").text
    Drive= Computer.selectSingleNode("drive").text
    Video= Computer.selectSingleNode("video").text
    Modem= Computer.selectSingleNode("modem").text
    Price = parseInt(Computer.selectSingleNode("price").text)

    NamesArray[Count] = "<TD>" + Name + "</TD>"
    NamesArray[Count] += "<TD>" + CPU + "</TD>"
    NamesArray[Count] += "<TD>" + Memory + "</TD>"
    NamesArray[Count] += "<TD>" + HD + "</TD>"
    NamesArray[Count] += "<TD>" + Drive + "</TD>"
    NamesArray[Count] += "<TD>" + Video + "</TD>"
    NamesArray[Count] += "<TD>" + Modem + "</TD>"
    NamesArray[Count] += "<TD>" + Price + "</TD>"

    CPUArray[Count] = "<TD>" + CPU + "</TD>"
    CPUArray[Count] += "<TD>" + Name + "</TD>"
    CPUArray[Count] += "<TD>" + Memory + "</TD>"
    CPUArray[Count] += "<TD>" + HD + "</TD>"
    CPUArray[Count] += "<TD>" + Drive + "</TD>"
    CPUArray[Count] += "<TD>" + Video + "</TD>"
    CPUArray[Count] += "<TD>" + Modem + "</TD>"
    CPUArray[Count] += "<TD>" + Price + "</TD>"


    MemoryArray[Count] = "<TD>" + Memory + "</TD>"
    MemoryArray[Count] += "<TD>" + CPU + "</TD>"
    MemoryArray[Count] += "<TD>" + Name + "</TD>"
    MemoryArray[Count] += "<TD>" + HD + "</TD>"
    MemoryArray[Count] += "<TD>" + Drive + "</TD>"
    MemoryArray[Count] += "<TD>" + Video + "</TD>"
    MemoryArray[Count] += "<TD>" + Modem + "</TD>"
    MemoryArray[Count] += "<TD>" + Price + "</TD>"

    /*HDArray[Count] +=
    DriveArray[Count] +=
    VideoArray[Count] +=
    PriceArray[Count] +=*/

    Count++;
    Computer = Computer.nextSibling;
    }
    }

    function SortStats(How)
    {
    var Result = "";
    var Prev = "";

    Result += "<TABLE border=1 align=center>"

    if (How == "Name")
    {
    NamesArray.sort();
    for (i = 0; i < NamesArray.length; i++)
    {
    Result += "<TR>" + NamesArray[i] + "</TR>"
    Prev = NamesArray[i];
    }
    }

    if (How == "CPU")
    {
    CPUArray.sort();
    for (i = 0; i < CPUArray.length; i++)
    {
    Result += "<TR>" + CPUArray[i] + "</TR>"
    Prev = CPUArray[i];
    }
    }

    if (How == "Memory")
    {
    MemoryArray.sort();
    for (i = 0; i < MemoryArray.length; i++)
    {
    Result += "<TR>" + MemoryArray[2] + "</TR>"
    Prev = MemoryArray[i];
    }
    }

    Result += "</TABLE>"
    return Result;
    }
    </SCRIPT>
    </HEAD>
    <BODY STYLE="font-family:Verdana;" ONLOAD="LoadArrays(gigi)">
    <table width="100%">
    <tr>
    <td style="border: #999999 1px solid;"><TABLE align="center">
    <TR>
    <TD colspan="3" ALIGN=CENTER> <table width="100%" border="0">
    <tr>
    <td style="border: #999999 1px solid;"><div align="center"><strong>Apple
    Store</strong></div></td>
    </tr>
    </table>
    </TR>
    <TR>
    <!--<TD ALIGN=CENTER><input name="BUTTON" type=BUTTON onClick="ResultDiv.innerHTML = 'Highest Mark: ' + Compute_Stat(CISY2412Source, 'high')" value="Highest Mark"/>
    <TD ALIGN=CENTER><input name="BUTTON22" type=BUTTON onClick="ResultDiv.innerHTML = 'Lowest Mark: ' + Compute_Stat(CISY2412Source, 'low')" value="Lowest Mark"/>
    <TD ALIGN=CENTER><input name="BUTTON23" type=BUTTON onClick="ResultDiv.innerHTML = 'Average Mark: ' + Compute_Stat(CISY2412Source, 'avg')" value="Average Mark"/>--></TR>
    <TR>
    <TD ALIGN=CENTER><INPUT name="BUTTON1" TYPE=BUTTON ONCLICK="ResultDiv.innerHTML = SortStats(gigi,'Name')" VALUE="Sort by Name"/>
    &nbsp;
    <TD ALIGN=CENTER><INPUT name="BUTTON2" TYPE=BUTTON ONCLICK="ResultDiv.innerHTML = SortStats(gigi,'CPU')" VALUE="Sort by CPU"/>
    &nbsp;
    <TD ALIGN=CENTER><INPUT name="BUTTON3" TYPE=BUTTON ONCLICK="ResultDiv.innerHTML = SortStats(gigi,'Memory')" VALUE="Sort by Memory"/>
    </TR>
    </TABLE></td>
    </tr>
    </table>
    <BR>
    <DIV ID=ResultDiv></DIV>
    </BODY>
    </HTML>
    Attached Files
  8. #5
  9. Senior Member
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2000
    Location
    Enschede, The Netherlands
    Posts
    1,527
    Rep Power
    16
    First, please post code in vB code 'blocks' next time
    Furthermore, using tabs is a good way to check your loops and if-statements (for correct closure)
    Code:
    function Compute_Stat(Doc, Which)
    {
    	var Total = 0;
    	var StudentCount = 0;
    	var Marks = 0;
    	
    	var Highest = 0;
    	var Lowest = 999;
    	var HighestLastName = "";
    	var HighestFirstName = "";
    	var LowestLastName = "";
    	var LowestFirstName = "";
    	
    	var Computer = Doc.documentElement.firstChild;
    	/*while (Student != null)
    	{
    		Marks = parseInt(Student.selectSingleNode("Marks").text)
    		LastName = Student.selectSingleNode("Last").text
    		FirstName = Student.selectSingleNode("First").text
    
    		if (Marks > Highest)
    		{
    			Highest = Marks;
    			HighestFirstName = FirstName;
    			HighestLastName = LastName;
    		}
    		if (Marks < Lowest)
    		{
    			Lowest = Marks;
    			LowestFirstName = FirstName;
    			LowestLastName = LastName;
    		}
    		Total += Marks
    		Student = Student.nextSibling;
    		StudentCount++;
    	}
    	if (Which == "avg")
    		return Total/StudentCount;
    	else if (Which == "high")
    		return HighestFirstName + " " + HighestLastName + " Marks " + Highest;
    	else if (Which == "low")
    		return LowestFirstName + " " + LowestLastName + " Marks " + Lowest;
    	*/
    }
    
    var Computer = Doc.documentElement.firstChild;/*Doc undefined Line 54*/
    var NamesArray = new Array();
    var CPUArray = new Array();
    var MemoryArray = new Array();
    var HDArray = new Array();
    var DriveArray = new Array();
    var VideoArray = new Array();
    var ModemArray = new Array();
    var PriceArray = new Array();
    
    function LoadArrays(Doc)
    {
    
    ...
    Those vars (line 54 and below) are outside the function above it and like ther erros says, Doc is undefined outside that function if I saw it right. Besides, you have that lin (var Computer = Doc.documentElement.firstChild twice in your script.. once inside the function (right before the comment) and one below it
    There are 10 types of people in this world - those who understand binary and those who don't...

    PHP | MySQL | DevShed Forum Search | Google Search
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Posts
    20
    Rep Power
    0
    Hi

    How do I place vb blocks to preserve formatting of code. Secondly I tried remove the first occurence of
    var Computer = Doc.documentElement.firstChild;

    but I get "doc" is undefined in the second occurence of
    var Computer = Doc.documentElement.firstChild;

    how do I fix this problem

    thanx

    getchoo
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Posts
    20
    Rep Power
    0

    Thanx


    Hi

    I just figure out what u meant, and it solve the problem.

    thanks

    getchoo
  14. #8
  15. Senior Member
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2000
    Location
    Enschede, The Netherlands
    Posts
    1,527
    Rep Power
    16
    Great

    About the vB code and 'code'-fields:
    http://forums.devshed.com/misc.php?action=bbcode

    [ CODE ](without the spaces)[ /CODE ]
    [ PHP ](without the spaces)[ /PHP ]
    [ QUOTE ](without the spaces)[ /QUOTE ]

    is what you need -> it makes the code much clearer for people to read
    There are 10 types of people in this world - those who understand binary and those who don't...

    PHP | MySQL | DevShed Forum Search | Google Search

IMN logo majestic logo threadwatch logo seochat tools logo