Jump to content

How to fix Invalid Array Length Error?

- - - - -

  • Please log in to reply
No replies to this topic

#1
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
I have a script that is supposed to work on an XML document. Though the problem is that the script causes an 'Invalid Array Length' exception; why is that?

Here's the XML code:
<file type="text/xml-****"> 

	<header> 

		<object> 

			<points> 

				vector= figure1.branch1 

				1 (170, 482, 123) 

				2 (280, 482, 123) 

				3 (280, 200, 143) 

				4 (170, 200, 143) 

			</points> 

			<faces> 

				1, 2, 3, 4 = #FF0000 

			</faces> 

		</object> 

		<object> 

			<points> 

				vector= figure2 

				1 (100, 100, 100) 

				2 (200, 200, 200) 

				3 (50, 100, 0) 

			</points> 

			<faces> 

				1, 2, 3 = #00FF00 

			</faces> 

		</object> 

	</header> 

</file> 

Here's the JavaScript code ('xdoc' is the variable for the XML document)[noparse]:[/noparse]
var head= xdoc.getElementsByTagName ("header")[0]; 

var arr= head.getElementsByTagName ("object"); 

var i; 

var a= new Array (); 

for (i in arr){ 

  a[i]= new Object (); 

  a[i].pts= new Array (); 

} 

What am I doing wrong?

---------- Post added at 06:58 PM ---------- Previous post was at 06:45 PM ----------

Oh, I get it now; not all the members of 'arr' happen to be integers. This works:
var head= xdoc.getElementsByTagName ("header")[0]; 

var arr= head.getElementsByTagName ("object"); 

var i; 

var a= new Array (); 

for (i in arr){ 

  if (isNaN (parseInt (i))) break; 

  a[i]= new Object (); 

  a[i].pts= new Array (); 

} 





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users