views:

28

answers:

1

Hi, i have a code line like this: (JS)

numOfprocess = parseInt(xmlDoc.getElementsByTagName('process_count')[0].childNodes[0].nodeValue)
for (i = 1; i <= numOfProcess; i++)
{
processStatus = xmlDoc.getElementsByTagName('proccess' + i)[0];
if(processStatus.childNodes[0].nodeValue == false)
{...}
}

everytime i use this syntax there is an error "obejct required" while in Firefox eveything is ok. (the ... are just to explain)

i tried to do some debug like this:

alert(processStatus.childNodes[0].nodeValue) 

and the result was 0 so the var is fine. (also worked in ff so..)

the xml:

         <process_count>2</process_count>
         <Application_Status>
                        <proccess2>1</proccess2>
         </Application_Status>

another thing is that for i=1 it's ok but for i=2 not.

Thank you.

+1  A: 

Indexing starts from 0, so if you have three items, their corresponding indexes are 0, 1 and 2, so you need to loop:

for (i = 1; i <  numOfProcess; i++)

instead of:

for (i = 0; i <= numOfProcess; i++)

EDIT:

You don't need a for loop to access your data, you can easily access required value via:

var processId = xmlDoc.getElementsByTagName('process_count')[0].childNodes[0].nodeValue;
var processStatus = xmlDoc.getElementsByTagName('process'+processId)[0].childNodes[0].nodeValue;

But, I suggest reconsidering your xml schema, since you need no more than one process status, why not doing simple thing like:

<application>
    <process id="2" status="1" />
</application>
aularon
but numOfProcess can be bigger than 1. only processStatus can be 0 or 1
Ronny
Yes, it can be bigger than 1, but your script loops till `numOfProcess`, this means, if `numOfProcess` was equal to `2`, your script will loop 4 times (i=0,i=1,i=2,i=3), cuz it loops till `<= numOfProcess`. while it should stop a step earlier, so we use `< numOfProcess`.
aularon
@Ronny: read the (edited) answer again. I think you're misreading it.
slebetman
it's not working for me. the numOfProcess is numOfProcess = parseInt(xmlDoc.getElementsByTagName('process_count')[0].childNodes[0].nodeValue);
Ronny
@Ronny, I edited it, it was `i = 1`, now it's `i = 0`, try with it now.
aularon
@Ronny, try to do `numOfProcess = xmlDoc.getElementsByTagName('proccess').length;` to ensure you are getting the right total, If nothing works, edit your question adding the xml structure you are working with, and a bit more of your code (that has to do with looping and extracting data, where it's failing to dowhat you want).
aularon
i added the xml structure. i tried all the options but than i got nothing in ff and ie.
Ronny
does `<process_count>` contain the number of another one element? like there will be only another `<proccess{num}>{num}</proccess{num}>` tag inside `Application_Status`?
aularon
no. there is only one <proccess{num}> per Application_Status. if <process_count> is 2 so it will run twice. when i=1 than <Application_Status> <proccess1>1 </proccess1> </Application_Status> and for i=2 <Application_Status> <proccess2>1 </proccess2> </Application_Status>
Ronny
@Ronny, edited check now.
aularon
the solution is not good for me because i have many vars that related to the process_count. i prefer to stay with `i=1`. again, in ff everything is working perfectly.
Ronny
i resolved it alone. thank you.
Ronny
@Ronny You can put an answer describing the problem and how you managed to solve it, and accept is as an answer. This way when somebody searches and end up here can know how to solve his problem.
aularon